Import list of users - Export List of users and Groups

依然范特西╮ 提交于 2021-01-27 17:11:31

问题


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

File example:

username
Jon Jo
Steve Price
Alan Partridge

Cheers.


回答1:


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

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.

The resulting CSV file would look something like this:

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



回答2:


You could use the memberof property and then join the array of groups inside a calculated property.

Import-Csv C:\Users.csv | 
    ForEach-Object{
        Get-AdUser -Filter "displayname -eq '$($_.username)'" -Properties memberof
    } |
    Select-Object samaccountname, name, @{n='memberof';e={$_.memberof -join ';'}} |
    Export-Csv -Path C:\UserPermiss.csv -NoTypeInformation


来源:https://stackoverflow.com/questions/50882966/import-list-of-users-export-list-of-users-and-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!