简体   繁体   English

从C#运行Powershell最有效的方法来在Exchange Online中创建多个邮件联系人

[英]Run powershell from c# most efficient way to create multiple mailcontacts in Exchange Online

I wish to create multiple mailcontacts (external Contacts) in the GAL in Microsoft Online by running Powershell command from C#. 我希望通过从C#运行Powershell命令在Microsoft Online中的GAL中创建多个mailcontact(外部联系人)。 The code below works, but is very slow and takes about 15-20 min to run for 400 mailcontacts. 下面的代码可以运行,但是速度很慢,大约需要15-20分钟才能运行400个邮件联系人。

foreach(EmailAdressVM emailAddressVM in emailList.emailAddresses1)
{
    //Create New MailContact.
    Pipeline pplNewMailContact = runspace.CreatePipeline();

    Command cmdNewMailContact = new Command("New-MailContact");
    cmdNewMailContact.Parameters.Add("Name", emailAddressVM.sExternalEmailAddress);
    cmdNewMailContact.Parameters.Add("Displayname", emailAddressVM.sFullName.Trim());
    cmdNewMailContact.Parameters.Add("Lastname", emailAddressVM.sLastName.Trim());
    cmdNewMailContact.Parameters.Add("Firstname", emailAddressVM.sFirstName.Trim());
    cmdNewMailContact.Parameters.Add("ExternalEmailAddress", emailAddressVM.sExternalEmailAddress.Trim());

    pplNewMailContact.Commands.Add(cmdNewMailContact);
    pplNewMailContact.Invoke();
    pplNewMailContact.Stop();
    pplNewMailContact.Dispose();
}

I am guessing that this is slow since I create a new Pipeline for every new mailcontact that is added and there has to be a more eficient way of doing this since running... 我猜测这很慢,因为我为添加的每个新邮件联系人创建了一个新管道,并且自运行以来必须有一种更有效的方式来执行此操作...

import-csv <filename> | ForEach {
    new-mailcontact -name $_.emailaddress -displayname $_.FullName -lastname $_.lastname -firstname $_.firstname -externalemailaddress $_.emailaddress -alias $_.alias
}

...is much faster. ...快得多。

I have found some references after many hours of searching the web that you can do something similar to using a CSV when running Powershell commands from C#, ie send a list (or array) of values to a command (in this case the "new-mailcontact" command). 在网上搜索了许多小时后,我发现了一些参考,您可以从C#运行Powershell命令时执行与使用CSV类似的操作,即将值列表(或数组)发送到命令(在本例中为“ new- mailcontact”命令)。 But, I have not found any good example of how to send more than one value to a command and I need to supply many values (for example: -name $ .emailAddress -displayname $ .FullName, etc.) to the "new-mailcontact" command. 但是,我还没有找到任何有关如何向命令发送多个值的好示例,并且我需要向“ new-”提供许多值(例如:-name $ .emailAddress -displayname $ .FullName等)。 mailcontact”命令。

Is it possible to send a list (or array) in a similar way as the "import-csv" command (when using regular powershell) and will this be faster, or is there an evan better way? 是否可以使用与“ import-csv”命令类似的方式(使用常规powershell时)发送列表(或数组),这样会更快,还是有更好的方法? Would I get better performance if I use Powershell 3 instead of 1 (as I am using now). 如果我使用Powershell 3而不是1(因为我现在正在使用),将会获得更好的性能。

Please provide working sample code i C#! 请提供有效的示例代码C#!

Please note that I cannot save a CSV file to disk and the execute powershell from CMD since I do not have write access to disk and that I do not think that I can run an entire script remotely (since remote scripting probably is disabled on Exchange Online). 请注意,我无法将CSV文件保存到磁盘,也无法从CMD执行powershell,因为我没有对磁盘的写访问权限,并且我不认为我可以远程运行整个脚本(因为远程脚本可能在Exchange Online上被禁用了) )。

The biggest reason I would think is because for each address you are creating a new Powershell instance and you are not multithreaded. 我认为最大的原因是因为您为每个地址创建了一个新的Powershell实例,并且您没有使用多线程。

You code looks something like this from above: 您的代码从上面看起来像这样:

Foreach email address{
    Declare a new Powershell process
    Add attributes to call later
    Start Powershell and pipe stuff in
    Close Powershell instance
}

I think you would be better off creating the Powershell instance / pipe once and then sending each object into it. 我认为您最好一次创建Powershell实例/管道,然后将每个对象发送到其中。 More along the lines of: 更多内容如下:

Create PS Pipe
    Foreach email address{
     PS.SendArguments(Email, Name, DN, etc.);
     }

I am not in an environment to get something working or tested right now, so hopefully this gives you at least most of what you need... 我现在不在环境中进行任何工作或测试,因此希望这至少可以为您提供所需的大部分东西...

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

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