【发布时间】:2020-10-29 00:53:46
【问题描述】:
我是 power shell 脚本的新手。我有一个 Power shell 脚本来检查可用磁盘空间并从文件夹中删除一些旧的子文件夹,直到可用空间达到阈值水平。
我的代码删除了所有文件夹,并且不会在任何地方退出。我正在检查可用空间是否大于可用空间并尝试终止它。 ($part.FreeSpace -gt $desiredBytes)..
我已经回应了 $desiredBytes、$part.FreeSpace、$directoryInfo.count。即使删除了一个巨大的文件夹,这些变量的值也不会更新。所以,所有文件夹都被删除了,但它仍然没有终止。
有人可以帮我解决这个问题吗?提前致谢:)
[WMI]$part = "Win32_LogicalDisk.DeviceID='D:'"
$directory = "D:\Suba\Suba\"
$desiredGiB = 262
$desiredBytes = $desiredGiB * 1073741824
$directoryInfo = Get-ChildItem $directory | Measure-Object
$directoryInfo.count #Returns the count of all of the objects in the directory
do{
if($part.FreeSpace -gt $desiredBytes)
{ exit
}
if ($directoryInfo.count -gt 0) {
echo $desiredBytes
echo $part.FreeSpace
echo $directoryInfo.count
foreach ($root in $directory) {
Get-ChildItem $root -Recurse |
Sort-Object CreationTime |
Select-Object -First 1 |
Remove-Item -Force
}
}
else
{
Write-Host "Enough Files are not there in this directory!!"
exit
}
}
while($part.FreeSpace -lt $desiredBytes)
【问题讨论】:
标签: powershell powershell-2.0 powershell-3.0