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