我有这个脚本。但是运行脚本时出现此错误。
Write-Host:无法将参数'ForegroundColor'绑定到目标。例外设定 “ ForegroundColor”:“由于枚举值,无法将null转换为类型” System.ConsoleColor“ 无效的。指定以下枚举值之一,然后重试。可能的 枚举值是“黑色,深蓝色,深绿色,深青色,深红色,深洋红色,深黄色,灰色,深 kGray,蓝色,绿色,青色,红色,洋红色,黄色,白色”。 在C:\ Scripts \ DA \ volCheck.ps1:49 char:33 +写主机$ _ -ForegroundColor $ color + ~~~~~~ + CategoryInfo:WriteError:(:) [Write-Host],ParameterBindingException + FullyQualifiedErrorId:ParameterBindingFailed,Microsoft.PowerShell.Commands.WriteHostComm 和
请帮助,非常感谢。
函数volcheck { 参数( [参数(必填)] [string] $ PClist )
$servers = Get-Content -Path $PClist
cls
Write-Host "`nChecking Disk Space:`n" -ForegroundColor Green
$obj = @()
Foreach ($server in $servers) {
If (Test-Connection -Computername $server -Count 2 -Quiet) {
#$volumes = (Get-WmiObject Win32_Volume -ComputerName $server -Credential $cred -Filter "DriveType = 3")
$volumes = (Get-WmiObject Win32_Volume -ComputerName $server -Filter "DriveType = 3")
Foreach ($vols in $volumes) {
$FreePerc = [math]::round(((($vols.FreeSpace / 1GB)/($vols.Capacity / 1GB)) * 100),0)
if ($FreePerc -ge 20) {
$StatusProperty = "OK"
}
elseif ($FreePerc -ge 15 ) {
$StatusProperty = "Warning"
}
else {
$StatusProperty = "Critical"
}
$obj += $vols | Select @{Name = "Server"; Expression = {$server}}, Name, Label, DriveLetter, FileSystem,
@{Name = "Capacity(GB)"; Expression = {[math]::round(($_.Capacity / 1GB),2)}},
@{Name = "FreeSpace(GB)"; Expression = {[math]::round(($_.FreeSpace / 1GB),2)}},
@{Name = "PctFree"; Expression = {$FreePerc}},
@{Name = "Status"; Expression = {$StatusProperty}}
$obj.psobject.typenames.insert(0,"MyDiskUsage")
}
} else {
Write-Host "$server not pingable"`n -ForegroundColor Red
}
}
$strings = ($obj | Format-Table -Auto | Out-String).Trim().split("`n")
$strings | select -first 2 | write-host -ForegroundColor Cyan
$strings | select -Skip 2 | foreach {
Switch -regex ($_) {
"OK" { $color = "Green" }
"Warning" { $color = "Yellow" }
"Critical" {$color = "Red" }
}
Write-Host $_ -ForegroundColor $color
}
write-Host "`n"
pause
}