优化Active Directory审核脚本

时间:2014-04-10 02:14:02

标签: powershell active-directory

作为我工作的一部分,我不断审核活动目录以获取跨域帐户的特定属性。

我构建了一个powershell脚本,根据给脚本的属性将信息输出到CSV。这很好,脚本可以很好地用于一小部分人,但是当我提供大量用户进行审核时,我注意到脚本会大大减慢。

这是剧本:

    $inputfile = "C:\Powershell\input.txt"
    $users = Get-Content $inputfile

    $audit = Read-Host "Audit Name"
    $csv = ".\output\Audit\$audit.csv"

    $failed = @()

    $serv = @("server1", "server2", "server3")

    if((Test-Path $csv) -eq $true){Remove-Item $csv}

    foreach($domain in $serv)
    {
        $count = $users.Count
        for( $i=0; $i -le $count - 1; $i++ )
        {
            if (($users.Get($i)) -ne "")
            {
                try
                {
                    Write-Host "Checking for $($users.get($i)) on" -NoNewline
                    switch($domain)
                    { # with fancier text for which domain we're searching
                        "server1" {write-host "...Server1" -ForegroundColor Cyan -NoNewline; $domainCsv = "Server1"}
                        "server2" {Write-Host "...Server2" -ForegroundColor White -NoNewline; $domainCsv = "Server2"}
                        "server3" {Write-Host "...Server3" -ForegroundColor Magenta -NoNewline; $domainCsv = "Server3"}
                    }

                    $usr = Get-ADUser -Identity $users.get($i) -Properties $properties -Server $domain | ? { ($_.distinguishedname -notlike '*Suspended*')}
                    if ($usr -ne $null) 
                    {
                        $usr = Get-ADUser -Identity $users.get($i) -Properties $properties -Server $domain | ? { ($_.distinguishedname -notlike '*Deletion*')}
                        if ($usr -ne $null) 
                        {
                            Write-Host "...Found" -ForegroundColor Green

                            $userobj = New-Object PSObject

                            Add-Member -InputObject $userobj -MemberType NoteProperty -Name "User" -Value $($users.Get($i))
                            Add-Member -InputObject $userobj -MemberType NoteProperty -Name "Domain" -Value $domainCsv

                            foreach($prop in $properties) {$userobj | Add-Member -MemberType NoteProperty -Name $prop -Value "$($usr.$prop)"}

                            $userobj | Export-Csv $csv -Append -NoTypeInformation
                        }
                        else
                        {
                            Write-Host "...Pending Delete" -ForegroundColor Red
                            $failed += "$($users.Get($i)),Pending deletion on $domainCsv"
                        }
                    }
                    else
                    {
                        Write-Host "...Suspended" -ForegroundColor Red
                        $failed += "$($users.Get($i)),Suspended on $domainCsv"
                    }                       
                }
                catch [System.Exception]
                {
                    Write-Host "...Not found" -ForegroundColor Red
                    $failed += "$($users.Get($i)),Could not find on $domainCsv"
                } # </Try
            } # </If user ""
        } # </For users
    } # </For Domains
    Add-Content $csv ""
    Add-Content $csv "Those who failed, (Not found or Suspended or Pending deletion)"
    Add-Content $csv ""
    Add-Content $csv "User,Domain"
    foreach($fail in $failed) {Add-Content $csv $fail}
    Write-Host " "
    Write-Host "    Audit saved to $csv" -ForegroundColor Green

脚本的作用

获取充满用户的输入文件(每行一个名称)(大多数是20行左右,但有时输入文件已超过200行)

user1
user2
user3
user4
user5

遍历每个域

检查他们是否在暂停帐户的OU中

检查他们是否在OU中查找待删除的帐户

如果不在任何一个OU中,则抓取信息并将其放入PSObject以插入CSV

完成后,它列出了它无法找到的帐户或者在OU中我不需要担心的帐户。

由于我对powershell很新,我不知道是否有一种方法可以缩短部分代码以便更快,我已经阅读了一些关于优化PowerShell的页面,但我能看到的唯一变化是变化

for( $i=0; $i -le $users.count - 1; $i++ )

$count = $users.count

for( $i=0; $i -le $count - 1; $i++ )

我的问题是:在给予更多用户的情况下,如何改进我的脚本以更快地循环?

1 个答案:

答案 0 :(得分:0)

据我所知,您的脚本大部分时间都在Get-ADUser中。你使用相同的参数调用它两次,使用$ usr你应该只调用一次,你的脚本执行时间应该是两个。

另一方面,我无法在您的脚本中找到$properties的定义,减少此列表也会降低网络负载。


测试这样的东西。

$usr = Get-ADUser -Identity $users.get($i) -Properties $properties -Server $domain | ? { ($_.distinguishedname -notlike '*Suspended*')}
if ($usr.distinguishedname -notlike '*Suspended*') 
{
  if ($usr.distinguishedname -notlike '*Deletion*') 
  {