c# - 使用 Powershell Core 进行多线程在线交换 - C#

标签 c# powershell office365 exchange-server powershell-core

我正在编写一个 C# 应用程序,它将使用 PowerShell V2 module 从 Exchange Online 收集数据。 .在客户执行管理员同意后,我将使用在 Windows 虚拟机上运行的多线程 C# 应用程序与他们的环境建立 PowerShell 连接。我正在使用 Net 5.0 和 PowerShell 7.x。我需要使用多个线程,因为从单个租户收集数据可能是一个漫长的过程。
问题是,虽然应用程序运行良好,但如果我尝试使用多个线程同时为两个租户运行应用程序,则会发生冲突。该模块似乎不是线程安全的。
我已经构建了一个通过 .Net DI 作为 transient 注入(inject)的服务。此服务创建一个 HostedRunspace 类,用于执行 PowerShell 的状态管理。

public class HostedRunspace : IDisposable
{
    private Runspace runspace;
 
    public void Initialize(string[] modulesToLoad = null)
    {

        InitialSessionState defaultSessionState = InitialSessionState.CreateDefault();
        defaultSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.RemoteSigned;
        
        if (modulesToLoad != null)
        {
            foreach (string moduleName in modulesToLoad)
            {
                defaultSessionState.ImportPSModule(moduleName);
            }
        }

        runspace = RunspaceFactory.CreateRunspace(defaultSessionState);
        
        runspace.ThreadOptions = PSThreadOptions.UseNewThread;
        runspace.ApartmentState = ApartmentState.STA;

        runspace.Open();
    }

    public async Task<List<PSObject>> RunScript(string scriptContents, Dictionary<string, object> scriptParameters = null)
    {
        if (runspace == null)
        {
            throw new ApplicationException("Runspace must be initialized before calling RunScript().");
        }

        PSDataCollection<PSObject> pipelineObjects;
        
        using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create(runspace))
        {
            ps.AddScript(scriptContents);

            if (scriptParameters != null)
            {
                ps.AddParameters(scriptParameters);
            }

            ps.Streams.Error.DataAdded += Error_DataAdded;
            ps.Streams.Warning.DataAdded += Warning_DataAdded;
            ps.Streams.Information.DataAdded += Information_DataAdded;

            // execute the script and await the result.
            pipelineObjects = await ps.InvokeAsync().ConfigureAwait(false);
            
            // print the resulting pipeline objects to the console.
            Console.WriteLine("----- Pipeline Output below this point -----");
            foreach (PSObject item in pipelineObjects)
            {
                Console.WriteLine(item.BaseObject.ToString());
            }
        }

        List<PSObject> psObjects = new List<PSObject>();
        foreach (PSObject pipelineObject in pipelineObjects)
        {
            psObjects.Add(pipelineObject);
        }

        return psObjects;
    }
当需要收集租户的 PowerShell 数据时,会创建一个新线程,如下所示:
IOnlineDataTaskRunner taskRunner = serviceProvider.GetRequiredService<IOnlineDataTaskRunner>();
Thread thread = new Thread(() => taskRunner.RunAsync(dataTask));
thread.Start();
在这里,我得到了我的 PowerShell 服务的临时版本,它本身将新建一个 HostedRunspace。我创建一个新线程,为其提供一些配置并启动该线程。
当线程运行时,我首先必须使用证书连接到 Exchange Online。
string command = $"Connect-ExchangeOnline -CertificateThumbprint \"{Thumbprint}\" -AppId \"{ClientId}\" -ShowBanner:$false -Organization {tenant}"; 
await runspace.RunScript(command);
然后,在此之后,我使用 PowerShell 模块执行各种其他数据检索任务,包括检索邮箱信息、存储大小等。这些也是通过
await runspace.RunScript(command);
如上所述,如果我一次运行一个线程,则没有问题。但是,如果我将线程 1 连接到租户 A 并将线程 2 连接到租户 B,则初始 Connect-ExchangeOnline 将毫无问题地进行。
但是,如果您检索邮箱信息,例如,两个线程都会为最后连接的租户提取数据。这表明模块或我的实现可能存在线程问题。

最佳答案

我没有第二个租户,但我确实有第二个具有不同权限的用户帐户,所以我将在我的建议中使用它。我测试了几种处理多个 exo 连接的方法,这就是有效的方法:
通过 Start-Job 使用 Powershell 作业.作业在它们自己的 session 中进行,并且具有一定程度的隔离性:

Start-Job -Name admin01 -ScriptBlock {
    Connect-ExchangeOnline -UserPrincipalName admin01@domain.com
    # Sleep so that second connection happens before this Get-Mailbox
    Start-Sleep -Seconds 10; 
    Get-Mailbox user02@domain.com  ## should succeed as admin
}
Start-Job -Name user01  -ScriptBlock {
    Connect-ExchangeOnline -UserPrincipalName user01@domain.com
    Get-Mailbox user02@domain.com  ## should error due to access as user
}

Receive-Job -Name admin01  ## returns mailbox
Receive-Job -Name user01   ## returns error
看起来像 Connect-ExchangeOnline使用 PSSession对象来存储您的连接,您可以重新导入以验证您已连接到正确的租户,例如:
Get-PSSession | ft -AutoSize

Id Name                            ComputerName          ComputerType  State  ConfigurationName  Availability
-- ----                            ------------          ------------  -----  -----------------  ------------
 5 ExchangeOnlineInternalSession_1 outlook.office365.com RemoteMachine Opened Microsoft.Exchange    Available
 6 ExchangeOnlineInternalSession_2 outlook.office365.com RemoteMachine Opened Microsoft.Exchange    Available
所以我可以使用 Import-PSSession为命令加载特定连接:
# First, run as admin01
Connect-ExchangeOnline -UserPrincipalName admin01@domain.com
Get-Mailbox user02@domain.com  ## Success

# Then, run as user01
Connect-ExchangeOnline -UserPrincipalName user01@domain.com
Get-Mailbox user02@domain.com  ## Error

# Then, run as admin01 again:
Import-PSSession (Get-PSSession 5) -AllowClobber
Get-Mailbox user02@domain.com  ## Success
最后,只运行两个单独的 powershell 实例也可以。
我对 .net 不是很熟悉,但我猜你目前正在重新使用 SessionState当开始一个新线程时,或者你正在分享你的runspace跨线程。

关于c# - 使用 Powershell Core 进行多线程在线交换 - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67290655/

相关文章:

c# - 大负数的 Math.Exp

office365 - 对于使用 javamail 处理的一个 Office365 邮箱,folder.close(true) 不会清除已删除的电子邮件

excel - Excel从 "Environ("用户名“)”中的哪里获取用户名?以及如何编辑它?

c# - MVC 中的 post 问题

c# - 如何禁用 visual studio 中的起始页

c# - Mysql插入查询错误计数列不匹配

excel - 如何从 Powershell 将数据添加到 Excel 电子表格中?

PowerShell netsh 内联命令无法正常工作

windows - 如何使用文件名中现有的 Unix 时间重命名多个文件?

office365 - Office.context.mailbox.item.body.setAsync 去除内嵌图像