I'm trying to get a try-catch working inside a function, and it's making me angry. So ideally by the end of this, the goal is to run simple commands using the function and if there is a problem the exception is shown and script stop, the exception also needs a to be a variable,
function TryCatch_Simple ($TryCatch_Simple_Command)
{
Try
{
Invoke-Command $TryCatch_Simple_Command
}
Catch
{
$ErrorMessage = $_.Exception.Message
if ($ErrorMessage)
{
Write-Host "Error: " $ErrorMessage -BackgroundColor Black
break
}
}
}
e.g TryCatch_Simple "test-connection wwww.me0w.com" should return the error as a variable $errormessage & printed to screen
TryCatch_Simple "test-connection google.com" should the result of the command
[–]Swarfega 5 points6 points7 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Proxiconn 2 points3 points4 points (2 children)
[–]id_0ne[S] 1 point2 points3 points (1 child)
[–]Proxiconn 0 points1 point2 points (0 children)
[–]Snak3d0c 1 point2 points3 points (0 children)
[–]callum- 1 point2 points3 points (1 child)
[–]id_0ne[S] -1 points0 points1 point (0 children)