获取DCIM_PhysicalDiskView的PrimaryStatus并将其放入Powershell中的变量中

时间:2014-10-09 18:21:27

标签: powershell wmi

我正在尝试使用DCIM_PhysicalDiskView中的primaryStatus并将其与3(降级硬盘)进行比较。如果匹配,将发送电子邮件通知管理员。这是代码:

$computerNames = Get-Content -Path C:\scripts\nameTest.txt

foreach ($computer in $computerNames) {

write-host "$computer" 
$value = "3"
$smtpserver = "mailserver.xxxx.com" # your mail server here
$smtpFrom = "xx@xxxx.com" # your from address, mail server will most likely allow any
$smtpTo = "xx@xxxx.com" #your email address here. can also be an array and will send to all
$MessageSubject = "Testing Failed Disk in $computer" #whatever you want the subject to be. 


gwmi -Namespace root\dcim\sysman -computername $computer -Class DCIM_PhysicalDiskView | ForEach {$name = $_.Name; $primaryStatus = $_.PimaryStatus}
    if( $primaryStatus -contains $value )
    {
        Send-MailMessage -SmtpServer $smtpserver -from $smtpFrom -to $smtpto -subject $messageSubject
        Write-Host "error message sent"
    }
}

我的问题是命令没有管道到foreach。 $ name和$ primaryStatus保持为null,因此不会通过if语句。

对此的任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

下面是您的代码,其中为foreach循环更改了大括号。请注意,您仍需要从开头foreach-object获得结束括号。

gwmi -Namespace root\dcim\sysman -computername $computer -Class DCIM_PhysicalDiskView | ForEach-Object {
    $name = $_.Name
    $primaryStatus = $_.PrimaryStatus
    if( $primaryStatus -eq $value )
    {
        Send-MailMessage -SmtpServer $smtpserver -from $smtpFrom -to $smtpto -subject $messageSubject
        Write-Host "error message sent"
    }
}

修正了$_.PrimaryStatus的拼写错误。 $primaryStatus是一个数组吗? -Contains仅适用于数组。我想你想-eq?我没有访问该命名空间,所以我无法测试我的理论。另外,我认为您可以使用Where-Object稍微更改一下。

gwmi -Namespace root\dcim\sysman -computername $computer -Class DCIM_PhysicalDiskView | 
    Where-Object { $primaryStatus -eq $value } | ForEach-Object{
        Send-MailMessage -SmtpServer $smtpserver -from $smtpFrom -to $smtpto -subject $messageSubject
        Write-Host "error message sent"
    }

免责声明:我没有那个命名空间,所以我无法测试这个,但逻辑看起来很有效,所以应该可以工作。