How do I catch an Access denied error?

Hi everyone. I have a very simple script that invokes a session to a list of servers. Under the TRY block, it gets a service from services.msc and returns the status. Now I am trying to CATCH the error if the credentials I used was denied, I used this code under the CATCH block but the string is not being written out when I run the code. CATCH [System.UnauthorizedAccessException] {Write-Output “Access Denied”} How do I get the “Access Denied” string as an output instead of the generic error (in red) it is throwing? Thank you!

submitted by /u/stilinski03
[link] [comments]

Leave a Reply