简体   繁体   English

如果磁盘大小小于 X,则停止 Powershell 脚本

[英]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.如果分区 E 的磁盘大小小于 10GB,我需要停止 powershell 脚本,如果大于 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 0
C C 77.9 77.9
D 0 0
E 34.05 34.05

So, i want to stop the powershell script if E unit has less than 10GB.所以,如果 E 单元小于 10GB,我想停止 powershell 脚本。 How can i do it?我该怎么做?

Thanks in advance提前致谢

If you want to put the Freespace of E in a variable you can do this:如果你想把 E 的自由空间放在一个变量中,你可以这样做:

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

then you can do a simple if for check:那么你可以做一个简单的 if 来检查:

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 :您可以为此使用Get-Volume cmdlet 或Get-CimInstance而不是旧的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如果此值低于 10Gb,则退出 PowerShell session

if ($freeOnE -lt 10) { exit }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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