【问题标题】:How to run multiple commands in single script block in PowerShell using C#?如何使用 C# 在 PowerShell 中的单个脚本块中运行多个命令?
【发布时间】:2020-02-25 23:53:57
【问题描述】:

我正在尝试在单个脚本中执行多个 PS 命令,但它引发了异常。我尝试了不同的选项,但没有奏效。

 using (powershell = PowerShell.Create())
 {
    command = new PSCommand();
    command.AddCommand("Invoke-Command");
    command.AddParameter("ScriptBlock",
          System.Management.Automation.ScriptBlock.Create(
           "New-MailContact -Name '" + txtEmail.Text + "' -ExternalEmailAddress '" + txtEmail.Text + "';" ));
     command.AddParameter("ScriptBlock",
           System.Management.Automation.ScriptBlock.Create(
           "Set-MailContact -Identity '" + txtEmail.Text + "'-HiddenFromAddressListsEnabled $true"));


    command.AddParameter("Session", session);
    powershell.Commands = command;
    powershell.Runspace = runspace;
    result = powershell.Invoke();
    if (powershell.Streams.Error.Count > 0 || result.Count != 1)
    {
         if (powershell.Streams.Error[0].ToString().ToLowerInvariant().Contains("already exists"))
         {
               return;
          }
          else
          {
               throw new Exception("Fail to establish the connection");
           }
       }
      }

无法绑定参数,因为多次指定了参数“ScriptBlock”。要为可以接受多个值的参数提供多个值,请使用数组语法。比如“-参数value1,value2,value3”。

我也试过了

command.AddParameter("ScriptBlock",
   System.Management.Automation.ScriptBlock.Create(
   "New-MailContact -Name '" + txtEmail.Text + "' -ExternalEmailAddress '" + txtEmail.Text + "';" +  " Set-MailContact -Identity '" + txtEmail.Text + "'-HiddenFromAddressListsEnabled $true"));

command.AddParameter("ScriptBlock",
   System.Management.Automation.ScriptBlock.Create(
   "(New-MailContact -Name '" + txtEmail.Text + "' -ExternalEmailAddress '" + txtEmail.Text + "')" +  " Set-MailContact -Identity '" + txtEmail.Text + "'-HiddenFromAddressListsEnabled $true"));

【问题讨论】:

  • 再添加一个:var anotherCommand = powershell.Commands.AddCommand();
  • 我不得不指出你有命令注入漏洞。您需要通过将撇号加倍(类似于 SQL)来转义撇号,以便输入中的撇号不会关闭字符串并允许用户输入的命令跟随。这样的输入可能非常危险:'; Remove-Item \ -Recurse -Force #

标签: c# powershell powershell-3.0


【解决方案1】:

我认为您可以大大简化您的代码 - 您不需要 Invoke-CommandScriptBlock 元素来分别调用一个命令 - 您可以直接调用 cmdlet。

如果您使用参数构建命令,您还可以同时解决@madreflection 引发的脚本注入问题。

这是 PowerShell 中的一个版本(对我来说,它比启动示例 C# 项目更容易测试 :-)),您可以将其转换为 C#:

$p = [System.Management.Automation.PowerShell]::Create()

$c = new-object System.Management.Automation.Runspaces.Command("New-MailContact");
$c.Parameters.Add("Name", $name)
$c.Parameters.Add("ExternalEmailAddress", $address)
$null = $p.Commands.AddCommand($c)

$c = new-object System.Management.Automation.Runspaces.Command("Set-MailContact");
$c.Parameters.Add("Identity", $identity)
$c.Parameters.Add("HiddenFromAddressListsEnabled", $hidden)
$null = $p.Commands.AddCommand($c)

$p.Invoke()

更新 - 啊,刚刚注意到您在 Invoke-Command 上使用了 Session 参数 - 如果您使用远程处理执行它,那么您可能确实需要毕竟Invoke-Command。也许这个答案会有所帮助......

Set Paramerters in ScriptBlock when Executing Powershell Commands with C#

【讨论】:

    猜你喜欢
    • 2019-08-15
    • 2020-08-11
    • 2022-08-04
    • 2023-01-12
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多