Pick an account after Connect-MicrosoftTeams

Viewed 700

I'd like to write a PowerShell script which will update Teams members from input list/object. However if I run Connect-MicrosoftTeams command (to authenticate/connect to cloud service) for the first time I am asked to pick an account to use for login. This is an issue since I would like this script to be run as scheduled job. Is there a way how to avoid this when running Connect-MicrosoftTeams command ? Commands I am using:

$credential = Get-Credential

Connect-MicrosoftTeams -Credential $credential

Connect-MicrosoftTeams credentials window

I tried to use "-AccountId "email@address.com" but that didn't help. Of course later I will change Get-Credential to username and encrypted password

EDIT

If I run Connect-MicrosoftTeams -Credential $credential on other computer, where I've never been logged in with my account, instead of "Pick an account" window, I get credential window for username and password: Connect-MicrosoftTeams credentials window

4 Answers

As commented, this is certainly a dissapointment, but Single-Sign-On cannot be enabled in Microsoft Teams.

See the discussion here

This should achieve what you're trying to do.

Credits to: https://www.jaapbrasser.com/quickly-and-securely-storing-your-credentials-powershell/

Save Credentials

$Credential = Get-Credential
$Credential | Export-CliXml -Path "<File path/name to save credentials"

Connect using saved credentials through MS Teams PowerShell

$Credential = Import-CliXml -Path "<path of exported credential>"
Connect-MicrosoftTeams -AccountId "<email>" -Credential $Credential

At the end I used other module 'AzureAD' and command 'Add-AzureADGroupMember':

# 'password' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString $Password = "01000000d08c9..." | ConvertTo-SecureString

$Credentials = (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "user@domain.com", $Password)

Connect-AzureAD -Credential $Credentials

$AZ_USER=Get-AzureADUser -Filter "UserPrincipalName eq 'user@domain.com'"

$AZ_GROUP=Get-AzureADGroup -Filter "DisplayName eq 'teams_name'"

Add-AzureADGroupMember -ObjectId $AZ_GROUP.ObjectId -RefObjectId $AZ_USER.ObjectId

then I have to wait couple hours until Active Directory and Teams got synchronized and users were added to AD groups / Teams teams. It's not ideal, but it works with saved credentials and with no user interaction.

Related