[英]Get-ADuser Script Issue
I would like to compare a list of email addresses that I have in a CSV file against our Active Directory. 我想将CSV文件中的电子邮件地址列表与Active Directory进行比较。 I am having difficulties with putting all the pieces together. 我很难将所有部分放在一起。
Psuedo-Code: 伪代码:
Code: 码:
Import-Csv users.csv |
Get-ADUser -Filter {mail -eq $_.mail } |
where {$_.enabled -eq $true} |
Select-Object -Property DistinguishedName, samaccountname, mail, enabled
I think I need to add additional information for the SearchBase, but I can't figure out how to add it. 我想我需要为SearchBase添加其他信息,但是我不知道如何添加它。
eg: -SearchBase "DC=na,DC=corp,DC=<company name>,DC=com"
例如: -SearchBase "DC=na,DC=corp,DC=<company name>,DC=com"
In the CSV file is one column. 在CSV文件中是一列。 Column name is mail. 列名是邮件。 AD attribute I am trying to do a join on is mail. 我尝试加入的AD属性是邮件。
You could store the csv import in one variable, and a Get of all the email addresses in AD you have, use Compare-Object
between the lists, then use foreach against the result. 您可以将csv导入存储在一个变量中,并获取AD中所有电子邮件地址的Get,在列表之间使用Compare-Object
,然后对结果使用foreach。
$matchingUsers = New-Object System.Collections.ArrayList
$csv = import-csv users.csv
$emails = $(get-aduser -filter * -Properties mail,Enabled | ? {$_.Enabled -eq $True} | select mail).mail
$comparison = $($(compare-object -ReferenceObject $a -DifferenceObject $b -IncludeEqual)| ? {$_.SideIndicator -eq '=='}).InputObject
foreach($object in $comparison) {
$user = Get-ADUser -Filter {mail -eq $object} -SearchBase "DC=na,DC=corp,DC=,DC=com" -Properties DistinguishedName,samaccountname,mail,enabled
$matchingUsers.Add($user)
}
Then export $matchingUsers
, format to a table, whatever you need after that 然后导出$matchingUsers
,将其格式化为表格,之后您需要什么
To streamline or shorten trebleCode's suggestion a little bit you could start with something like this: 为了简化或缩短 trebleCode的建议,您可以从如下所示开始:
$MailAddressList = Import-Csv -Path users.csv
$AllUsersList = Get-ADUser -Filter * -SearchBase 'DC=na,DC=corp,DC=,DC=com' -Properties DistinguishedName,samaccountname,mail,enabled | Where-Object {$_.enabled}
Compare-Object -ReferenceObject $MailAddressList -DifferenceObject $AllUsersList -Property 'Mail' -PassThru -IncludeEqual -ExcludeDifferent
You can exchange the -ReferenceObject
with the -DifferenceObject
if needed. 如果需要,可以将-ReferenceObject
与-DifferenceObject
交换。 I don't have an Active Directory at the moment to test. 我目前没有要测试的Active Directory。
Get the csv emails as an array of strings ($array). 以字符串数组($ array)形式获取csv电子邮件。 Select -expand is your friend. 选择-expand是您的朋友。
Grab users from AD, pipe to foreach-object, and ask if $array.Contains(the email address property of the piped in object). 从AD中获取用户,通过管道传输到foreach-object,并询问$ array.Contains(通过管道传输的对象的电子邮件地址属性)。
How you process the result of that conditional is up to you; 您如何处理该条件的结果取决于您; many ways to handle it. 处理它的方法很多。 But you need to see a no-nonsense way to get what you're asking, and that's what I provided. 但是您需要一种毫不费力的方式来获取您所要的内容,这就是我所提供的。
I would probably do something like this: 我可能会做这样的事情:
# enter the DistinghuishedName of the OU where the users to check are in
$SearchBase = 'DC=na,DC=corp,DC=,DC=com'
$CsvPath = 'PATH TO THE USERS.CSV FILE'
$UserList = Import-Csv -Path $CsvPath
# for fast lookup, best use a Hashtable
$MailLookup = @{}
foreach ($user in $UserList) {
$MailLookup[$user.mail] = $true # the value is not important here
}
# in fact, the properties DistinguishedName,SamAccountName and Enabled are returned by default
Get-ADUser -Filter * -SearchBase $SearchBase -Properties DistinguishedName,SamAccountName,EmailAddress,Enabled |
Where-Object {$_.Enabled -and $MailLookup.ContainsKey($_.EmailAddress) }
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.