[英]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.