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: Remove Nic Teaming

# Check NIC TeamingPS C:\Windows\system32> get-NetLbfoTeamName : ISCSIMembers : {iSCSI-B, iSCSI-A}TeamNics : ISCSITeamingMode : SwitchIndependentLoadBalancingAlgorithm…

PowerShell: Fix All VMs CPU Compatibility Setting

# fixAllVmCpuCompatibility.ps1 # version 0.01 function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu'…

PowerShell: Script to Stop, Start, Disable, and Enable Exchange Server

<#This script contains a set of functions to administer Exchange 2010, 2013, 2016, and 3000.…