Home > OS >  Powershell to create list of duplicated proxyaddresses as .CSV file?
Powershell to create list of duplicated proxyaddresses as .CSV file?

Time:07-05

I need some way to report which users in our AD are having duplicated ProxyAddresses or aliases.

Get-ADUser -filter * -properties proxyaddresses |
Select-Object Name,
              @{ L = "proxyAddresses"; E = { ($_.ProxyAddresses -like 'smtp:*') -join ";" } } |
              export-csv -Path C:\proxyaddresses.csv -NoTypeInformation

I need only the duplicated AD user, not the whole lot, how can I get that report to . CSV file?

CodePudding user response:

You need to wait before concatening your proxy addresses until you are done working with them.

You can get the duplicates by comparing the count of proxy addresses with the count of unique proxy addresses (Select-Object -Unique). If the count mismatch, then you have some dupe in there. If it is the same, then no duplicates.

Here is an example:

$Users = Get-ADUser -filter * -properties proxyaddresses |
    Select-Object Name,
    @{ L = "proxyAddresses"; E = { $_.ProxyAddresses -like 'smtp:*' } } 
    
$Dupes = $Users | Where-Object -FilterScript { $_.proxyaddresses.Count -ne ($_.ProxyAddresses | Select-Object -Unique).Count }
$Dupes | Select Name, @{'Name' = 'ProxyAddresses' ; 'Expression' = { $_.proxyAddresses -join ';' } }  |  export-csv -Path C:\proxyaddresses.csv -NoTypeInformation

Reference dataset used

$Users = @(
    [PSCustomObject]@{Name = 'Value'; proxyaddresses = @('SMTP:[email protected]', 'SMTP:[email protected]' ) }
    [PSCustomObject]@{Name = 'Bob Value'; proxyaddresses = @('SMTP:[email protected]', '[email protected]') }
)

CodePudding user response:

Not sure if you want:

  • Users that have a duplicated address in their proxy list (see answer @SagePourpre), or
  • All users that have the same proxy addresses in their list as another user (this answer)

Create an index (hashtable) where each proxy address refers to a list of users that own that specific proxy address:

$ADUserByProxy = @{}
Get-ADUser -filter * -properties proxyaddresses |
ForEach-Object {
    ForEach ($Proxy in $_.ProxyAddresses) {
        if (!$ADUserByProxy.Contains($Proxy)) {
            $ADUserByProxy[$Proxy] = [Collections.Generic.List[Object]]::new()
        }
        $ADUserByProxy[$Proxy].Add($_)
    }
}

Than list all the values that contain more then 1 user:

$ADUserByProxy.GetEnumerator() |
Where-Object { $_.Value.Count -gt 1 } |
ForEach-Object { $_.Value } |
Export-csv -Path C:\proxyaddresses.csv -NoTypeInformation

CodePudding user response:

Perhaps not the fastest method, but here's an alternative:

Get-ADUser -Filter * -Properties proxyaddresses | Foreach-Object { 
    $unique = $_.ProxyAddresses | Select-Object -Unique
    $dupes  = Compare-object -ReferenceObject $unique -DifferenceObject $_.ProxyAddresses -PassThru
    if (@($dupes).Count) {
        $_ | Select-Object Name, @{Name = 'DuplicateAddresses'; Expression = {$dupes -join ';'}}
    }
} | Export-Csv -Path 'C:\proxyaddresses.csv' -NoTypeInformation
  • Related