知識這東西就像雪球,越滾越大,今天看到了這篇本身1年多前寫的博文,簡直弱爆了。因而更新一下程序:spa
2016-6-15更新,短短几行代碼,就拿到了組和組成員,其中還用到了遞歸,以處理組成員是組的狀況:code
Get-ADGroup -Filter * -SearchBase "ou=test-ou,ou=vb-group,dc=corp,dc=vb" | foreach { [PSCustomObject]@{'GroupName'=$_.name;'Members'=$null} Get-ADGroupMember -Identity $_.name -Recursive | foreach { [PSCustomObject]@{'GroupName'=$null;'Members'=$_.name} } }
如下是一年多前寫的:blog
cd 'F:\ACL\HZ ACL' $report = New-Item -Path .\ACL-APAC.CSV -ItemType file -Force Add-Content -Path .\ACL-APAC.CSV -Value "GroupName,EmpNo,Name" #方法一:(後寫) Import-Csv .\Groups.CSV | ForEach-Object { #下面這句得到組的名稱,而後追加輸出到CSV文件 Get-ADGroup -Filter * -SearchBase $_.GroupName | select -ExpandProperty Name | Out-File $report -Encoding ASCII -Append #下面這個循環:將一個組中的每一個成員的samAccountName, displayName存入變量$users ForEach-Object { $users = Get-ADGroup -Filter * -SearchBase $_.GroupName | Get-ADGroupMember | Get-ADUser -Properties samAccountName, displayName #| select -Property samAccountName, displayName #下面這個循環:將每一個成員的samAccountName, displayName追加輸出到CSV文件 ForEach($user in $users) { "," + $user.samAccountName + "," + $user.displayName | Out-File $report -Encoding ASCII -Append } } } <#方法二:(先寫) Import-Csv .\Groups.CSV | ForEach-Object { #下面是groupname的循環 Get-ADGroup -Filter * -SearchBase $_.GroupName | select -ExpandProperty Name | Out-File $report -Encoding ASCII -Append #下面是group裏成員的循環 ForEach-Object { $MemberSummary = Get-ADGroupMember -Identity $_.GroupName | select -Property SamAccountName #下面是列出每一個用戶的完整屬性 ForEach ($Member in $MemberSummary) { $MemberDetail = Get-ADUser -Identity $Member.SamAccountName -Properties Name, displayName #下面是最終生成的CSV中真正的員工號,姓名那一行內容 ForEach-Object { "," + $MemberDetail.Name + "," + $MemberDetail.DisplayName | Out-File $report -Encoding ASCII -Append } } } } #>