0

I have a script, on which I connect to MicrosoftTeams (connect-microsoftteams) since I have a long running operation, I am trying to run that code in a separate job. however, I do not know how I can pass along the already existing connection to MicrosoftTeams, to that scriptblock.

if (!$Connection) {$Connection = Connect-MicrosoftTeams}

$scriptBlocks = @{
  $btns[0] = 
    {
    param ([Parameter(Mandatory=$true)]$Users)
        #Connect-MicrosoftTeams
        $agents = @()
        foreach ($a in $Users) {
            $user = get-csonlineuser $a.ObjectID
            if ($user) {
                $agents += $user
            }
        }
        return $agents
    }
}

I launch this using:

foreach ($btn in $btns) {

  $btn.Add_Click({

    # Temporarily disable this button to prevent re-entry.
    $this.IsEnabled = $false

    # Show a status message in the associated text box.
    $txtBoxes[$this].Visibility = "Visible"

    # Asynchronously start a background thread job named for this button.
    # Note: Would work with Start-Job too, but that runs the code in *child process*, 
    #       which is much slower and has other implications.
    $null = Start-Job -Name $this.Name $scriptBlocks[$this]

  })

}

I later do some checks to see if the job completed. However, this creates a new Powershell instance, that does not have the $Connection to MicrosoftTeams as the 'parent' has..

[edit] I tried some more, like passing in a Get-Credential, this is almost willing to work.. except MFA is being a problem

$scriptBlocks = @{
  $btns[0] = {
    param ($Credential)
    Connect-MicrosoftTeams -Credential $Credential
    $global:AllCallQueues = Get-CsCallQueue -first 10000
  }
}

this Produces:


Loaded Module 'Microsoft.Teams.ConfigAPI.Cmdlets'
One or more errors occurred.
AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access 'xxx'.
Trace ID: xxx
Correlation ID: xx
Timestamp: 2021-10-26 18:58:45Z
One or more errors occurred.
Exception calling "GetSteppablePipeline" with "1" argument(s): "Exception calling "GetRemoteNewCsOnlineSession" with "1" argument(s): "Run either Connect-MicrosoftTeams or new-csonlinesession before runnin
g cmdlets.""

all credits for this code go to https://stackoverflow.com/a/65531439/3014542

Fimlore
  • 147
  • 1
  • 9
  • 1
    If feasible, try `Start-ThreadJob` instead of `Start-Job`. `Start-ThreadJob` comes with PowerShell (Core) v6+ and in Windows PowerShell can be installed on demand with, e.g., `Install-Module ThreadJob -Scope CurrentUser`. In most cases, thread jobs are the better choice, both for performance and type fidelity - see the bottom section of [this answer](https://stackoverflow.com/a/59440248/45375) for why. In your case it _may_ make the MFA problem go away. – mklement0 Oct 26 '21 at 19:25
  • 1
    Interesting. This does indeed seem to passthru the already loaded connection. – Fimlore Oct 26 '21 at 19:38
  • I had a hunch; with `Start-ThreadJob` you're running in a new thread _in the same process_, whereas `Start-Job` uses an independent _child process_ that knows nothing about the caller's state. – mklement0 Oct 26 '21 at 20:30

0 Answers0