我修改了這個腳本(https://gallery.technet.microsoft.com/scriptcenter/Create-HTML-Uptime-and-68e6acc0)來存取多個伺服器,但即使只有一個系統關閉,它也不會產生報告。如何修改腳本以繼續出現錯誤並在最後產生報告?錯誤是:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\user\Desktop\CheckDiskSpaceDomain\GetDiskDriveSpaceDomain.ps1:19 char:25
+ $os = Get-WmiObject <<<< -class win32_OperatingSystem -cn $s
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
You cannot call a method on a null-valued expression.
At C:\Users\user\Desktop\CheckDiskSpaceDomain\GetDiskDriveSpaceDomain.ps1:21 char:51
+ uptime = (get-date) - $os.converttodatetime <<<< ($os.lastbootuptime)}
+ CategoryInfo : InvalidOperation: (converttodatetime:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
答案1
看起來您在執行 get-wmiObject 時遇到了問題,在呼叫之後只需編寫
-ErrorAction "Resume"
看起來 $OS 實際上是空值,所以你可能想做這樣的事情來知道 $s 是否為空並決定要做什麼:
if ($OS -ne $null){
$uptime = (get-date) - $os.converttodatetime
}
else {
write-host " OS is null"
}
有關錯誤處理的更多資訊:
答案2
嘗試使用 -erroraction 靜默繼續