【问题标题】:Executing chain of commands in Powershell Pipeline在 Powershell Pipeline 中执行命令链
【发布时间】:2018-06-08 17:23:11
【问题描述】:

我想执行一组 powershell 命令来管理我的 HyperV Guest 机器。我可以使用下面的代码来做到这一点吗?在执行下一个命令之前是否需要清除 pipeline.Commands ?

Collection<PSObject> results = null;
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript("Shutdown-VM ADServer-01");
    results = pipeline.Invoke();
    //Do I need pipeline.Commands.Clear();
    pipeline.Commands.AddScript("Get-VMState ADServer-01");
    results = pipeline.Invoke();
    runspace.Close();
}

【问题讨论】:

  • 考虑使用PowerShell 类而不是Pipeline。在各个管道之间使用AddStatement(),你应该很高兴

标签: c# .net powershell powershell-4.0 runspace


【解决方案1】:

根据您在此处显示的内容,我看不出您使用明确努力的理由。

【讨论】:

  • 我不需要在调用后将关闭命令保留在集合中吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 2014-11-03
  • 2011-03-31
相关资源
最近更新 更多