So I’m having an issue getting a try/catch block to function correctly and have spent several days trying to correct it, with no luck at this point. So I am hoping someone can lend a hand.
Basically what I have is a “New-UDinput -type File” that takes a CSV and attempts to create users based off of the info in the CSV. I am using custom objects (in both try and catch) to generate information for logging purposes. The output is a New-UDtable inside of a New-UDModal.
If there are no errors the try block works as expected.
The problem I am having is when there is an error and the catch block is run. The custom object does not complete.
Here is a screengrab of the output of the try block.
After looking into this more, that’s not the same issue. His issue is that the input field never returns. Yours is returning but with invalid data. I think your problem is that in your catch you are using the $_ variable. You are expecting it to be the user account but it’s actually the exception object.
Thanks, Adam! I really appreciate you taking the time to look at this and getting me pointed in the right direction. At first I didn’t understand why that would make any difference, so I did a little reading.
For anyone else who may not know or is still trying to learn Powershell. Here is what was going on.
Inside the catch block, there is an automatic variable ( $PSItem or $_ ) of type ErrorRecord that contains the details about the exception. So When I was calling on $_ I was calling the error info. Creating a separate variable provided the expected outcome.
Thanks again Adam for helping me become a better powersheller!!
guy