【问题标题】:If / Else statement using Get-WMIObject使用 Get-WMIObject 的 If/Else 语句
【发布时间】:2018-02-19 21:57:12
【问题描述】:

我正在尝试使用“Get-WMIObject”。

如果我运行脚本,它会以红色文本输出,而不是绿色,因为它应该基于 if/else 语句。此外,在脚本运行后,如果我键入 $PredictFailure,它会显示 $PredictFailure 为“False”。

显示正在发生的事情的屏幕截图。

$PredictFailure = Get-WmiObject -namespace root\wmi –class MSStorageDriver_FailurePredictStatus | Select-Object PredictFailure

Foreach ($D in $PredictFailure)

{

$PredictFailure = $D.PredictFailure

    If ($D.PredictFailure -eq "False")

    {Get-WmiObject -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ComputerName $env:computername | Select-Object PSComputerName, Active, PredictFailure, Reason | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Green}

Else

{Get-WmiObject -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ComputerName $env:computername | Select-Object PSComputerName, Active, PredictFailure, Reason | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Red}

}

【问题讨论】:

    标签: powershell if-statement get-wmiobject


    【解决方案1】:

    您正在与字符串“False”进行比较。使用$FALSE 引用布尔常量false:

    If ($D.PredictFailure -eq $FALSE)
    

    或使用否定

    If (-not $D.PredictFailure)
    

    【讨论】:

    • 谢谢 - 我是新的,它必须是一个简单的修复。从来没有想过这一点,因为我有几乎相同的脚本可以在没有布尔引用的情况下工作。我 60 岁,上周刚决定开始学习 PowerShell(我住在科罗拉多的地方下雪了,去滑雪很冷,所以我想我会尝试一些新的东西)。再次感谢,我当然会接受您的回答,以便您获得积分
    • 很高兴我能提供帮助,并感谢您的积分。所以你也是编程新手?
    • 是的,PowerShell - 我在 70 年代后期的大学里学习了“基本”。我第一次打开 PowerShell 控制台是在一周前。在你和这里的另一个编码员的帮助下,我终于完成了一个脚本,它根据磁盘可用空间、健康状态、潜在故障和其他一些东西输出不同的前景色。这既有趣又令人沮丧,但多亏了你们,我完成了我打算做的事情。
    猜你喜欢
    • 2019-04-09
    • 1970-01-01
    • 2017-02-05
    • 2015-07-06
    • 2012-08-05
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多