Help with try/catch and UDInput

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.

Here is a screengrab of the catch block that is not working.

Here is a sample of code.

Any help would be appreciated. At this point I’m kind of lost.


I think that this may be this issue. I’ll look into it for the next release:

Bummer. :cry: Looks like I’ll have to come up with an alternative method. Any idea what the time frame is looking like before we receive the new version?

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.

Try this:

Line 18 ->

$UserObjects | ForEach-Object {
$CurrentUser = $_

Line 34 ->

          catch {
                $F ++
                $FProps = @{
                    FirstName = $CurrentUser.FirstName
                    LastName = $CurrentUser.LastName
                    MiddleInitial = $CurrentUser.MiddleInitial
                    EmployeeID = $CurrentUser.EmployeeID
                    RequestingUser = $user
                    RequestTimeStamp = $Time
                    UserName = $CurrentUser.UserName
                    Created = 'false'
                }#end CProps

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!!