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

JoshT
  • 23
  • 1
  • 8
  • 1
    As an aside: Unless you're still using PSv2, a much simpler and more efficient way to construct custom objects is to use the following syntactic sugar: `[pscustomobject] @{ foo = 1; bar = 'two' }` - see [this answer](https://stackoverflow.com/a/45293404/45375) – mklement0 Apr 21 '23 at 16:28
  • 1
    $args[0] is only the first element of the array. – js2010 Apr 21 '23 at 16:35

1 Answers1

1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • looks like that did the trick it's processing and getting the data now I just need to figure out how to get the data out of the job – JoshT Apr 21 '23 at 17:54
  • @JoshT, for that you need [`Receive-Job`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/receive-job) and [`Remove-Job`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/remove-job) to clean up, though you can combine the two with `Receive-Job -Wait -AutoRemoveJob` – mklement0 Apr 21 '23 at 19:42
  • 1
    Receive-job wasn't working with the Return if I actually just call the $allusers variable it shows in the output of the job and I'm able to return to the primary variable of the script. Thank you very much for your help! I'll post a full redacted version back once I get every piece setup. – JoshT Apr 22 '23 at 01:21
  • My pleasure, @JoshT. Note that `return ` is just syntactic sugar for `; return`, so the value of `` is output in _both_ cases - see [this answer](https://stackoverflow.com/a/55066394/45375). – mklement0 Apr 22 '23 at 02:25