简体   繁体   English

Powershell 将重复代理地址列表创建为 .CSV 文件?

[英]Powershell to create list of duplicated proxyaddresses as .CSV file?

I need some way to report which users in our AD are having duplicated ProxyAddresses or aliases.我需要一些方法来报告我们 AD 中的哪些用户有重复的 ProxyAddresses 或别名。

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 .我只需要重复的 AD 用户,而不是全部,我怎样才能得到那个报告到 . CSV file? CSV 文件?

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 ).您可以通过将代理地址的计数与唯一代理地址的计数( 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:a@a.com', 'SMTP:a@a.com' ) }
    [PSCustomObject]@{Name = 'Bob Value'; proxyaddresses = @('SMTP:a@a.com', 'b@a.com') }
)

Not sure if you want:不确定你是否想要:

  • Users that have a duplicated address in their proxy list (see answer @SagePourpre ), or代理列表中有重复地址的用户(请参阅答案@SagePourpre ),或
  • 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:比列出包含超过 1 个用户的所有值:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM