+ Post New Thread
Results 1 to 6 of 6
Scripts Thread, [VBS] Need Script to Pause all VMs on Host in Coding and Web Development; Hi gang, had a little google but mostly mystified by what I've found or it's Powershell, which I've even less ...
  1. #1
    Miscbrah's Avatar
    Join Date
    Dec 2011
    Posts
    780
    Thank Post
    194
    Thanked 189 Times in 119 Posts
    Rep Power
    97

    [VBS] Need Script to Pause all VMs on Host

    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.

  2. #2

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,182
    Thank Post
    1,285
    Thanked 1,029 Times in 729 Posts
    Rep Power
    658
    I'm assuming that the VMs are running on Hyper-V?

  3. Thanks to LosOjos from:

    Miscbrah (30th January 2014)

  4. #3

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,182
    Thank Post
    1,285
    Thanked 1,029 Times in 729 Posts
    Rep Power
    658
    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):

    Code:
    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:

    Code:
    CScript PauseVM.vbs "Server1" "Machine1"
    EDIT: missed a line break from the original source, it should work now
    Last edited by LosOjos; 30th January 2014 at 12:56 PM.

  5. #4

    sparkeh's Avatar
    Join Date
    May 2007
    Posts
    6,259
    Thank Post
    1,138
    Thanked 1,463 Times in 980 Posts
    Blog Entries
    22
    Rep Power
    457
    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.

  6. Thanks to sparkeh from:

    Miscbrah (30th January 2014)

  7. #5
    Miscbrah's Avatar
    Join Date
    Dec 2011
    Posts
    780
    Thank Post
    194
    Thanked 189 Times in 119 Posts
    Rep Power
    97
    Quote Originally Posted by sparkeh View Post
    Hang on, unless I am mistaken you want to *pause* a VM before the host shuts down?
    Yep.

    Quote Originally Posted by sparkeh View Post
    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?

  8. #6

    sparkeh's Avatar
    Join Date
    May 2007
    Posts
    6,259
    Thank Post
    1,138
    Thanked 1,463 Times in 980 Posts
    Blog Entries
    22
    Rep Power
    457
    Quote Originally Posted by Miscbrah View Post
    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.

SHARE:
+ Post New Thread

Similar Threads

  1. Script to Defrag all drives on log on
    By nephilim in forum Scripts
    Replies: 1
    Last Post: 5th October 2010, 11:51 AM
  2. Script to get all file on a url
    By Midget in forum Scripts
    Replies: 5
    Last Post: 23rd January 2007, 01:10 PM
  3. Replies: 4
    Last Post: 23rd November 2006, 09:33 PM
  4. Replies: 3
    Last Post: 25th September 2006, 12:27 PM
  5. Replies: 7
    Last Post: 27th November 2005, 10:41 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •