I want to print custom error message in case of error
Foreach ($server in $servers){ Try{ $admins = Gwmi win32_groupuser –computer $server $admins = $admins |? {$_.groupcomponent –like '*"Administrators"'} $admins = $admins |% { $_.partcomponent –match “.+Domain\=(.+)\,Name\=(.+)$” > $nul $matches[1].trim('"') + “\” + $matches[2].trim('"') | Where-Object {$_ -like "$domain*"}} } catch [System.Runtime.InteropServices.COMException] { # You can inspect the error code to see what specific error we're dealing with if($_.Exception.ErrorCode -like "*0x800706BA*") { # This is instead of the "RPC Server Unavailable" error Write-Verbose -Message ('{0} is unreachable' -f $computer) -Verbose } else { Write-Warning $Error[0] } } # If any other type of Exception is thrown, execute this block catch [System.Exception] { Write-Error -Message "Some other exception that's nothing like the above examples" } } But i'm getting default error
Gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) At line:4 char:18 $admins = Gwmi win32_groupuser –computer $server
CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand I want to print only names of servers where WMI command fails
I tried this solution but no help
Tried also:
catch [Exception] { if ($_.Exception.GetType().Name -like "*COMException*") { Write-Verbose -Message ('{0} is unreachable' -f $computer) -Verbose } }