全部,我想知道以下内容有什么问题。 我对输出到html或更改输出格式不感兴趣
$servers = Get-Content "c:\x\servers.txt";
$File = "C:\x\serverslowdrivespace.txt"
$datetime = Get-Date -Format "MM-dd-yyyy_HH:mm";
Write-Host $datetime
$datetime | Out-File $File
"`n" | Out-File $File -Append
foreach ($server in $servers)
{
#Write-Host $server
$server | Out-File $File -Append
Get-WmiObject -query "SELECT * from Win32_logicaldisk WHERE DriveType ='3'
-AND (($_.freespace/$_.Size)*100) -le 10)" | Format-Table DeviceID, @{ Name
= "Size(GB)" ; Expression = { [decimal] ( "{0:N0}" -f ($_.size /
1gb) ) } }, @{ Name = "Free Space(GB)" ; Expression = { [decimal]
( "{0:N0}" -f ($_.freespace / 1gb) ) } }, @{ Name = "Free (%)" ; Expression
= { "{0,6:P0}" -f (($_.freespace / 1gb) / ( $_.size / 1gb )) } } -AutoSize
| Out-File $File -Append
}
答案 0 :(得分:0)
我认为你现在遇到的问题是你正在使用$ drive,但$ drive永远不会定义,因为它不会那样工作。我不知道制作WMI查询的方法包括计算结果作为其WHERE子句的一部分。相反,我建议你做的是管道你所有的drivetype =' 3'驱动到PowerShell中的Where子句。这样您就可以将其过滤到只有低可用空间的驱动器。在下面的建议中,我使用您在Format-Table选项中创建的计算属性创建自定义对象,只是为了尝试保持清洁。如果您遇到问题,请尝试删除管道后的换行符,但我不知道PSv2是否友好。
$servers = Get-Content "c:\x\servers.txt"
$File = "C:\x\serverslowdrivespace.txt"
$datetime = Get-Date -Format "MM-dd-yyyy_HH:mm"
Write-Host $datetime
$datetime | Out-File $File
"`n" | Out-File $File -Append
foreach ($server in $servers)
{
$server | Out-File $File -Append
Get-WmiObject -ComputerName $server -query "SELECT * from Win32_logicaldisk WHERE DriveType ='3'" | #Query server for hard drives
Where{(($_.freespace/$_.Size)*100) -lt 10} | #Filter for drives with less than 10% free space
ForEach{New-Object PSObject -Property @{
"DeviceID" = $_.DeviceID
"Size(GB)" = [decimal] ( "{0:N0}" -f ($_.size / 1gb) )
"Free Space(GB)" = [decimal] ( "{0:N0}" -f ($_.freespace / 1gb) )
"Free (%)" = "{0,6:P0}" -f (($_.freespace / 1gb) / ( $_.size / 1gb ))
}
} |
Format-Table -AutoSize | Out-File $File -Append
}