Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Catching particular WMI exception

$
0
0

Hello All

Can someone please help me with the following question.

I am trying to be more specifc when catching exceptions

if I do the following

Get-WmiObject -ComputerName Server1 -Class Win32_PowerPlan -Namespace "root\cimv2\power"  -ErrorAction stop | where {$_.IsActive}

that works fine if the Server in question has the relevent WMI namespace. Some of my older Servers to not and therefore the following exception is thrown

Get-WmiObject : Invalid namespace
At line:1 char:14
+ Get-WmiObject <<<<  -ComputerName Server1 -Class Win32_PowerPlan -Namespace "root\cimv2\power"  -ErrorAction stop | where {$_.IsActive
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

OK, so if I do

try {
Get-WmiObject -ComputerName Server1 -Class Win32_PowerPlan -Namespace "root\cimv2\power"  -ErrorAction stop | where {$_.IsActive}
}

catch [exception] {"there was an exception"}

that works OK too, however I want to catch a more specifc and therfore relevent exception from the above error, I have tried

catch [Microsoft.PowerShell.Commands.GetWmiObjectCommand]
catch [GetWMIManagementException]
catch [ManagementException]

no Cigar any advise/suggests please

So basically I want to catch the 'Invalid namespace' exception, but I am not sure of the correct format for the catch [xxxxxx] statement

Thanks All

Ernie

 


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>