Powershell Trapping Get-WMIObject

There are many times when I have to connect to remote servers and get information using WMI but not all of the servers in my list are available all the time...

Even though the error from the Get-WMIObject cmdlet is not terminating, I want to handle it.

So I use the following code to do it:

# Declare the variable to hold the result
$servicename

# Call the Get-WMIObject cmdlet
try
    {
        $servicename = get-wmiobject win32_service -computername $server -ErrorAction Stop 
    }
catch
    {
        # If an exception is thrown, set the variable to null
        $servicename = $null
    }
 
# Check the value of the variable.
if( -Not($servicename -eq $null) )
{
     # Continue processing
}
else
{
    # Exception was thrown
}







 
I've added the "-ErrorAction Stop" in order to handle the non terminating error.

Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Running Multiple NGINX Ingress Controllers in AKS