简体   繁体   English

比较AD用户中的2个属性

[英]Comparing 2 attributes within AD users

Can someone give me some advice with comparing 2 attributes within an AD user? 有人可以给我一些比较AD用户中2个属性的建议吗? I want to scan the user accounts in an OU, and compare 2 attributes to see if they don't match. 我想扫描一个OU中的用户帐户,并比较2个属性以查看它们是否不匹配。

I started off with the following expression to see all the details: 我首先使用以下表达式查看所有详细信息:

Get-ADUser -SearchBase "OU=users,OU=company,DC=blah,DC=blah,DC=com" -Filter * -Properties * |
    Format-Table name, l, physicaldeliveryofficename

I need to compare the office and city and export the resulting accounts that don't match to a csv. 我需要比较办公室和城市,并将不匹配的结果帐户导出到csv。

Do I have to import the list, or can I use the results from the get-ADUser expression? 我是否必须导入列表,或者可以使用get-ADUser表达式中的结果?

How about two simple loops, (assuming name is unique) something like this: 两个简单的循环(假设名称是唯一的)是这样的:

$file = Import-CSV C:\example.csv
$Users = get-ADUser -searchbase "OU=users,OU=company,DC=blah,DC=blah,DC=com" -filter * -properties name, physicaldeliveryofficename | Select name, physicaldeliveryofficename
foreach ($user in $users) {
    foreach {$entry in $file) {
        if ($user.name -eq $entry.name) {
            if ($user.physicaldeliveryofficename -eq $entry.physicaldeliveryofficename) {
                #Both match
            } else {
                #Does not match
            }
        }
    }
}

Ended up with this, which works well. 最终结束了,效果很好。

##   Returns all AD users where the city attribute and Office attribute don't match
#Creates an array for output
$Outarray = @()
$Users = get-ADUser -searchbase "OU=blah,OU=blah,DC=blah,DC=blah,DC=blah" - filter * -properties * | Select samaccountname, name, l, physicaldeliveryofficename
#Writes name, username, office and city to the array where city and office don't match
foreach ($user in $users) {
   if ($user.physicaldeliveryofficename -eq $user.l) {
            #They Match 
        } else {
            $outarray += $user.name + ","+ $user.samaccountname + "," + $user.physicaldeliveryofficename + "," + $user.l
        } 
    }
#creates a CSV file, delimited with a comma
#change path to your location
$outarray |sort-object| out-file "c:\temp\output.csv"

Filter the user objects by inequality of the two properties in question, then export them to a CSV: 通过有问题的两个属性的不相等过滤用户对象,然后将它们导出为CSV:

$ou = 'OU=users,OU=company,DC=example,DC=com'
Get-ADUser -SearchBase $ou -Filter * -Properties * |
    Where-Object { $_.l -ne $_.physicaldeliveryofficename } |
    Select-Object SamAccountName, Name, l, physicaldeliveryofficename |
    Export-Csv 'C:\path\to\output.csv' -NoType

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

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