How do you edit a Powershell script to continue on an error?


I modified this script( to hit multiple servers, but it doesn't generate a report even if only one of the systems are down. How do I modify the script to continue on errors and generate the report at the end? The error is:

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
asked on Server Fault Mar 3, 2015 by blashmet

2 Answers


Looks like you have the issue when you run get-wmiObject, after that call just write

-ErrorAction "Resume"

Looks like the $OS is actually the null-value so you might want to do something like this that will know if $s is null and make a decision of what to do:

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

For more info on error handeling :

answered on Server Fault Mar 3, 2015 by DevOpsDevon

Try using -erroraction silentlycontinue

answered on Server Fault Mar 10, 2015 by Dave

User contributions licensed under CC BY-SA 3.0