2014-10-09 91 views
0

我正嘗試使用DCIM_PhysicalDiskView中的primaryStatus並將其與3(降級硬盤)進行比較。如果有匹配,則會發送一封電子郵件通知管理員。這裏是代碼:獲取DCIM_PhysicalDiskView的PrimaryStatus並將其放入Powershell中的變量

$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 = "[email protected]" # your from address, mail server will most likely allow any 
$smtpTo = "[email protected]" #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保留爲空,因此不通過if語句。

對此的任何幫助將不勝感激。謝謝!

+0

是'$ _。PimaryStatus'是否爲錯字?第二個ForEach也看起來不正確。您每次分配'$ primaryStatus',所以我只會記住最後一個條目。 – Matt 2014-10-09 19:03:15

+0

@Matt這是一個錯字。 – kaka 2014-10-13 13:27:19

回答

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" 
    } 

免責聲明:我沒有這樣的命名空間,所以我不能對此進行測試,但邏輯看起來聽起來那麼它應該工作。

+0

你是對的。建議的一段代碼就像一個冠軍!非常感謝你,你救了我很多頭痛。 – kaka 2014-10-13 13:28:47

相關問題