Hey guys,
I have a case where i need to convert the immutable ID of a office 365 user into a GUID and then scan across the forest using that GUID and once located in the correct domain, collect some additional info.
The problem i have is there are 20+ domains and because I am scanning through around 37 000 users, it can take a considerable amount of time.
So I started to research the idea of using jobs and run multiple threads at the same time.
My source material was:
The potential is amazing, but for the life of me i cant get it to work as intended.
The main script looks as follows:
Param($ScriptFile = “D:\Docs\Scripts\Speed_Test.ps1”,
$SourceFile = (Import-Csv -Path ‘D:\Docs\CSV\LicensedUsers_ImmTest.csv’),
$MaxThreads = 200,
$SleepTimer = 5)
“Killing existing jobs . . .”
Get-Job | Remove-Job -Force
“Done.”
$i = 0
foreach($User in $SourceFile)
{
While ($(Get-Job -state running -Verbose).count -ge $MaxThreads)
{
Write-Progress -Activity "Creating User List" -Status "Waiting for threads to close" -CurrentOperation "$i threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($i / $SourceFile.count * 100)
Start-Sleep -Milliseconds $SleepTimer
}
$I++
Start-Job -FilePath “D:\Docs\Scripts\Speed_Test.ps1” -ArgumentList $User -Name $User.ImmutableId | Out-Null
Write-Progress -Activity "Creating User List" -Status "Waiting for threads to close" -CurrentOperation "$i threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($i / $SourceFile.count * 100)
}
$Data = ForEach ($Job in (Get-Job))
{ Receive-Job $Job
#Remove-Job $Job
}
$Data | select UserPrincipalName, DistinguishedName |
Export-Csv -NoTypeInformation -Delimiter “;” -Path ‘D:\Docs\Reports\SSRP_Group_SourceInfo_SpeedTest.csv’ -Append
Script referenced in the start-job:
$Domains = “Domain1”, “Domain2”,“Domain3”, Domain 4-20…
$Decode = [system.convert]::frombase64string($args[0].ImmutableId)
$GUID = [GUID]$Decode
foreach($Domain in $Domains)
{
$UserInfo = Get-ADUser -Identity $GUID -Server $Domain -Properties UserPrincipalName, DistinguishedName
}
If i run the referenced script mentioned in the Start-job independantly it works as intended, but when i attempt to run it through a start-job i get no results and the constant error code:
A referral was returned from the server
+ CategoryInfo : ResourceUnavailable: (52ad7263-504a-87c8-3224-d0c50ad12ea0:ADUser) [Get-ADUser], ADReferralException
+ FullyQualifiedErrorId : A referral was returned from the server,Microsoft.ActiveDirectory.Management.Commands.GetADUser
+ PSComputerName : localhost