Hello,
Has anyone found a proper way to connect to Teams powershell and run basic commands?
Currently, it is possible to connect to Microsoft Teams using only the credentials of a user account. The credentials can be specified in the Run As section of the Run a program or PowerShell script action using the This account option.
I'm running Adaxes 2021.1, Version 3.14.18804.0, and I believe I've got Teams powershell 2.3.1 installed
You need to update the Microsoft Teams PowerShell module to version 4.0.0 or later. There are known issues in version 2.3.1 of the module.
I attempted to follow Adaxes's other suggestion here to swap our service account to an Application Account
You can register Adaxes as an application in Azure and use the application account to manage your Microsoft 365 tenant in Adaxes. However, according to our tests, the Connect-MicrosoftTeams cmdlet does not work when an application account access token is specified, it looks like there is a bug in the cmdlet. The cmdlet works only when the credentials of a user account are specified.
Here is the script as I have made it.
The Get-CsOnlineUser, Grant-CsOnlineVoiceRoutingPolicy, and Grant-CsCallingLineIdentity cmdlets do not accept a user object in the Identity parameter. To identify a user, the Identity property can be used. In this case, the command should look like this:
Set-CsUser -Identity $user.Identity -EnterpriseVoiceEnabled $true -HostedVoiceMail $true
Additionally, please, note that the following command will not find a user if no Microsoft 365 license is assigned to the user:
Get-CsOnlineUser -Filter "Identity -eq '$objectId'"
We updated the script you provided and tested it, it should work just fine. Please, find it below.
# Get saved credentials
$username = $Context.RunAs.UserName
$password = $Context.RunAs.Password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PsCredential($username, $password)
try
{
# Get the object ID in Microsoft 365
$objectId = ([Guid]$Context.TargetObject.Get("adm-O365ObjectId")).ToString()
}
catch
{
return # The user doesn't have a Microsoft 365 account
}
try
{
# Get the user in Microsoft Teams
Connect-MicrosoftTeams -Credential $credential
$user = Get-CsOnlineUser -Filter "Identity -eq '$objectId'"
if ($NULL -eq $user)
{
$Context.LogMessage("The user does not have a Microsoft Teams account", "Information")
return # User does not exist in Microsoft Teams
}
Set-CsUser -Identity $user.Identity -EnterpriseVoiceEnabled $true -HostedVoiceMail $true
Grant-CsOnlineVoiceRoutingPolicy -Identity $user.Identity -PolicyName "Worldwide"
Grant-CsCallingLineIdentity -Identity $user.Identity -PolicyName "No DDI Auckland"
}
finally
{
# Close the connection and release resources
Disconnect-MicrosoftTeams
}