将Powershell管道变量转换为get-user命令

时间:2015-04-07 20:02:21

标签: powershell powershell-v3.0

我正在尝试将一组电子邮件地址通过powershell

传递给get-user命令
$email = get-content -path "c:\temp\file.csv" get-user -indentity $email | select-object userprincipalname,department,phone,name | format-table | out-file c:\temp\file.txt

1 个答案:

答案 0 :(得分:0)

欢迎来到SO。

让我们看......从哪里开始

  • 电子邮件地址不是Identity
  • 认可的值之一
  • 拼写为-identity
  • 不要将Format-table用于对象输出。
  • Department不是返回的默认值之一。
  • 没有名为phone
  • 的AD属性
  • Get-Aduser而非get-user
  • 不知道这只是一个复制粘贴事故,但你有多行作为一个。
  • -Identity需要一个值。不是一系列名字。

知道我们可以看看我们是否可以尝试你想要做的事情。假设您的文件“c:\ temp \ file.csv”仅包含没有标题的地址(因为这就是您对它的处理方式。)

Get-Content c:\temp\file.csv | ForEach-Object{
    Get-ADUser -Filter "emailaddress -eq '$_'" -Properties department,OfficePhone
} | Select-Object UserPrincipalName,Department,OfficePhone,Name | Export-CSV C:\temp\outputfile.csv -NoTypeInformation

此处没有错误更正,因此您可能需要查看-ErrorAction try / catch组合。我鼓励你自己看一下。

相关问题