I'm trying to build an app to restart VMs in Hyper V in Server 2012 I had each VM in the list restarting but i want to adapt it to turn the machine off and then back on. The commented code is the working forced resart. Thanks in advance.
public async static void RestartAllVMs(List<VM> vmList, int timeDelay)
{
PowerShell ps = PowerShell.Create();
foreach (VM vm in vmList)
{
/*//Create PowerShell object
PowerShell ps = PowerShell.Create();
ps.AddCommand("Restart-VM");
ps.AddArgument(vm.vmName);
ps.AddParameter("Force");
ps.Invoke();
await Task.Delay(timeDelay * 1000);*/
//Create PowerShell object
//I want to run from here down instead of just restarting the code doesn't work and no errors are thrown.
ps.AddCommand("Stop-VM");
ps.AddArgument(vm.vmName);
ps.AddCommand("Start-Sleep");
ps.AddParameter("s", 10);
ps.AddCommand("Start-VM");
ps.AddArgument(vm.vmName);
ps.AddCommand("Start-Sleep");
ps.AddParameter("m", 500);
ps.Invoke();
await Task.Delay(timeDelay * 1000);
}
}
Stop-VMcommand need a-nameargument? Something likeStop-VM -name SomeComputer?