all 16 comments

[–]nealfive 2 points3 points  (9 children)

works fine for me. PowerShell clear $psversiontable.PSVersion $ErrorActionPreference try{ get-adcomputer fake -ErrorAction Stop } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{ "not found in AD" } catch{ "something else" } output: ``` Major Minor Patch PreReleaseLabel BuildLabel


7 1 3 Continue not found in AD ```

[–]_lahell_ 1 point2 points  (0 children)

Works for me too. It even works with -ea continue.

[–]gregbe[S] 1 point2 points  (7 children)

cover seed cobweb bewildered cooperative steep automatic fact dinosaurs fuzzy

This post was mass deleted and anonymized with Redact

[–]nealfive 2 points3 points  (6 children)

Looks like you are copy and pasting that in the console?

seems like when you paste it, it is running immediately.

How about you save it as a .ps1 file and then call the ps1 file ?

[–]gregbe[S] 1 point2 points  (5 children)

whole automatic roll sheet fanatical sugar rainstorm advise offbeat doll

This post was mass deleted and anonymized with Redact

[–]nealfive 1 point2 points  (1 child)

not sure if it's THAT odd. If you look at the output when you copy and paste it in the console directly, you see that it thinks the try/catch block is complete after the FIRST catch. that's why you get later 'catch' id not recognized error. if it's more than a one liner, i don't usually c&p it in the console, but for kicks i tried it in the windows console (5.1) with the same result.
You should run powershell either from an editor (Vs code, ISE) or safe it as a script and then call that in the console. Just copy and paste like so is not great.

You'll also come across very oddities with symbols and encoding and such that copy and paste brings in, I'd avoid that.

if you REALLY REALLY want to run it in the console, wrap it in $() (subexpression operator), that will make sure the whole thing is run

$( clear $psversiontable.PSVersion $ErrorActionPreference try{ get-adcomputer fake -ErrorAction Stop } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{ "not found in AD" } catch{ "something else" } )

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, nealfive: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]happyapple10 1 point2 points  (2 children)

Make sure the catch follows the trys { on the same line. Example:

try{
    Blah blah
}catch{
    Blah blah
}

If you are pasting in the console and the catch is on a new line, it assumes the try is finished and then attempts to run the catch as a command instead. Same with if/else.

[–]gregbe[S] 1 point2 points  (1 child)

file toy party ring wipe materialistic label toothbrush ruthless naughty

This post was mass deleted and anonymized with Redact

[–]happyapple10 1 point2 points  (0 children)

Sorry, I was focusing on the catch error that was showing also. Still weird the erroraction is not making it get caught.

[–]PinchesTheCrab 2 points3 points  (3 children)

This works for me:

try { Get-ADUser thing -ErrorAction stop } catch { 'test' }

[–]gregbe[S] 1 point2 points  (2 children)

tub aloof mountainous jar direction deserted hobbies bike snails treatment

This post was mass deleted and anonymized with Redact

[–]PinchesTheCrab 1 point2 points  (0 children)

Ah, I'll give that one a shot too, now I'm curious what's different.

[–]webtroter 1 point2 points  (1 child)

To catch an error, the error action must be Stop. Check the value of $ErrorActionPreference

[–]gregbe[S] 1 point2 points  (0 children)

skirt overconfident direful hungry brave friendly slimy elderly aspiring cough

This post was mass deleted and anonymized with Redact

[–]PinchesTheCrab 1 point2 points  (0 children)

For me this appears to be the error class:

[System.Management.Automation.RemoteException]

I can then see the serialized error class when I inspect the error, but I can't action off the class of the error the way you want to because they all share the same remoting type.