运行非常慢的Get-ADUser阵列-我可以加快速度吗?

时间:2018-07-06 13:24:10

标签: powershell

我使用以下代码已有一段时间了,它可以工作,但是要花几个小时才能运行。如果运行单独的Get-命令,则可在数分钟内获得结果,但是一旦在其中添加阵列,则可立即扩展到数小时。

由于我对PS还是很陌生,因为我不需要经常使用它,因此我可能对此感到不敢忍受。

  Import-Module Activedirectory

              $Data=@(

              Get-ADUser  -filter * -Properties * |  
              Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
              @{Name = "Last Name";Expression = {$_.Surname}},
              @{name=  "OU";expression={$_.DistinguishedName.split(',')[1].split('=')[1]}},
              @{Name = "Email";Expression = {$_.Mail}},
              @{Name = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}},
              @{Name = "Department";Expression = {$_.Department}}

                     )

              $Data | Export-Csv -Path c:\adusers.csv -NoTypeInformation      

2 个答案:

答案 0 :(得分:2)

仅选择所需的内容将使其运行更快。我将开始在工作环境中使用这种方法。我从这个问题中学到了一些东西

Import-Module Activedirectory

$Data=@()

$Data = Get-ADUser -Filter * -Properties "Mail","Department" | 
    Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
    @{Name = "Last Name";Expression = {$_.Surname}},
    @{Name=  "OU";expression={$_.DistinguishedName.split(',')[1].split('=')[1]}},
    @{Name = "Email";Expression = {$_.Mail}},
    @{Name = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}},
    @{Name = "Department";Expression = {$_.Department}}

$Data | Export-Csv -Path c:\logs\adusers.csv -NoTypeInformation

答案 1 :(得分:0)

运行秒!

Import-Module Activedirectory

$Data=@(                  
Get-ADUser  -Filter {Enabled -eq $true} -SearchBase “ou=User Accounts,ou=UserAccounts,dc=hiscox,dc=com” -Properties EmailAddress, extensionattribute2 |  
Select-Object @{Label = "First Name";Expression = {$_.GivenName}}, @{Name = "Last Name";Expression = {$_.Surname}}, @{Name = "Email";Expression = {$_.EmailAddress}}, @{Name = "Business Area";Expression = {$_.extensionattribute2}}
        )

$Data | Export-Csv -Path c:\adusers.csv -NoTypeInformation