Microsoft Teams Automation - Teams PowerShell in Azure Functions

After playing around with the Teams cmdlets in Azure Automation I tried to use them in Azure Functions. Should be pretty forward given the fact you can upload your own modules to your function in a matter of minutes. Well, it took me some hours. But let’s get started. To upload a custom PowerShell module have a look at This gives a good overview of all the steps needed to run your scripts in your function. To get the files of the Teams module use this PowerShell command:Save-Module MicrosoftTeams -Repository PSGallery -Path "YOUR\_PATH"The directory should look like this now: You can use ftp now to upload the whole “MicrosoftTeams” folder to a subfolder called “Modules” in your Azure Function. The expected folder structure should be like this: Now you can start to use the Teams cmdlets in your Azure Function, or at least you should be able to do so. But instead I get an error like this:2017-11-03T22:30:42 Welcome, you are now connected to log-streaming service. 2017-11-03T22:30:47.755 Function started (Id=a77121f1-2023-4d02-8974-8d182a2b657c) 2017-11-03T22:30:52.637 Loaded modules: /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/MicrosoftTeams.psd1 /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/Microsoft.IdentityModel.Clients.ActiveDirectory.dll /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/Microsoft.Open.Teams.CommonLibrary.dll /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/Microsoft.TeamsCmdlets.PowerShell.Custom.dll /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/Newtonsoft.Json.dll /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/RestSharp.dll /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/MicrosoftTeams.psm1 2017-11-03T22:30:56.196 Import-Module : The specified module 'D:\\home\\site\\wwwroot\\TeamsRemoteAPI\\modules\\MicrosoftTeams\\0.9.0\\Microsoft.Open.Teams.CommonLibrary.Resources.dll' was not loaded because no valid module file was found in any module <ScriptBlock>, /TeamsRemoteAPI/modules/MicrosoftTeams/0.9.0/MicrosoftTeams.psm1: line 14+ Import-Module+ \_\_\_\_\_\_\_\_\_\_\_\_\_ + CategoryInfo : ResourceUnavailable: (D:\\home\\site\\ww...y.Resources.dll:String) \[Import-Module\], FileNotFoundException + FullyQualifiedErrorId : Modules\_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand.All we try at this moment in the function looks like this:``` $secpasswd = ConvertTo-SecureString ‘MY_PWD’ -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential (“MY_USER”, $secpasswd)

Connect-MicrosoftTeams -Credential $mycreds Same few lines run fine on my local machine and in the Azure Automation Runbook, but not in the Azure Function. After sniffing around I came across the references made in MicrosoftTeams.psm1. In the default way this file looks like this: param( $DllBasePath = $env:dllBasePath)

If ($DllBasePath -eq $Null -or $DllBasePath -eq '' ) { $DllBasePath = $PSScriptRoot } $DllBasePath += “\”

Import-Module “$($DllBasePath)Newtonsoft.Json.dll” Import-Module “$($DllBasePath)Microsoft.IdentityModel.Clients.ActiveDirectory.dll” Import-Module “$($DllBasePath)Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll” Import-Module “$($DllBasePath)Microsoft.Open.Teams.CommonLibrary.dll” Import-Module “$($DllBasePath)Microsoft.Open.Teams.CommonLibrary.Resources.dll” Import-Module “$($DllBasePath)Microsoft.TeamsCmdlets.PowerShell.Custom.dll” In line 14 there is a reference to the DLL my Azure Function is complaining about. Turns out if you just modify this file and get rid of this reference the function is up and running. I now can create a new Teams team with the cmdlets running in an Azure Function. $secpasswd = ConvertTo-SecureString ‘MY_PWD’ -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential (“MY_USER”, $secpasswd)

Connect-MicrosoftTeams -Credential $mycreds

New-Team -DisplayName “TestFromAzure”