New-CIMInstance dosent trigger try catch

0

why did the catch block

$ComputerName = "server"

try {
    #Hostname Abfragen der fürs CMI Query passt 
    $OptDCOM = New-CimSessionOption -Protocol Dcom
    $CimSessionDCOM = New-CimSession -ComputerName $ComputerName -SessionOption $OptDCOM
}
catch{
    Write-Host "Fehler beim CIM-Instance mit DCOM: $ComputerName"
    $array += $_.Exception.Message
    $BS = "PSR"
}
$BS 

doesnt trigger ? got the following error:

New-CimSession : Der RPC-Server ist nicht verfügbar. 
In Zeile:6 Zeichen:23
+ ... ssionDCOM = New-CimSession -ComputerName $ComputerName -SessionOption ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-CimSession], CimException
    + FullyQualifiedErrorId : HRESULT 0x800706ba,Microsoft.Management.Infrastructure.CimCmdlets.NewCimSessionCommand
    + PSComputerName        :

But nothing in the catch-Block happen, i need to change to PS-Remoting when this above failed.

Thanks

powershell
try-catch
asked on Stack Overflow Sep 15, 2017 by Steakschen

1 Answer

2

Add -ErrorAction Stop

    $CimSessionDCOM = New-CimSession -ComputerName $ComputerName -SessionOption $OptDCOM -ErrorAction Stop
answered on Stack Overflow Sep 15, 2017 by Vincent K

User contributions licensed under CC BY-SA 3.0