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

0

I modified this script(https://gallery.technet.microsoft.com/scriptcenter/Create-HTML-Uptime-and-68e6acc0) 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
windows-server-2008
powershell
asked on Server Fault Mar 3, 2015 by blashmet

2 Answers

1

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 :

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

answered on Server Fault Mar 3, 2015 by DevOpsDevon
0

Try using -erroraction silentlycontinue

answered on Server Fault Mar 10, 2015 by Dave

User contributions licensed under CC BY-SA 3.0