Monday, November 29, 2021

PowerShell Exception Handling Code




Here is PS Exception handling code :


Try 

 {


}

Catch


    $ex = $_.Exception

    $line = $_.InvocationInfo.ScriptLineNumber

    $msg = $ex.Message 

    Write-Host -ForegroundColor Red "caught exception: $ex at $line"

    Write-Host -ForegroundColor Red "An exception occurred."

    Write-Host -ForegroundColor Red '$_ = ' $_

    Write-Host -ForegroundColor Red '$_.GetType().FullName = ' $_.GetType().FullName

    Write-Host -ForegroundColor Red '$_.Exception = ' $_.Exception

    Write-Host -ForegroundColor Red '$_.Exception.GetType().FullName = '         $_.Exception.GetType().FullName

    Write-Host -ForegroundColor Red '$_.Exception.Message = ' $_.Exception.Message

}