从显示名称获取 Samaccountname 到 .csv [英] Get Samaccountname from display name into .csv
问题描述
我有一个带有 displayName 的 .csv 文件,然后是用户的显示名称.
I have a .csv file with displayName and then the display names of the users.
我正在运行以下代码,但返回的 .csv 文件为空白 0KB.我在这上面花了几个小时,但我不知道我做错了什么.
I am running the following code, but the returned .csv file is blank 0KB. I've spent hours on this and I can't figure out what I'm doing wrong.
(我什至尝试将 displayName 切换为DisplayName",但这不起作用)
(I've even tried switching the displayName to "DisplayName" but that doesn't work)
Get-Content C:\Scripts\displaynames.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$user'" -Properties Name, SamAccountName, City, co, DistinguishedName |
Select Name,SamAccountName, City, co, DistinguishedName
} | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation
我只需要简单返回 displayName = samaccountname
推荐答案
如果您的文件实际上是一个 Csv 文件,每列都有一个标题,
并且 displayname
是其中之一 - 然后使用 Import-Csv
获取数据.
If your file is in fact a Csv file with a header for each column,
and displayname
is one of them - then use Import-Csv
to get the data.
ForEach-Object
使用变量 $_
或 $PSItem
来分配当前迭代的数据行,并将列作为属性.
ForEach-Object
uses the variable $_
or alternatively $PSItem
to assign the currently iterated row of data with the columns as properties.
所以改为:
Import-Csv C:\Scripts\displaynames.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$($_.DisplayName)'" -Properties Name, SamAccountName, City, co, DistinguishedName |
Select Name,SamAccountName, City, co, DistinguishedName
} | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation
Read this helpful answer on the issue of Get-AdUser
and -filter
from mklement0
这篇关于从显示名称获取 Samaccountname 到 .csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!