Posted On July 30, 2021

PowerShell: Try Catch Technique to Obtain Error Type

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

PowerShell: Use EMCOPY to Mirror a Directory

# Purpose: this PowerShell snippet is to demonstrate the use of Emcopy$source="C:\Users\tester\Desktop\Clients"$destination="C:\Users\tester\Desktop\Test"#$switches="/o /secforce /s /de…

PowerShell: Quick EMCopy

# Short Version $arr=@(); $arr+=[PSCustomObject]@{From='D:\Someshare';To='\\NEWSERVER\Someshare'} $arr+=[PSCustomObject]@{From='D:\Test';To='\\NEWSERVER\Test'} # Normal copying # $arr|%{emcopy $_.From $_.To /s /de…

Reset Internet Explorer Settings Script

You can reset Internet Explorer settings to return them to the state they were in when…