如何编辑Powershell脚本以继续出现错误?

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

看起来你运行get-wmiObject时遇到了这个问题,在那个调用写完之后

 -ErrorAction "Resume" 

看起来$ OS实际上是空值,所以你可能想要做这样的事情,知道如果$ s是空的,并做出决定做什么:

 if ($OS -ne $null){ $uptime = (get-date) - $os.converttodatetime } else { write-host " OS is null" } 

有关错误handeling的更多信息:

http://blogs.msdn.com/b/kebab/archive/2013/06/09/an-introduction-to-error-handling-in-powershell.aspx

尝试使用-erroraction静静地继续