简体   繁体   English

导入CSV和Foreach以使用Get-ADUser

[英]Import-CSV and Foreach to use Get-ADUser

I have a CSV file that looks like this: 我有一个CSV文件,看起来像这样:

name    
fname1lname1@companyemail.com  
fname2lname2@companyemail.com  
...

I would like to loop through each email address and query AD for that address to grab the user objects ID. 我想遍历每个电子邮件地址,并向AD查询该地址以获取用户对象ID。 I have been able to do this using a script, but I would like to be able to do it using just one line. 我已经可以使用脚本来做到这一点,但我希望仅使用一行就可以做到这一点。

This is what I've done so far: 到目前为止,这是我所做的:

import-csv -path .\csv_file.csv | foreach-object { get-aduser -filter { proxyaddresses -like "*$_.name*} | select name } | out-file .\results.csv

This obviously doesn't work and I know it has something to do with how I am handling my $_ object in the foreach loop. 这显然是行不通的,我知道这与我如何在foreach循环中处理$ _对象有关。

I'm hoping for the output to look something like: 我希望输出看起来像:

fname1lname1@companyemail.com,userID1  
fname2lname2@companyemail.com,userID2
...

You are filtering on the property proxyaddresses however that is not part of the default property set that Get-AdUser returns. 您正在过滤属性proxyaddresses但这不是Get-AdUser返回的默认属性集的一部分。 Also your code had a errant " which might have been a copy paste error. 另外,您的代码有错误" ,这可能是复制粘贴错误。

Import-CSV -Path .\csv_file.csv | ForEach-Object { 
    Get-ADUser -Filter "ProxyAddresses -like '*$($_.name)*'"  -Properties ProxyAddresses,EmailAddress | select EmailAddress,SamAccountName 
} | Export-CSV .\results.csv -NoTypeInformation

-Filter can be tricky sometimes as it is looking for string input. -Filter 寻找字符串输入时有时会很棘手 Wrap the whole thing in quotes and use a sub expression to ensure that the variable $_.Name is expanded properly and has is asterisks surrounding it. 将整个内容括在引号中,并使用子表达式来确保变量$_.Name正确展开,并在其周围带有星号。

Since you are also looking for emailaddress we add that to the properties list as well. 由于您也在寻找emailaddress我们也将其添加到属性列表中。 I will assume the second column is for samaccountname . 我将假设第二列是samaccountname

We also use Export-CSV since that will make for nice CSV output. 我们还使用Export-CSV因为这样可以实现出色的CSV输出。

If you're using Exchange this can be much simpler and faster if you use the Exchange cmdlets: 如果您使用的是Exchange,则使用Exchange cmdlet可以更加简单,快捷:

Import-CSV -Path .\csv_file.csv | ForEach-Object { 
Get-Recipient $_ |
Select PrimarySMTPAddress,SamAccountName 
} | Export-CSV .\results.csv -NoTypeInformation

Exchange requires all email address to be unique, and maintains it's own internal database that uses email address as a primary index so it can return the DN and SamAccountName that goes with that email address almost immediately. Exchange要求所有电子邮件地址都是唯一的,并维护它自己的内部数据库,该数据库使用电子邮件地址作为主要索引,因此它几乎可以立即返回与该电子邮件地址一起使用的DN和SamAccountName。

AD doesn't require them to be unique, so it doesn't index on that attribute and it has to search every user object looking for the one that has that email address in it's proxy address collection. AD不需要它们唯一,因此它不会在该属性上建立索引,它必须搜索每个用户对象,以在其代理地址集合中查找具有该电子邮件地址的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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