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