导入用户列表-导出用户和组列表 [英] Import list of users - Export List of users and Groups

查看:71
本文介绍了导入用户列表-导出用户和组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以导入用户列表,在AD中获取他们的组并导出列表?

Is there any way I can import a list of users, get their groups in AD and export the list?

Import-Csv C:\Users.csv |
  % {Get-AdUser -filter "displayname -eq '$($_.username)'"} |
    Get-ADprincipalGroupMembership |
      Select samaccountname, name |
        Export-csv -path C:\UserPermiss.csv -NoTypeInformation

文件示例:

username
Jon Jo
Steve Price
Alan Partridge

干杯.

推荐答案

在PSv4 +中,您可以利用 -PipelineVariable / -pv 通用参数来生成早期管道阶段可用于后期管道阶段中使用的脚本块:

In PSv4+ you can leverage the -PipelineVariable / -pv common parameter to make the output of an earlier pipeline stage available to script blocks used in later pipeline stages:

Import-Csv C:\Users.csv |
  ForEach-Object -pv user { Get-AdUser -filter "displayname -eq '$($_.username)'"} |
    Get-ADprincipalGroupMembership |
      Select-Object @{ n = 'samaccountname'; e = { $user.samaccountname } }, name |
        Export-csv -path C:\UserPermiss.csv -NoTypeInformation

使用 -pv用户可使 Get-AdUser 的AD用户输出在 Select-Object中作为变量 $ user 提供阶段,在计算的属性 samaccountname 的脚本块中对其进行引用,将手边的AD用户的名称与他们所属的每个AD组的名称配对.

Using -pv user makes the AD user output by Get-AdUser available as variable $user in the Select-Object stage, where it is referenced in the script block of calculated property samaccountname, pairing the name of the AD user at hand with the name of each AD group they are a member of.

生成的CSV文件如下所示:

The resulting CSV file would look something like this:

"samaccountname","name"
"jjo","group1"
"jjo","group2"
"sprice","group1"
"sprice","group3"
"sprice","group4"
# ...

这篇关于导入用户列表-导出用户和组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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