特定 OU 的 Powershell 脚本

特定 OU 的 Powershell 脚本

需要以下帮助,一个脚本可以导出特定 OU 中多个安全组的所有成员。OU 的位置是:OU=Security Groups、OU=Public folders、OU=Exchange、DC=REGROEP、DC=LOCAL,我希望你能帮助我。

我尝试以下;

脚本;

Import-Module ActiveDirectory
cd AD:
$MemberList = New-Item -Type file -Force “c:\scripts\GroupMembers.csv”
Import-Csv “C:\Scripts\Groups.csv” | ForEach-Object {
$GName = $_.GroupName
$group = Get-ADGroup $GName
$group.Name | Out-File $MemberList -Encoding ASCII -Append
foreach ($member in Get-ADGroupMember $group)
{
$member.Name | Out-File $MemberList -Encoding ASCII -Append
  }
$nl = [Environment]::NewLine | Out-File $MemberList -Encoding ASCII -Append
}

標籤:

GroupName
OU=Security Groups,OU=Public folders,OU=Exchange,DC=REGROEP,DC=LOCAL

我收到错误;

Get-ADGroup : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a    non-null argument and try the command again.
At C:\scripts\PF users.PS1:6 char:21
+ $group = Get-ADGroup <<<<  $GName
   + CategoryInfo          : InvalidData: (:) [Get-ADGroup], ParameterBindingValidationException
    + FullyQualifiedErrorId :     ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADGroup

Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Supply an argument
 that is not null or empty and then try the command again.
At C:\scripts\PF users.PS1:8 char:38
+ foreach ($member in Get-ADGroupMember <<<<  $group)
    + CategoryInfo          : InvalidData: (:) [Get-ADGroupMember],     ParameterBindingValidationException
    + FullyQualifiedErrorId :     ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember

答案1

$outfile = 'Groups.csv'
$groups = Get-ADGroup -Filter * -SearchBase "OU=customOU,DC=panic,DC=org"

foreach ($group in $groups) {
    Get-ADGroupMember -Identity $group |
        Select name, objectClass, @{Expression={$group.Name}; Label='Group'} |
            Export-CSV -Path $outfile -NoTypeInformation `
                                      -Encoding ASCII `
                                      -Append `
                                      -Verbose
}

应该也适用于 PowerShell v2。已测试:v4+

群组.csv:

"name","objectClass","Group"
"User1","user","Group1"
"User2","user","Group1"
"User3","user","Group2"
"User4","user","Group2"
"User5","user","Group3"
"User6","user","Group3"

如果您有无数个这样的数据,您可能希望将结果存储在 PSCustomObject 中,然后将其转储到 CSV 中,避免为每一行输出触碰磁盘。

相关内容