从显示名称获取 Samaccountname 到 .csv [英] Get Samaccountname from display name into .csv

查看:27
本文介绍了从显示名称获取 Samaccountname 到 .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

mklement0

Read this helpful answer on the issue of Get-AdUserand -filter from mklement0

这篇关于从显示名称获取 Samaccountname 到 .csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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