简体   繁体   中英

Comparing 2 attributes within AD users

Can someone give me some advice with comparing 2 attributes within an AD user? I want to scan the user accounts in an OU, and compare 2 attributes to see if they don't match.

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.

Do I have to import the list, or can I use the results from the get-ADUser expression?

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:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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