Connecting to Microsoft Teams through non-interactive powershell script

Daniel Becker 1 Reputation point

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.
2,991 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,065 questions
{count} votes

2 answers

Sort by: Most helpful
  1. JayaC-MSFT 5,496 Reputation points

    Hello @Daniel Becker , it seems to be an issue :
    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 24,846 Reputation points Microsoft Vendor

    @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 = ""  
    $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.