我修改了这个脚本(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 silentlycontinue