I am trying to build a script that would start up my domain services in order in the event of a complete power failure
I am booting up the machines remotely using an iDrac, waiting a few minutes then checking the system uptime using get-wmiobject to make sure that the operating system is loaded.
To do this I have this code I found on a website
$timeinseconds = ((get-date)-[system.management.managementdatetimeconverter]::todatetime((get-wmiobject -class win32_operatingsystem -computer servername -erroraction silentlyContinue -ErrorVariable badoutput).Lastbootuptime)).TotalSeconds
Now this works fine when the system is online, but if the system is offline it throws an error message
Exception calling "ToDateTime" with "1" argument(s): "Specified argument was out of the range of valid values.
Parameter name: dmtfDate"
I am guessing this is because it is trying to convert the variable to .TotalSeconds but there is no value in the $timeinseconds.
I have added the erroraction and errorvariable commands so that I can store the error, but this I think is only for the wmi section, as it will store "the rpc server is unavailable" as expected, but the exception on top is still generated.
What could I change to get this to work better?
Thanks in advance for any help.