[英]Powershell Script to get details of user along with group they are member of
我正在尝试导出 AD 用户列表以及他们所属的组,导出的 csv 格式应该类似于
A 组 User1_name
A 组 User2_name
B 组 User3_name
C 组 User4_name
C 组 User5_name
我努力了
$ErrorActionPreference = "SilentlyContinue"
Get-Content R:\PHRBI-R\ADGroup_list.csv | Where{$_ -ne $null -and $_.trim() -ne ""} |
foreach{
$Group = $_
Write-Host "$Group"
Get-ADGroup -Identity $Group -Properties members |
Select-Object -ExpandProperty members |
Get-ADUser -Properties samaccountname, enabled |
Select samaccountname, name, Enabled
}
但它没有提供我正在寻找的模板,并且新组不断添加,因此无法在任何文件中手动列出组并导出所需的详细信息。 如果有人可以提供帮助,我对 Powershell 完全陌生。
下面应该让你去,我试图让它合乎逻辑地遵循。 当然,这可以被清理和优化。
# Loop through each group
Get-Content R:\PHRBI-R\ADGroup_list.csv | Where-Object{$_ -ne $null -and $_.trim() -ne ""} | Foreach-Object {
# Keep the group name so we can use it later
$Group = $_
# Get the group members and loop through each one
(Get-ADGroup -Properties members -Identity $Group).members | Foreach-Object {
# Get each members username
"$Group $((Get-ADUser -identity $_ -Properties samaccountname).samaccountname)"
}
}
请注意,如果存在嵌套组(即组内的组),则Get-AdUser
cmdlet 将出错。
Import-Module ActiveDirectory
New-Item R:\PHRBI-R\Nishant\AD_List\user_list.csv -ItemType File -force
Add-Content -Path R:\PHRBI-R\Nishant\AD_List\user_list.csv -Value 'Group Name, NUID, Name,Enabled'
$Groups=Get-ADGroup -Filter {Name -like 'ABC*'}
foreach($Group in $Groups)
{
$Details=Get-ADGroup -Identity $Group.Name -Properties members |
Select-Object -ExpandProperty members |
Get-ADUser -Properties samaccountname, enabled, name | Select samaccountname, name, Enabled
foreach($Detail in $Details)
{
Add-Content -Path R:\PHRBI-R\Nishant\AD_List\user_list.csv -Value "$($Group.Name),$($Detail.samaccountname),$($Detail.name),$($Detail.Enabled)"
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.