vendredi 18 août 2017

PowerShell Try/Catch with If Statements

Problem/Details

I am working in PowerShell and trying to figure out how custom Try Catch statements work. My current major issue involves mixing Try/Catch and If statements. So the idea of what I am trying to achieve is something like this:

try {
    if (!$someVariable.Text) { throw new exception 0 }
    elseif ($someVariable.Text -lt 11) { throw new exception 1 }
    elseif (!($someVariable.Text -match '[a-zA-Z\s]')) { throw new exception 2}
}
catch 0 {
    [System.Windows.Forms.MessageBox]::Show("Custom Error Message 1")
}
catch 1 {
    [System.Windows.Forms.MessageBox]::Show("Custom Error Message 2")
}
catch 2 {
    [System.Windows.Forms.MessageBox]::Show("Custom Error Message 3")
}

Now I know the above code is very inaccurate in terms of what the actual code will be, but I wanted to visually display what I'm thinking and trying to achieve.

Question

Does anyone know how to create custom error messages with PowerShell that could assist me with achieving something close to the above idea?

So far, the link below is the closest thing I have found to what I'm trying to achieve:

PowerShell Try, Catch, custom terminating error message

Aucun commentaire:

Enregistrer un commentaire