$frontlineusers = Get-MsolGroupMember -GroupObjectID $frontline -All
Start-Job {
$users = $args[0]
$allusers2 = @()
$svcaccount = "<accountname>"
$admincred = GetAdminCredentials $svcaccount
Connect-msolservice -Credential $admincred
ForEach ($user in $users) {
$msuser = Get-msoluser -ObjectID $user.ObjectID
$pso = New-Object psobject
$pso | Add-Member -MemberType NoteProperty -Name DisplayName -Value $msuser.DisplayName
$pso | Add-Member -MemberType NoteProperty -Name Department -Value $msuser.Department
$pso | Add-Member -MemberType NoteProperty -Name City -Value $msuser.City
$pso | Add-Member -MemberType NoteProperty -Name FirstName -Value $msuser.FirstName
$pso | Add-Member -MemberType NoteProperty -Name LastName -Value $msuser.LastName
$pso | Add-Member -MemberType NoteProperty -Name Office -Value $msuser.Office
$pso | Add-Member -MemberType NoteProperty -Name UserPrincipalName -Value $msuser.UserPrincipalName
$pso | Add-Member -MemberType NoteProperty -Name PrimarySMTPAddress -Value $user.EmailAddress
$pso | Add-Member -MemberType NoteProperty -Name PostalCode -Value $msuser.PostalCode
$pso | Add-Member -MemberType NoteProperty -Name State -Value $msuser.State
$pso | Add-Member -MemberType NoteProperty -Name IsLicensed -Value $msuser.IsLicensed
$pso | Add-Member -MemberType NoteProperty -Name LicenseType -Value "frontline"
$pso | Add-Member -MemberType NoteProperty -Name SamAccountName -Value $msuser.SignInName.Split("@")[0]
$allusers2 += $pso
}
Return $allusers2
} -ArgumentList $frontlineusers
The Jobs run but it doesn't actually connect to msol and pull user data and obviously I can't get it to return anything. I have been trying setting the job to a variable using the $users variable as a defined param nothing seems to work. Any help is appreciated.
Note:
The next section solves a part of your problem, but it doesn't address why
Connect-msolservicedoesn't connect.A potential reason is if
GetAdminCredentialsresulted in an interactive prompt, which you wouldn't get to see until you try to collect the job's output withReceive-JobGetAdminCredentialsbefore starting the job and reference the resulting credentials object in the job script block as$using:admincred-ArgumentList $frontlineuserspasses the elements of array$frontlineusersindividually as arguments to your job script block.Therefore,
$args[0]refers to only the first element.There are two solutions:
Keep your job script block as-is and pass the array as a nested one:
Keep your
-ArgumentListargument as-is and replace$users = $args[0]with$users = $argsin the job script block.