簡體   English   中英

比較AD用戶中的2個屬性

[英]Comparing 2 attributes within AD users

有人可以給我一些比較AD用戶中2個屬性的建議嗎? 我想掃描一個OU中的用戶帳戶,並比較2個屬性以查看它們是否不匹配。

我首先使用以下表達式查看所有詳細信息:

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

我需要比較辦公室和城市,並將不匹配的結果帳戶導出到csv。

我是否必須導入列表,或者可以使用get-ADUser表達式中的結果?

兩個簡單的循環(假設名稱是唯一的)是這樣的:

$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
            }
        }
    }
}

最終結束了,效果很好。

##   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"

通過有問題的兩個屬性的不相等過濾用戶對象,然后將它們導出為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