Search code examples
powershellstart-job

Build an array of data in a start-job in powershell and return it


$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.


Solution

  • Note:

    • The next section solves a part of your problem, but it doesn't address why Connect-msolservice doesn't connect.

    • A potential reason is if GetAdminCredentials resulted in an interactive prompt, which you wouldn't get to see until you try to collect the job's output with Receive-Job

      • If that is the problem, run GetAdminCredentials before starting the job and reference the resulting credentials object in the job script block as $using:admincred

    -ArgumentList $frontlineusers passes the elements of array $frontlineusers individually 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:

      # The unary form of `,` the array-constructor operator,
      # creates a single-element array with the operand as its
      # only element.
      -ArgumentList (, $frontlineusers)
      
    • Keep your -ArgumentList argument as-is and replace $users = $args[0] with $users = $args in the job script block.