问题描述
我正在使用此命令通过 PowerShell 7.1.3 或 PowerShell 5.1 连接 Microsoft Teams。
步骤 1 连接到第一个域 (bijay@abc.com)
Import-Module MicrosoftTeams
$userCredential = Get-Credential
Connect-MicrosoftTeams -Credential $userCredential
第 2 步 点击命令获取租户在线应用实例: 获取-CsOnlineApplicationInstance
结果:成功检索到所有应用实例
第 3 步 在同一个 PowerShell 中连接到第二个域 (bijay@xyz.com)
Import-Module MicrosoftTeams
$userCredential = Get-Credential
Connect-MicrosoftTeams -Credential $userCredential
结果:连接成功
第 4 步: 再次点击命令获取应用实例:
Get-CsOnlineApplicationInstance
错误结果:处理来自远程服务器 api.interfaces.records.teams.microsoft.com 的数据失败,并显示以下错误消息:
现在在上面的步骤中,如果我在第 2 步之后使用 disconnect-MicrosoftTeams 命令,我会得到预期的结果。
我通过 C# .Net 和 Asp.Net Core 完成所有这些工作。 SDK:Microsoft.PowerShell.SDK 代码片段
scriptContents.AppendLine("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process");
scriptContents.AppendLine($"$MicrosoftTeamsConnectorPath = Get-Item -Path {microsoftTeamsModulePath}");
scriptContents.AppendLine("Import-Module $MicrosoftTeamsConnectorPath -verbose");
scriptContents.AppendLine("Get-PSSession | Remove-PSSession");
scriptContents.AppendLine("disconnect-MicrosoftTeams");
scriptContents.AppendLine("$securestringPassword = ConvertTo-securestring $Password -AsPlainText -Force");
scriptContents.AppendLine("$Credential = New-Object System.Management.Automation.PSCredential -argumentlist
$UserName,$securestringPassword");
scriptContents.AppendLine("Connect-MicrosoftTeams -Credential $Credential");
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript(scriptContents.ToString());
ps.AddParameters(scriptParameters);
ps.Streams.Error.DataAdded += Error_DataAdded;
ps.Streams.Warning.DataAdded += Warning_DataAdded;
ps.Streams.information.DataAdded += information_DataAdded;
var pipelineObjects = await ps.InvokeAsync().ConfigureAwait(false);
if (ps.Streams.Error.Count > 0)
{
}
else
{
}
}
主要关注点/问题: 当我在本地运行应用程序脚本时,通过使用 disconnect-MicrosoftTeams 步骤 1 到 4 可以正常工作,但是当我在 Azure 应用服务上托管它时它不起作用。 Application Insight 上的错误:
删除之前的 MicosoftTeams 会话(步骤 1 到 2)以便步骤 3 到 4 起作用的最佳方法是什么? 有什么我遗漏的吗?
注意:当作为 .NET 核心或 .NET 框架 Web API 托管在 Azure 应用服务上时,我希望 PowerShell 能够使用 Microsoft Teams 模块命令(如 Get-CsOnlineApllicationInstance)为多个域工作
任何帮助将不胜感激。
解决方法
嗨@Bijay 要满足您的要求,要么我们必须在另一个窗口中运行连接第二个域,要么在 azure 应用服务中创建单独的实例。 New-PSSession 显式创建新会话,然后可以通过将会话传递给 Invoke-Command、Enter-PSSession 和许多其他远程感知命令的 -Session 参数来使用该会话。 https://devops-collective-inc.gitbook.io/secrets-of-powershell-remoting/session-management
要在 azure 应用服务中运行,请创建多个实例。 https://medium.com/devoops-and-universe/microsoft-azure-multi-instance-infrastructure-with-appservice-as-the-center-of-the-universe-29db20400eaa