有人要求我监控 Windows 服务器上的 c:\temp。我需要知道有多少个文件夹,以及它们存在的时间是否超过 1、2 和 3 分钟。此报告以基本形式工作,但并不完全正确。一旦有 1 个文件夹,我仍然会收到一份报告,称它存在超过 3 分钟,这是错误的。我的 for 循环有什么问题?我想要使用 for 循环来计算存在且具有特定年龄的文件夹的数量。但是 for 循环循环一次,然后我得到计数器 = 1,即使这是错误的。即它是几秒前,但我收到的报告说它存在了 3 分钟。任何输入都将不胜感激。谢谢
以下是我的可能很差的代码...
$Now = Get-Date
$OneMinute = 1
$TwoMinutes = 2
$ThreeMinutes = 3
$TargetFolder = "C:\Temp"
$LastWrite1Min = $Now.AddMinutes(-$OneMinute)
$LastWrite2Min = $Now.AddMinutes(-$TwoMinutes)
$LastWrite3Min = $Now.AddMinutes(-$ThreeMinutes)
$i=0
$j=0
$k=0
$g=0
$StringThere = "There are "
$StringOne = "Folder(s) older than 1 Minute"
$StringTwo = "Folder(s) Older than 2 Minute"
$StringThree = "Folder(s) Older than 3 Minute"
$directoryInfo = Get-ChildItem C:\Temp | Measure-Object
#$directoryInfo.count #Returns the the number folders within the directory
If ($directoryInfo.count -eq 0){
write-host "Folder is empty - Good!"
}
##################################################
If ($directoryInfo.count -ne 0){
write-host "Temp has at least one folder present - ... "
$Folders = get-childitem -path $TargetFolder |
Where {$_.psIsContainer -eq $true} |
Where {$_.LastWriteTime -le "$LastWrite1Min"}
foreach ($Folder in $Folders)
{
$i = $i + 1
}
$Folders = get-childitem -path $TargetFolder |
Where {$_.psIsContainer -eq $true} |
Where {$_.LastWriteTime -le "$LastWrite2Min"}
foreach ($Folder in $Folders)
{
If ($Folder -ne "`0" ){
$j = $j + 1
}
}
$Folders = get-childitem -path $TargetFolder |
Where {$_.psIsContainer -eq $true} |
Where{ $_.LastWriteTime -le "$LastWrite3Min"}
foreach ($Folder in $Folders)
{
if($_.LastWriteTime -le "$LastWrite3Min"){
}
}
foreach ($Folder in $Folders)
{
if($_.LastWriteTime -le "$LastWrite3Min")
{
$k = $k + 1
}
}
}#end of not zero if
#######################################################
write-host $StringThere $i $StringOne
write-host $StringThere $j $StringTwo
write-host $StringThere $k $StringThree
write-host $exitcode
exit $exitcode
我需要正确的退出代码,但 for 循环却让这个代码完全错误。谢谢!
答案1
这是一个可以运行的版本,可以进一步改进。
$Now = Get-Date
$TargetFolder = "C:\Temp"
$LastWrite1Min = $Now.AddMinutes(-1)
$LastWrite2Min = $Now.AddMinutes(-2)
$LastWrite3Min = $Now.AddMinutes(-3)
$i=0
$j=0
$k=0
$StringThere = "There are "
$StringOne = "Folder(s) older than 1 Minute"
$StringTwo = "Folder(s) Older than 2 Minute"
$StringThree = "Folder(s) Older than 3 Minute"
$directoryInfo = Get-ChildItem C:\Temp | Measure-Object
#$directoryInfo.count #Returns the the number folders within the directory
If ($directoryInfo.count -eq 0){
write-host "Folder is empty - Good!"
}
##################################################
If ($directoryInfo.count -ne 0)
{
write-host "Temp has at least one folder present - ... "
$Folders = get-childitem -path $TargetFolder |
Where {$_.psIsContainer -eq $true}
foreach ($Folder in $Folders)
{
if ($Folder.LastWriteTime -le $LastWrite3Min)
{
$k++
}
elseif ($Folder.LastWriteTime -le $LastWrite2Min)
{
$j++
}
elseif ($Folder.LastWriteTime -le $LastWrite1Min)
{
$i++
}
}
}#end of not zero if
#######################################################
write-host $StringThere $i $StringOne
write-host $StringThere $j $StringTwo
write-host $StringThere $k $StringThree