Need some help in Try Catch error handling inside calculated properties?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Need some help in Try Catch error handling inside calculated properties?

Post by ITEngineer »

Hi Folks,

I need some help in creating the Try Catch Block inside the Calculated properties.

The below code to show the duplicated AD attributes working fine:

Code: Select all

Get-ADUser -Filter { (Enabled -eq $True) -and (extensionAttribute1 -like "*") -and (extensionAttribute2 -like "*") } -SearchBase "DC=domain,DC=com" -Properties CanonicalName, extensionAttribute1, extensionAttribute2, WhenCreated |
    Group-Object -Property extensionAttribute1, extensionAttribute2 |
        Where-Object { $_.Count -gt 1 } |
            ForEach-Object { $_.Group } |
                Select-Object -Property @{N = "User Account"; E = { $_.samAccountName } },
                @{N = "Att 1"; E = { $_.extensionAttribute1 } },
                @{N = "Att 2"; E = { $_.extensionAttribute2 } },
                @{N = 'CN';          E = {Split-Path $_.CanonicalName -Parent}}, WhenCreated | Sort-Object WhenCreated -Descending |
                Export-Csv -NoTypeInformation -Path 'C:\Result\DuplicateAttr.csv'
However, when I use the coding best practice to make it neat and showing the Department attributes which sometimes filled in and sometimes empty, it is throwing the error?

Code: Select all

$BaseOU = "DC=domain,DC=com"
$Properties = @(
    'CanonicalName'
    'Department'
    'extensionAttribute1'
    'extensionAttribute2'
    'WhenCreated'
)
$Filter = '(Enabled -eq $True) -and (extensionAttribute1 -like "*") -and (extensionAttribute2 -like "*")'
$ResultCSV = 'C:\Result\DuplicateAttr.csv'

Get-ADUser -Filter $Filter -SearchBase $BaseOU -SearchScope Subtree -Properties @Properties |
    Group-Object -Property extensionAttribute1, extensionAttribute2 |
        Where-Object { $_.Count -gt 1 } |
            ForEach-Object { $_.Group } |
                Select-Object -Property @{N = "User Account"; E = { $_.samAccountName } },
                @{N = "Department"; E = { $_.department } },
                @{N = "Att 1"; E = { $_.extensionAttribute1 } },
                @{N = "Att 2"; E = { $_.extensionAttribute2 } },
                @{N = "OU Location"; E = {Split-Path $_.CanonicalName -Parent}}, WhenCreated | Sort-Object WhenCreated -Descending | 
                Export-Csv -NoTypeInformation -Path $ResultCSV

Invoke-Item $ResultCSV
This is the error code:

Code: Select all

Get-ADUser : A positional parameter cannot be found that accepts argument 'Department'.
At line:13 char:1
+ Get-ADUser -Filter $Filter -SearchBase $BaseOU -Properties @Propertie ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Thank you in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Need some help in Try Catch error handling inside calculated properties?

Post by jvierra »

"$properties" is NOT a splat. It is a variable and needs to be used as a variable. You are thinking that it is a "splatted" parameter.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Need some help in Try Catch error handling inside calculated properties?

Post by ITEngineer »

Yes, you are right, so how to fix that issue?

If the Department has some values in it, then it will not returns error.
But if it is empty, just skip it and move on to the next Attributes / properties.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Need some help in Try Catch error handling inside calculated properties?

Post by jvierra »

The @ is only for splats. Remove it.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Need some help in Try Catch error handling inside calculated properties?

Post by ITEngineer »

jvierra wrote: Mon Feb 17, 2020 7:47 pm The @ is only for splats. Remove it.
LOL, yes, that was great finding :-)
:D

Thanks Mr. Vierra.
/* IT Engineer */
This topic is 4 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked