简体   繁体   中英

Stop Powershell Script if disk size is less than X

I wonder if you could help me out guys. I need to stop a powershell script if the disk size of partition E is less than 10GB, and to continue if it´s more than 10GB.

So far i managed to get my disk size listed with this.

Get-WmiObject -Class win32_logicaldisk | Format-Table DeviceId,@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

And i get this result:

DeviceId Freespace
A 0
C 77.9
D 0
E 34.05

So, i want to stop the powershell script if E unit has less than 10GB. How can i do it?

Thanks in advance

If you want to put the Freespace of E in a variable you can do this:

$VarSpace = $(Get-WmiObject -Class win32_logicaldisk | Where-Object -Property Name -eq C:).FreeSpace/1GB

then you can do a simple if for check:

if ($VarSpace -le 10){ <Something for stopping you script like exit> }

You can use the Get-Volume cmdlet for that or Get-CimInstance rather than the old Get-WmiObject :

$freeOnE = (Get-CimInstance -ClassName win32_logicaldisk | Where-Object {$_.DeviceID -eq 'E:'}).FreeSpace / 1GB

or

$freeOnE = (Get-Volume -DriveLetter E).SizeRemaining / 1GB

Then exit your PowerShell session if this value is below 10Gb

if ($freeOnE -lt 10) { exit }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM