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

# Call the Get-WMIObject cmdlet
        $servicename = get-wmiobject win32_service -computername $server -ErrorAction Stop 
        # 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
    # 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

Verbose Parameter Passing to cmdlet inside Function