Connecting to Microsoft Teams through non-interactive powershell script

Daniel Becker 1 Reputation point
2021-02-26T08:48:34.62+00:00

I am trying to write an azure function in PowerShell to connect to a Microsoft Teams account:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "Running teams account setup."

[string]$userName = 'xxx'
[string]$userPassword = 'xxx'
[securestring]$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
Write-Host ($secStringPassword | Format-Table | Out-String)
[pscredential]$credentials = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
Write-Host ($credentials | Format-Table | Out-String)
Connect-MicrosoftTeams -Credential $credentials

The error message I'm getting is essentially: ERROR: password_required_for_managed_user: Password is required for managed user

I read that it might be necessary to replace -Credentials with -Identity, but I can't find any documentation on how to implement that.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,263 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
2,845 questions
{count} votes

2 answers

Sort by: Most helpful
  1. JayaC-MSFT 5,526 Reputation points
    2021-03-09T14:36:24.527+00:00

    Hello @Daniel Becker , it seems to be an issue : https://github.com/MicrosoftDocs/office-docs-powershell/issues/5950
    Our core team will be rolling out the fix soon. I will update this thread as soon as there is any development regarding this.

    If this helps, please "Accept the Answer" and "Up-vote" so that it could help others in the community.

    1 person found this answer helpful.

  2. Sharon Zhao-MSFT 25,056 Reputation points Microsoft Vendor
    2021-03-01T02:38:49.81+00:00

    @Daniel Becker ,

    As we are mainly responsible for general issue of Microsoft Teams, your question which is more related to script is not in our scope. Thanks for your understanding.

    The following script is for your reference. I have tested it in my lab. It works well.

    $User = "xxx@contoso.com"  
    $PassWord = ConvertTo-SecureString -String "YourPassword" -AsPlainText -Force  
    $UserCredential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PassWord  
    import-module microsoftteams  
    Connect-MicrosoftTeams -Credential $UserCredential -AccountId $User  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.