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: Windows Automated Disk Cleanup

##################################################################################     <#      This script is created to automate the cleanup activity. Doing so will benefit to reduce the size of disk.     This script will perform the following   1. Clear windows temp and user temp folder   2. Empty recycle bin   3. Disk Cleanup   4. Clear CBS cabinet log files   5. Clear downloaded patches   6. Clear downloaded driver   7. Clean download folder      Note:  …

PowerShell: Set Virtual Machine Default Paths on Hyper-V Host of a Cluster

$newVirtualMachinePath='D:\VirtualMachines' $newVirtualHardDiskPath='D:\VirtualMachines' 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' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…

PowerShell: Find Process IDs of a Locked File and Kill It

There's a newer iteration of this function here. $targetFile='C:\Users\kimconnect\Desktop\testfile.csv' function killPidLockedFile($filename){ if (!(Get-Command handle.exe -ErrorAction…