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

查看:1280
本文介绍了Import-CSV和Foreach使用Get-ADUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的CSV文件:

I have a CSV file that looks like this:

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

我想循环浏览每封电子邮件地址和查询AD的地址来抓取用户对象ID。我已经能够使用脚本做到这一点,但我想只能使用一行。

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.csv

这显然不起作用,我知道它与在foreach循环中处理我的$ _对象有关。

This obviously doesn't work and I know it has something to do with how I am handling my $_ object in the foreach loop.

我希望输出看起来像:

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


推荐答案

您正在筛选属性 proxyaddresses ,但不属于 Get-AdUser 返回。您的代码还有一个错误的,这可能是一个复制粘贴错误。

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

code> 有时可能很棘手,因为它是寻找字符串输入。使用引号括起整个事物,并使用子表达式来确保变量 $ _。Name 正确扩展,并且它周围有星号。

-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.

由于您也在寻找 emailaddress ,我们也将其添加到属性列表中。我将假设第二列是 samaccountname

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.

我们还使用 Export-CSV ,因为这将使CSV输出更好。

We also use Export-CSV since that will make for nice CSV output.

这篇关于Import-CSV和Foreach使用Get-ADUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆