【发布时间】: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