0

I am creating a script which checks for disabled user. To shorten the time it takes to find the information for each user I wanted to create jobs which do this in parallel. Put only the first job actually finishes, all other jobs timeout with this message:

The operation returned because the timeout limit was exceeded.

Here is my code to find the disabled user and starting the job:

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."
}

This is the scriptblock for the job:

$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 "______________________________________________________________________" 
    }
}
  • 1
    You could do _a lot_ to optimize your script _before_ parallelizing. Two calls (`Get-ADUser -Filter {Enabled -eq $true} -Properties memberOf` and `Get-ADGroup -Filter *`) is, strictly speaking, all you need, everything else you can compute client-side – Mathias R. Jessen Apr 22 '21 at 14:00
  • 1
    Yeah, agree with Mathias, also use [`start-threadjob`](https://learn.microsoft.com/en-us/powershell/module/threadjob/start-threadjob?view=powershell-7.1`) instead of normal jobs which are pretty garbage – Santiago Squarzon Apr 22 '21 at 14:06
  • Thank you, this helps a lot. I didn't think about this angle before. I'll get started computing it client-side. But I am still curios why it times out like this. – Frogrhino Apr 22 '21 at 14:11
  • Hard to tell if we don't have the error message, if you're getting a `invalid enumeration context` exception then the issue is that AD cmdlets will timeout after 30 minutes. This is a standard AD WS configuration. – Santiago Squarzon Apr 22 '21 at 14:21
  • I can rewrite this script for you using `start-threadJob` if you're able to use that module. But first I need to know for sure that this line is NOT the one timming out: `$disabledUsers = (Get-ADUser -filter {(Enabled -eq "false")})` – Santiago Squarzon Apr 22 '21 at 15:22
  • This is definitely not the line which is timing out the script. – Frogrhino Apr 23 '21 at 06:53

0 Answers0