User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

Article Index


The Auto Start/Stop Script

The following PowerShell script is for starting and stopping VMs, which will be called by the Task Scheduler at specific events. Some variables at the beginning of the script must be set according to how your host system is set up.

  • $vmrun_path: the full path of the VMrun.exe program location.
  • $auto_vms_path: the full path of the folder containing your virtual machines.
  • $start_delay: how many seconds delay between starting VMs.
  • $stop_delay: how many seconds delay between stopping or suspending VMs.

The function Get-AutoVMs is one of the primary work horses in this script. It finds all the VMs which should be controlled in the location specified by $auto_vms_path, and return them in a sorted order according to the +autovm.nn (where nn is the starting order number) filename in each VM's folder.

For example, a VM with a file +autovm.03 in its folder will start before a VM with a file +autovm.05. The stopping/suspending order will be exactly the reverse of the starting sequence. To disable a VM from auto starting/stopping, you simply delete the +autovm.nn file or change it to something like -autovm.nn, for instance.

The parts of script that does actual work to control VMs are the 2 foreach loops, one for starting them and the other for stopping/suspending them. The program VMrun.exe is utilized in those loops. To start a VM, the code 

& "$vmrun_path" -T ws start "$vm" nogui

is used where nogui is specified because it will run at background without a GUI. For stopping and suspending a VM, the script uses

& "$vmrun_path" -T ws $command "$vm" $option

where $command can be 'stop' or 'suspend' and $option can be 'hard' or 'soft'. With option 'soft', VMware will ask the guest system to run corresponding script first. With option 'hard', the guest system will not be given a chance to run its script.

  1. # Get script arguments
  2. # $command: start, stop or suspend
  3. # $option: hard or soft when $command is stop or suspend
  4. param ([string] $command, [string] $option = "soft")
  5.  
  6. ## defaults
  7. # location of vmrun.exe
  8. $vmrun_path = "${Env:ProgramFiles(x86)}\VMware\VMware VIX\VMrun.exe"
  9. # location of virtual machine directory
  10. $auto_vms_path = "E:\Virtual Machines"
  11. # delay interval (in seconds) for starting up and stopping VMs
  12. $start_delay = 90
  13. $stop_delay = 30
  14.  
  15. ## functions
  16. # Get VMs to be auto-started at $path sorted by +autovms.xx name.
  17. function Get-AutoVMs($path) {
  18. Get-ChildItem "$path\*\+autovm.*" | Sort-Object Name `
  19. | ForEach-Object {$_.DirectoryName+"\*.vmx"} | Get-ChildItem `
  20. | ForEach-Object {$_.FullName}
  21. }
  22.  
  23. # Get running VMs
  24. # NOTE: this only works this VMs started in the same session, so we can't use it here.
  25. function Get-RunningVMs() {
  26. & "$vmrun_path" -T ws list | Where-Object {$_.EndsWith(".vmx")} `
  27. | Sort-Object -Descending
  28. }
  29.  
  30. ## main program
  31. if ($command.ToLower() -eq "start") {
  32. # Retrieve VMs to be auto-start
  33. $auto_vms = @(Get-AutoVMs $auto_vms_path)
  34.  
  35. # Start each VM in the list
  36. foreach ($vm in $auto_vms) {
  37. & "$vmrun_path" -T ws start "$vm" nogui
  38. # Delay a little while.
  39. if ($vm -ne $auto_vms[-1])
  40. { Start-Sleep -Seconds $start_delay }
  41. }
  42.  
  43. exit 10
  44.  
  45. } elseif ((("stop", "suspend") -contains $command.ToLower()) `
  46. -and (("soft", "hard") -contains $option.ToLower())) {
  47. # Retrieve VMs, and reverse its order.
  48. #$auto_vms = @(Get-RunningVMs)
  49. $auto_vms = @(Get-AutoVMs $auto_vms_path)
  50. [Array]::Reverse($auto_vms)
  51.  
  52. # Suspend each VM in the running list.
  53. foreach ($vm in $auto_vms) {
  54. & "$vmrun_path" -T ws $command "$vm" $option
  55. # Delay a little while.
  56. if ($vm -ne $auto_vms[-1])
  57. { Start-Sleep -Seconds $stop_delay }
  58. }
  59.  
  60. exit 20
  61. }
  62.  
  63. exit 1
FaLang translation system by Faboba