简体   繁体   English

C#和Powershell Multiple命令

[英]C# and powershell multiple command

I have two lines of power-shell command in the code below. 我在下面的代码中有两行power-shell命令。 Command 1 enables the mailbox, and command 2 disables the active sync. 命令1启用邮箱,命令2禁用活动同步。

PSCommand psCmd = new PSCommand();

command2 ="Enable-Mailbox -Identity \""+ ExchangeIdentity 
    +"\" -Database \""+ExchangeDBName+ "\" -DomainController \""
    +ExchangeDC+"\" -primarysmtpaddress \""+ExchangePrimarySMTPAddress +"\"";

command1 = "Set-CASMailbox -Identity \"" +ExchangePrimarySMTPAddress
    +"\" -ActiveSyncEnabled "+ActiveSync_Status;

allcommand = command2 + " |" + command1;

runspace.Open();

powershell.AddScript(allcommand);

powershell.Runspace = runspace;
ICollection<PSObject> iobj = powershell.Invoke();

When I try to run the command 1 alone in the add script, the mailbox is created successfully. 当我尝试在添加脚本中单独运行命令1时,邮箱创建成功。 When I combine it with the second, the mailbox alone is created but active sync is not disabled (if i pass the value as false). 当我将其与第二个结合使用时,将单独创建邮箱,但不会禁用活动同步(如果我将值传递为false)。

How can I handle two commands in single line? 如何单行处理两个命令?

I would recommend not using AddScript if you are just invoking commands. 如果您只是调用命令,建议不要使用AddScript。

Assuming you want to invoke both commands but not pipe the results from the first to the second, and assuming you are using PowerShell V3, I would write the code like this: 假设您要调用这两个命令,但不希望将结果从第一个传递到第二个,并且假设您使用的是PowerShell V3,我将编写如下代码:

powershell.AddCommand("Enable-Mailbox")
    .AddParameter("Identity", ExchangeIdentity)
    .AddParameter("Database", ExchangeDBName)
    .AddParameter("DomainController", ExchangeDC)
    .AddParameter("PrimarySmtpAddress ", ExchangePrimarySMTPAddress );
// AddStatement - like adding a ';' in a script - V3 only
powershell.AddStatement();
powershell.AddCommand("Set-CASMailbox")
    .AddParameter("Identity", ExchangePrimarySMTPAddress)
    .AddParameter("ActiveSyncEnabled", ActiveSync_Status);

Written this way (not using AddScript), you avoid one possible problem with script injection. 以这种方式编写(不使用AddScript),可以避免脚本注入的一个可能问题。 If any of the arguments can come from a malicious user, it would be possible to construct an argument that closes one of the previous quotes, then adds malicious script and you'll run that script with admin privileges. 如果任何参数可以来自恶意用户,则可以构造一个参数以关闭前面的引号之一,然后添加恶意脚本,然后将使用管理员权限运行该脚本。 Using AddCommand/AddParameter, you can avoid this situation. 使用AddCommand / AddParameter,可以避免这种情况。

Even if you control the arguments completely, you could still hit problems with some arguments if they contain characters like ';' 即使您完全控制了参数,但是如果某些参数包含诸如';'之类的字符,您仍然可能会遇到问题。 and you weren't properly quoting the arguments. 并且您没有正确引用参数。

Doesn't it work if you AddScript(command2) and AddScript(command1) to the pipeline and then Invoke them? 如果将AddScript(command2)和AddScript(command1)添加到管道,然后调用它们,这是行不通的吗?

This should force powershell to run both commands in sequence after each other... 这应该强制powershell依次依次运行两个命令...

For version 2.0 对于2.0版

create c# code to invoke power shell and execute 1st command for mailbox creation then dispose and write another set of c# code to invoke and execute the Active sync command and dispose. 创建c#代码以调用Power Shell并执行第一个命令以创建邮箱,然后处理并编写另一组c#代码以调用并执行Active sync命令并进行处理。

Note: the Domain controller should be the same for mailbox creation and Active sync. 注意:域控制器对于邮箱创建和活动同步应该相同。

According to the docs on the Enable-Mailbox command, it doesn't output anything. 根据Enable-Mailbox命令上的文档,它不会输出任何内容。 Forget the pipe. 别管了。 Change it to ';' 将其更改为“;” to separate the two statements. 分开这两个语句。

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

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