2016-09-29 49 views
0

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

我開始用下面的表達來看到所有的細節:

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

我需要比較局,市和導出不匹配到CSV產生的賬戶。

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

+0

比較的是什麼?不匹配什麼? –

+0

我想比較AD用戶記錄的城市和辦公室屬性並查看它們是否匹配(即丹佛和丹佛總部不匹配) –

回答

0

結束了這一點,這效果很好。

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

大約兩個簡單的循環,(假設名字是唯一的)這樣的方法:

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

通過篩選問題的兩個屬性的不平等的用戶對象,然後將其導出爲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