简体   繁体   English

在C#中更快地调用PowerShell cmdlet

[英]Invoking PowerShell cmdlets more quickly in C#

I am able to fetch user details from O365 using Powershell cmdlets in C#. 我可以使用C#中的Powershell cmdlet从O365获取用户详细信息。 The problem is the fetching time. 问题是获取时间。 That is too slow. 那太慢了。

It takes 2 seconds to each user so it will lead to a time problem if I have the bulk of users. 每个用户需要2秒钟,所以如果我有大量用户,则会导致时间问题。

Here I am just want to print all the user's info like name, group details, licenses. 在这里,我只想打印所有用户的信息,例如姓名,组详细信息,许可证。 How can I do it more quickly? 我怎样才能更快地完成?

Tried one: 尝试了一个:

        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        Pipeline UserDetailsPipe = runspace.CreatePipeline();
        UserDetailsPipe.Commands.AddScript("Get-AzureADUser");
        foreach (PSObject info in UserDetailsPipe.Invoke())  /////////*******
        {
            ArrayList Groups = new ArrayList();   // to hold memberOf
            ArrayList Licenses = new ArrayList(); // to hold of licenses

            string UserPrincipalName = info.Members["UserPrincipalName"].Value.ToString();
            string DisplayName = info.Members["DisplayName"].Value.ToString();

            //Getting MemberOf
            Pipeline memberPipe = runspace.CreatePipeline();
            memberPipe.Commands.AddScript("Get-AzureADUser -ObjectId '" + UserPrincipalName + "'| Get-AzureADUserMembership");

            //Getting Licenses
            Pipeline licensePipe = runspace.CreatePipeline();
            licensePipe.Commands.AddScript("$license = Get-AzureADUserLicenseDetail -ObjectId '" + UserPrincipalName + "' | select ServicePlans ");
            licensePipe.Commands.AddScript("$license.ServicePlans");


                foreach (var licensenames in licensePipe.Invoke())////////*****
                {
                    Licenses.Add(licensenames.Members["ServicePlanName"].Value.ToString());
                }

            foreach (var memberOf in memberPipe.Invoke())////////*******
            {
                Groups.Add(memberOf.Members["DisplayName"].Value.ToString());
            }
       }

I know that I am invoking many pipelines. 我知道我正在调用许多管道。 So how to get my answers by using only one invoke? 那么,如何仅使用一个调用就可以得到我的答案呢? (It may be in PowerShell class too). (它也可能在PowerShell类中)。

It appears the best solution will be to combine into as few scripts as possible to reduce the number of pipelines. 似乎最好的解决方案是将脚本合并为尽可能少的脚本,以减少管道数量。 I also would suggest using the PowerShell object instead of Runspaces 我也建议使用PowerShell对象代替Runspaces

Also in order to get all results in the final Collection after invoking, use the .AddStatement() method. 同样,为了在调用后获得最终Collection中的所有结果,请使用.AddStatement()方法。 This is demonstrated below. 如下所示。 Documentation for AddStatement AddStatement的文档

This will need to be modified to your environment as I am simply getting the date twice to demonstrate returning both dates to the final collection. 这将需要针对您的环境进行修改,因为我只是简单地获取两次日期以演示将两个日期都返回到最终集合中。

using (PowerShell powershell = PowerShell.Create()) {
    powershell.AddScript("Get-Date");
    powershell.AddStatement().AddScript("Get-Date");

    Collection<PSObject> result = powershell.Invoke();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM