Page 1 of 1

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

Posted: Mon Feb 17, 2020 5:30 pm
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.

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

Posted: Mon Feb 17, 2020 6:09 pm
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.

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

Posted: Mon Feb 17, 2020 6:42 pm
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.

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

Posted: Mon Feb 17, 2020 8:47 pm
by jvierra
The @ is only for splats. Remove it.

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

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

Thanks Mr. Vierra.