Import
Import-CSV和Foreach使用Get-ADUser(Import-CSV and Foreach to use Get-ADUser)我有一个CSV文件,如下所示:
name fname1lname1@companyemail.com fname2lname2@companyemail.com ...我想遍历每个电子邮件地址并查询该地址的AD以获取用户对象ID。 我已经能够使用脚本执行此操作,但我希望能够使用一行来完成此操作。
这是我到目前为止所做的:
import-csv -path .\csv_file.csv | foreach-object { get-aduser -filter { proxyaddresses -like "*$_.name*} | select name } | out-file .\results.csv这显然不起作用,我知道它与我在foreach循环中处理$ _对象的方式有关。
我希望输出看起来像:
fname1lname1@companyemail.com,userID1 fname2lname2@companyemail.com,userID2...I have a CSV file that looks like this:
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. 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.csvThis obviously doesn't work and I know it has something to do with how I am handling my $_ object in the foreach loop.
I'm hoping for the output to look something like:
fname1lname1@companyemail.com,userID1 fname2lname2@companyemail.com,userID2... 最满意答案您正在过滤属性proxyaddresses但这不是Get-AdUser返回的默认属性集的一部分。 此外,您的代码有错误"可能是复制粘贴错误。
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有时会很棘手,因为它正在寻找字符串输入。 将整个事物包装在引号中并使用子表达式来确保变量$_.Name正确扩展并且具有围绕它的星号。
由于您还在寻找emailaddress我们也将其添加到属性列表中。 我假设第二列是samaccountname 。
我们还使用Export-CSV因为这将产生漂亮的CSV输出。
You are filtering on the property proxyaddresses however that is not part of the default property set that Get-AdUser returns. 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. 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.
Since you are also looking for emailaddress we add that to the properties list as well. I will assume the second column is for samaccountname.
We also use Export-CSV since that will make for nice CSV output.