Jump to content

[VBS] Need Script to Pause all VMs on Host


Recommended Posts

Posted

Hi gang, had a little google but mostly mystified by what I've found or it's Powershell, which I've even less talent for than vbs. Which I have zero talent for.

 

Anyways, as per the title, could any passing helpful soul knock me up a script (vbs please) to pause all VMs on a server, or at least something with some dead easy options whereby I can enter the names of both VMs and it'll pause those?

 

Use is for Powerchute UPS to run so that the VMs can be paused in a few moments, then the host shut down, rather than just shut them down from a batch file and wait and wait until they shut down THEN the host and hope the battery lasts for both.

 

Thanking all posts with any help - cheers for looking people. :)

Posted (edited)

This script will allow you to pause a named VM on a named server. Call it from a BAT with the server name as first argument, VM name as second (I've done it this way to make it easily expandable if you add more VMs in the future):

 

Option Explicit
Dim CallWMI
Dim InventoryVMs
Dim ServerName
Dim VMName

'Check arguments
If WScript.Arguments.Length <> 2 Then
WScript.Echo "Usage: CScript PauseVM.vbs ""Server Name"" ""VM Name"""
WScript.Quit
End If

ServerName = WScript.Arguments(0)
VMName = WScript.Arguments(1)

Set CallWMI = GetObject("winmgmts:\\" + ServerName + "\root\virtualization")

Set InventoryVMs = CallWMI.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" & VMName & "'")

InventoryVMs.ItemIndex(0).RequestStateChange(32768)

 

I haven't tested it (can't, I'm not an NM ;)) but the source is here, I just added argument handling: How to use PowerShell or VBScript scripting with Hyper-V

 

To call the script, save it somewhere your BAT can find it and pass it through CScript to direct any error messages to the console, otherwise it'll lock up waiting for you to respond to an error dialog.

 

For example, if the script was saved as PauseVM.vbs and you were trying to pause a VM called "Machine1" on a server called "Server1", you'd put this in your BAT:

 

CScript PauseVM.vbs "Server1" "Machine1"

 

EDIT: missed a line break from the original source, it should work now :)

Edited by LosOjos
Posted

Hang on, unless I am mistaken you want to *pause* a VM before the host shuts down?

 

Paused VMs won't survive a host reboot will they? At least on Hyper-V I am pretty sure they won't.

  • Thanks 1
Posted
Hang on, unless I am mistaken you want to *pause* a VM before the host shuts down?

 

Yep. :(

 

Paused VMs won't survive a host reboot will they? At least on Hyper-V I am pretty sure they won't.

 

They won't? We thought they'd be ok. :( Am I best just shutting them down, then the host down afterwards and hoping the UPS handles it?

Posted

They won't? We thought they'd be ok. :( Am I best just shutting them down, then the host down afterwards and hoping the UPS handles it?

Pausing a VM frees up CPU and disk use but it still holds everything in memory. Pretty sure a reboot would be like pulling the plug on a running system.

A save would be better but then, in my experience, that can take a long time so any benefit could be minimal.

 

I think shutting down is the safest thing to do, of course in an ideal world the UPS would be specced to handled the VM and host shutdown period.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...