Announcement

Collapse
No announcement yet.

VM (on server) shutdown, copy and start script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VM (on server) shutdown, copy and start script

    I'm looking to create an automated way to shutdown some of my Virtual Machines on some servers, copy the various files and then startup again.

    I've come up with this, but it isn't quite working right. All it seems to be doing is shutting down and restarting.

    The command I'm using is:

    Cscript test.vbs VMname "E:\test\Virtual Machines\VMname\"

    test.vbs

    Code:
    'Script Begins
    On Error Resume Next
     
    'Create Shell Object
    Set objShell = CreateObject ("WScript.Shell")
     
    'Connect to Virtual Server
    Set virtualServer = CreateObject("VirtualServer.Application")
     
    'Get virtual machine from command-line parameter
    set vm = virtualServer.FindVirtualMachine(WScript.Arguments(0))
     
    'shutdown the virtual machine
    set shutdownTask = vm.GuestOS.Shutdown
     
    'Loop waiting for task completion
    while not shutdownTask.isComplete
       WScript.Sleep 1000
    wend
     
    'Copy virtual hard disks and undo disks
    for each vhd in vm.HardDiskConnections
       objShell.Run "%comspec% /c copy " & chr(34) & vhd.undoHardDisk.file & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 , True
       objShell.Run "%comspec% /c copy " & chr(34) & vhd.HardDisk.file & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 , True
    next
    'Copy .VMC and .VSV files
    objShell.Run "%comspec% /c copy " & chr(34) & vm.File & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 , True
    objShell.Run "%comspec% /c copy " & chr(34) & vm.SavedStateFilePath & chr(34) & " " & chr(34) & WScript.Arguments(1) & chr(34),1 , True
     
    'Once everything is done - startup the virtual machine
    vm.Startup
     
    'Script ends
    Any pointers?

    Thanks,

    Mark
    Last edited by Rems; 22nd June 2011, 16:39.

  • #2
    Re: VM (on server) shutdown, copy and start script

    Ok, I've got this script working. The VM shuts down, copies the files but the VM doesn't start. Not sure why?

    Code:
    Option Explicit
    On Error Resume Next
    
    Dim objFSO, objVirtualServer, objVM, objSaveTask, objVHD
    
    'Connect to file system object.
    set objFSO = CreateObject("Scripting.FileSystemObject") 
    
    'Connect to Virtual Server.
    set objVirtualServer = CreateObject("VirtualServer.Application")
    
    'Get virtual machine from command-line parameter.
    set objVM = objVirtualServer.FindVirtualMachine(WScript.Arguments(0))
    
    'Shutdown of the Virtual Machine.
    set objSaveTask = objVM.GuestOS.Shutdown()
    
    'Loop waiting for Virtual Machine shutdown task completion
    while not objSaveTask.isComplete
        WScript.Sleep 1000
    wend
    
    'Copy Virtual hard disks and undo disks.
    for each objVHD in objVM.HardDiskConnections
        If objFSO.FileExists(objVHD.HardDisk.file) Then
            'Wscript.Echo objVHD.HardDisk.file & " " & WScript.Arguments(1)
            objFSO.CopyFile objVHD.HardDisk.file, WScript.Arguments(1)
        End If
        If objFSO.FileExists(objVHD.undoHardDisk.file) Then
            'Wscript.Echo objVHD.undoHardDisk.file & " " & WScript.Arguments(1)
            objFSO.CopyFile objVHD.undoHardDisk.file, WScript.Arguments(1)
        End If
    Next
    
    'Copy .vmc and .vsv files.
    objFSO.CopyFile objVM.File, WScript.Arguments(1)
    objFSO.CopyFile objVM.SavedStateFilePath, WScript.Arguments(1)
    
    'Start the Virtual Machine.
    objVM.GuestOS.Startup()
    Last edited by Rems; 22nd June 2011, 16:42.

    Comment


    • #3
      Re: VM (on server) shutdown, copy and start script

      Sorted.

      Comment


      • #4
        Re: VM (on server) shutdown, copy and start script

        I'm pleased.
        Sure hope noone else needs to do the same thing.
        Please do show your appreciation to those who assist you by leaving Rep Point https://www.petri.com/forums/core/im.../icon_beer.gif

        Comment


        • #5
          Re: VM (on server) shutdown, copy and start script

          Originally posted by tehcamel View Post
          I'm pleased.
          Sure hope noone else needs to do the same thing.
          I'll happily pass on the script if anyone needs it.

          Comment

          Working...
          X