PowerShell: Try Catch Technique to Obtain Error Type

# Test

try{
Read-SCVirtualMachine $vmName -EA Stop
}catch{
$errorMessage=$error[0].Exception.GetType().FullName
write-host $errorMessage
}

# Get error type

Microsoft.VirtualManager.Utils.CarmineException

# Retry catch with specific error type

try{
Read-SCVirtualMachine $vmName -EA Stop
}catch [Microsoft.VirtualManager.Utils.CarmineException]{
$errorMessage=$_
$smbPath=[regex]::match($errorMessage,'\\\\(.*)\\').Value
if($smbPath){
write-host "Add this SMB/CIFS path the cluster: $smbPath"
}else{
write-host $errorMessage
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *