Need some help in Try Catch error handling inside calculated properties?
Posted: Mon Feb 17, 2020 4:30 pm
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:
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?
This is the error code:
Thank you in advance.
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'
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
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