GET-IT: TEAMS DAY | 1-Day Free Virtual Conference all about Teams. Here on Petri.com - 8/12/20 GET-IT: TEAMS DAY - 8/12/20

VM (on server) shutdown, copy and start script

Home Forums Scripting General Scripting VM (on server) shutdown, copy and start script

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    marcopolo
    Member
    #155061

    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:testVirtual MachinesVMname”

    test.vbs

    ‘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[/CODE]
    Any pointers?

    Thanks,

    Mark[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[/CODE]
    Any pointers?

    Thanks,

    Mark

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.