0

我正在创建一个检查禁用用户的脚本。为了缩短为每个用户查找信息所需的时间,我想创建并行执行此操作的作业。仅将第一个作业实际完成,所有其他作业超时并显示此消息:

由于超出了超时限制,操作返回。

这是我查找禁用用户并开始工作的代码:

function Get-Disabled-Info ($infoFile){
    Write-Host "Collecting all disabled users..."
    $disabledUsers = (Get-ADUser -filter {(Enabled -eq "false")})

    Write-Output "Number of user who are disabled: " >> $infoFile
    $disabledUsers.Count >> $infoFile
    
    foreach ($user in $disabledUsers){
        $userCluster[$counter] = ($user.ToString())

        if((($counter % 10) -eq 0 -And $counter -ne 0) -or $counter+1 -eq $disabledUsers.Count){
            Start-Job -ScriptBlock $disabledUserBlock -ArgumentList (,$userCluster)
            $userCluster = New-Object string[] 10
            #break
        }
        $counter++
    }

    Write-Host "Waiting for background jobs..." -NoNewLine

    While (@(Get-Job | Where-Object { $_.State -eq "Running" }).Count -ne 0)
    {  
       Start-Sleep -Seconds 3
       Write-Host "." -NoNewline
    }
     
    ForEach ($Job in (Get-Job)) {
       Receive-Job $Job -Keep >> $infoFile
       Receive-Job $Job
       Remove-Job $Job
    }
    

    Write-Output "`nDomain user information saved to $disabledText in current directory."
}

这是作业的脚本块:

$disabledUserBlock = {
    param (
        [String[]]$users
        )
    for ($i = 0; $i -lt $users.Count; $i++) {
        Get-ADUser $users[$i] -Properties * | Select-Object GivenName, Surname, SamAccountName, Enabled, LastLogonDate, CanonicalName 
        Write-Output "Group membership: " 
        (Get-ADPrincipalGroupMembership $users[$i]).Name 
        Write-Output "______________________________________________________________________" 
    }
}
4

0 回答 0