0

I want to execute a set of powershell commands to manage my HyperV Guest machine. Can I use the below code to do this ? Do I need to clear pipeline.Commands before executing the next command ?

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();
}
Yesudass Moses
  • 1,841
  • 3
  • 27
  • 63
  • 1
    Consider using the `PowerShell` class instead of `Pipeline`. Use `AddStatement()` in between individual pipelines and you should be good to go – Mathias R. Jessen Dec 28 '17 at 14:43

1 Answers1

0

Based on what you show here, I cannot see a reason for you to use the clear effort.

postanote
  • 15,138
  • 2
  • 14
  • 25