从不同域的AD组列表中提取ADUser

时间:2019-05-22 04:59:33

标签: powershell active-directory

我有这段代码从AD组列表中提取ADuser信息,但是有些AD组位于不同的域中。问题在于该代码仅提取域列表中提到的第一个 Domain1 的ADuser信息,而不会转到其他域并提取位于其他域中的AD组的信息。 。

$outFile=".\Output.csv"
$report=@()
$ADGroups = Get-Content ".\ADGroups.txt"

$Domains = @("Domain1.com","Domain2.com","Domain3.com","Domain4.com","Domain5.com")

foreach ($ADGroup in $ADGroups)
{

   try{

Foreach ($Domain in $Domains)


           {
$ADGroupMem = Get-ADGroupMember -Identity $ADGroup  -server $Domain

ForEach ($Member in $ADGroupMem){

    $ADUser = $Member | Get-ADUser -Properties SamAccountName,Department,Title,Manager,l,Mobile
    [PSCustomObject]@{
            UserID = $ADUser.SamAccountName
            Group = $ADGroup
            City = $ADUser.l

                      } 

     $localAdminsReport = new-object PSObject
     $localAdminsReport | add-member -membertype NoteProperty -name "UserID" -Value $ADUser.SamAccountName
     $localAdminsReport | add-member -membertype NoteProperty -name "Group" -Value  $ADGroup
     $localAdminsReport | add-member -membertype NoteProperty -name "City" -Value  $ADUser.l


    #Write output into .csv file
    #write-host  $report  -ForegroundColor cyan 
     write-host   $Domain                
    $report +=$localAdminsReport
    $report|Export-Csv -Path $outFile -NoTypeInformation 

                                } 
            }

         }
      Catch{}
}

提取报表时,它仅从第一个Domain1 AD组中提取了信息,而忽略了Domain2,Domain3 .....中的其他AD组。

2 个答案:

答案 0 :(得分:0)

我已经修改了您的脚本。如以上注释中所述,您应该使用-Append cmdlet的Export-Csv开关。另一件事是您的catch子句为空,因此您不会识别任何错误。我还建议将$ErrorPreference设置为Stop,否则cmdlet不会引发错误。这是重做的代码:

    $outFile = ".\Output.csv"
    $report = @()
    $ADGroups = Get-Content ".\ADGroups.txt"

    $Domains = @("Domain1.com", "Domain2.com", "Domain3.com", "Domain4.com", "Domain5.com")
    $ErrorActionPreference = "Stop"
    foreach ($ADGroup in $ADGroups) {
        try {
            Foreach ($Domain in $Domains) {
                $ADGroupMem = Get-ADGroupMember -Identity $ADGroup -server $Domain
                ForEach ($Member in $ADGroupMem) {

                    $ADUser = $Member | Get-ADUser -Properties SamAccountName, Department, Title, Manager, l, Mobile
                    $localAdminsReport = new-object PSObject
                    $localAdminsReport | add-member -membertype NoteProperty -name "UserID" -Value $ADUser.SamAccountName
                    $localAdminsReport | add-member -membertype NoteProperty -name "Group" -Value  $ADGroup
                    $localAdminsReport | add-member -membertype NoteProperty -name "City" -Value  $ADUser.l

                    $report += $localAdminsReport
                    $report | Export-Csv -Path $outFile -NoTypeInformation -Append
                } 
            }
        }
        Catch { 
            Write-Error "Exception occurred $_"
        }
    }
    $ErrorActionPreference = "SilentlyContinue"
    $report | ForEach-Object { Write-Host $_}

在代码的内容$report中转储用于调试目的。检查$report与导出的CSV文件是否不同。

您还可以通过Visual Studio Codecommand line degguerthe Powershell ISE调试代码。

希望有帮助。

答案 1 :(得分:0)

这对我有用。谢谢Moerwald

    $outFile = ".\Output.csv"
    $report = @()
    $ADGroups = Get-Content ".\ADGroupslist.txt"



    foreach ($ADGroup in $ADGroups) {


    $Domains = @("Domain1","Domain2","Domain3","Domain4","Domain5")

    foreach ($Domain in $Domains)
    {


        try
        {
            $ADGroupMem = Get-ADGroupMember -Identity $ADGroup -server $Domain
            ForEach ($Member in $ADGroupMem) {

                    $ADUser = $Member | Get-ADUser -Properties SamAccountName, l
                    [PSCustomObject]@{
                        UserID = $ADUser.SamAccountName
                        Group = $ADGroup
                        City = $ADUser.l

                                      } 
                    $localAdminsReport = new-object PSObject
                    $localAdminsReport | add-member -membertype NoteProperty -name "UserID" -Value $ADUser.SamAccountName
                    $localAdminsReport | add-member -membertype NoteProperty -name "Group" -Value  $ADGroup
                    $localAdminsReport | add-member -membertype NoteProperty -name "City" -Value  $ADUser.l

                    $report += $localAdminsReport
                    $report | Export-Csv -Path $outFile -NoTypeInformation -Append
                    write-host   "$ADGroup : $Domain"   -ForegroundColor cyan   

                    }
        }
        catch
        {
             Write-Error "Exception occurred $_"

        }

    }


}
$ErrorActionPreference = "Continue"
$report | ForEach-Object { Write-Host $_}