从雇员ID列表中将SamAccountName拉入csv

时间:2017-06-29 13:15:15

标签: powershell active-directory powershell-v3.0

$user = ForEach ($user in $(get-content C:\Users\desktop\employeeids.csv)) {

    Get-ADUser $user -properties employeeid
}

$users |

Select-object Samaccountname |

export-csv -path C:\users\desktop\usernames.csv

我正在尝试拉出我获得的员工ID列表的Samaccountname,并将这些samaccountnames导出到csv中。这是我到目前为止,但我不断收到以下错误

Get-ADUser : Cannot find an object with identity: '12345' under: 'DC=,DC='.
At line:3 char:5
+     Get-ADUser $user -properties employeeid
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (12345:ADUser) [Get-ADUser], ADIdentity 
   NotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Managem 
   ent.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.Get  
  ADUser

1 个答案:

答案 0 :(得分:2)

$users = ForEach ($user in $(get-content C:\Users\desktop\employeeids.txt)) {
Get-ADUser $user -properties employeeid
}
$users |
Select-object Samaccountname |
export-csv -path C:\users\desktop\usernames.csv

我已将原始变量更改为$users而非$user 我还将文件扩展名更改为.txt,但是如果使用.csv扩展程序正常工作,则应该没问题