locked
Powershell to start and shutdown hosts in specific order with pause in between RRS feed

  • Question

  • I apoligze in advance that I do not know powershell that well yet. However I am looking for a script that will shutdown a set of Hyper-V VMs in a certain order and another one that will start them in a certain order. I would like a pause in between each one that waits for 2 minutes before starting the next one. I have tried to write the script but I cant seem to get it working so Im posting here. :) Is it possible to have the hosts in two seperate files hypervstart and hypervstop. They are shutdown in a different order than they are started in.

     

    Thanks!

    Tuesday, December 6, 2011 9:32 PM

Answers

All replies

  • Show us what you've got.
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, December 6, 2011 9:33 PM
  • Hi,

    If you have Virtual machine Manager in your environment you can use Start-VM and Shutdown-VM cmdlets from VirtualMachineManager Snap-in. Otherwise you can use this library: http://pshyperv.codeplex.com/.

    Regards!


    http://www.purgar.net
    Tuesday, December 6, 2011 11:06 PM
  • I do this to start VM's, but it does not do them in order - you should be able to modify ths to your specifications easily with a call to Get-Content

     $vms = Get-WmiObject -Namespace root\virtualization  -Query "Select * From Msvm_VirtualSystemSettingData Where notes='autostart'"
    foreach ($vmname in $vms)
    {
     $vm = get-wmiobject -namespace root\virtualization -class Msvm_ComputerSystem -filter " ElementName = `'$($vmname.ElementName)`'"
     if ($vm.enabledstate -ne 2)
     {
      $vm.RequestStateChange(2)
      Start-Sleep 60
     } 
    }

    Karl


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, December 6, 2011 11:07 PM
  • Get-Content VMstart.txt | %{Start-VM $_; Start-Sleep -Seconds 120}
    
    Get-Content VMstop.txt | %{Stop-VM $_; Start-Sleep -Seconds 120}
    

    Here are two one-liners that will do what you are looking for.  Put the list of VMs that you want to start/stop in the order that you want them started or stopped in the appropriate file.
    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Client System Administration tool
    AD User Creation tool
    Wednesday, December 7, 2011 12:29 AM