0

我有以下从 C#.net 服务调用的代码,导致两次调用 powershell 脚本。

using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
      runspace.Open();

      runspace.SessionStateProxy.SetVariable("scriptpath", scriptPath);
      runspace.SessionStateProxy.SetVariable("DBRecordKey", reportId.Key);
      runspace.SessionStateProxy.SetVariable("SymbolPath", symbolPath);
      Logger.Log("DBRecordKey = " + reportId.Key, writer);
      using (Pipeline pipeline = runspace.CreatePipeline(scriptText))
      {
             //Here's how you add a new script with arguments
             pipeline.Commands.AddScript(scriptText);

             // Execute PowerShell script
             Collection<PSObject> results = pipeline.Invoke();

             StringBuilder outputString = new StringBuilder();
             foreach (PSObject obj in results)
             {
                     outputString.AppendLine(obj.ToString());
             }
             Logger.Log("outputString 2 - " + outputString, writer);
             pipeline.Stop();
             runspace.Close();
      }
}

我的代码有什么问题吗?

4

1 回答 1

1

您在CreatePipeline(scriptText)和中指定脚本AddScript(scriptText)。我不是 Powershell 用户,但我建议你要么

using (Pipeline pipeline = runspace.CreatePipeline())
{
    pipeline.Commands.AddScript(scriptText);
    ...
}

...或者您想AddScript完全删除呼叫。如果您需要特定原因AddScript,那么在没有逻辑的情况下创建管道scriptText似乎是合乎逻辑的方法。

于 2014-05-02T06:41:20.793 回答