question

KedarTamboli-6326 avatar image
0 Votes"
KedarTamboli-6326 asked Amandayou-MSFT edited

WMI Query to Calculate Disk Free SPace Percentage

Hello Team,

I want to configure Task Sequence Option with WMI Query condition. Remediation script should run if condition met:

If C: Free Space is less than 10% of total C: OR C: Free Space is less than 5 GB then remediation script will run.

Please suggest if below WMI query is correct or needs modification.

"Select * from Win32_LogicalDisk WHERE (NAME LIKE 'C:') AND ((FREESPACE <= '5368709120') OR (FREESPACE/SIZE <= '0.1'))"

mem-cm-generalmem-cm-osd
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your posting on Q&A.

I will do some research further and I will come back as soon as possible. Please keep us in touch if there are any updates of the case.

Thanks for your understanding and cooperation.

Best regards,
Amanda

0 Votes 0 ·

1 Answer

Amandayou-MSFT avatar image
0 Votes"
Amandayou-MSFT answered Amandayou-MSFT edited

Hi @KedarTamboli-6326,

Mathematical operations are not supported in WMI, so FREESPACE/SIZE <= '0.1' can not be used.

We could use wbemtest to see if the WMI query is correct. Here is the result of this query:

199481-56.png

So we could use powershell script to achieve it. Here is the powershell, kindly refer to it:

 $DiskCount = ((Get-WmiObject -Class Win32_DiskDrive).Caption).count
 $DiskInfo = Get-WmiObject -Class Win32_LogicalDisk 
 echo "--------------------Statistics disk partition status-------------------------"
 echo "    Drive letter    Free space   Remaining proportions"        
 foreach ($Drivers in $DiskInfo) 
 {
     $PartitionID = $Drivers.DeviceID
         if ($PartitionID -eq 'c:'){
             $PartitionSize = "{0:N2}GB" -f ($Drivers.Size/1GB)
             $PartitionFreeSize = "{0:N2}GB" -f ($Drivers.FreeSpace/1GB)
             $PartitionFree = ($PartitionFreeSize/$PartitionSize)*100
                 if(($PartitionFree -le 10) -or ($PartitionFreeSize/1 -lt 5GB)){
                     echo "    $PartitionID       $PartitionFreeSize    $PartitionFree%" 
                  }
         }
 }


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




56.png (19.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.