Full BackUp of VMs with script RRS feed

  • Question

  • Hi,
    with WindowsServer2003 and VirtualServer2005R2SP1 I created a script, to shutdown the VM's, copy them away and start them. It was a pretty way to full backup the vm's.

    I want to do the same thing with my VM's on WindowsServer2008 with HyperVRC0. The old script doesn't work. Where can I find the new commands to shutdown / start the VM's.

    Has anyone done a script like this?

    Regards, Pascal
    Monday, April 21, 2008 2:57 PM


All replies

  • We've moved away from the COM object model that was present in Virtual Server to a WMI based management model in Hyper-V. Our WMI calls are documented here:




    Tuesday, April 22, 2008 11:46 PM
  • Pascal

    Check my blog here. There are some examples to shutdown VMs through scripting




    • Proposed as answer by Brian Borg Wednesday, September 9, 2009 10:00 PM
    Wednesday, May 14, 2008 9:44 PM
  • Hi Dung,
    thank you for your answer. I'm a newbie in PS-Scripting...
    I've already found your blog, its great.

    But I've got some new problems. It would be great if you could help me.
    I want to read from a list of VMs saved in a textfile. Then check the State of each object (if the state is 2, shutdown the machine, if not, write to log).
    After shutdwon ist complete, my robocopy script should copy the files to my other backup destination. Wait until it has finished copying, the start the machine.

    Here my script, theres an error with my EnabledState - Check I don't understand... (Property 'EnabledState' cannot be found on this object; make sure it exists and is settable.)

     ###START of script###
    #Define var
    $date = Get-Date -Format yyyy/MM/dd
    $log = "C:\scripts\"

    #Get the VMs from File
    Get-Content c:\scripts\VMs.txt | Foreach-Object {
                             #Define var
                              $src = "D:\VirtualServer\" + $_
                              $dst = "\\srv1\_SavesVirtualServers\" + $_ + "\"
                             #Get the VM Object
                              $query = "SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" + $_ + "'"
                              $VM = get-wmiobject -query $query -namespace "root\virtualization" -computername "."
                             #Get the Shutdown Component for the VM
                              $query = "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='" + $VM.name + "'"
                              $Shutdown = get-wmiobject -query $query -namespace "root\virtualization" -computername "."

                              if($Shutdown.EnabledState = '2'){
                                               #Shutdown the machine
                                                  $Result = $Shutdown.InitiateShutdown($true,"Shutdown for FullBackUp")
                                                  # Command to wait until the robocopy has finished...
                                   "Maschine is not in Enabled Status" > $log$date.log
                              robocopy.exe $src $dst /E /NP /TS /R:5 /W:10 /log+Embarrassedlog$date.log
                              #Command to wait until the robocopy has finished...
                            #Start the machine

     ###END of script###

    Regards, Pascal

    Thursday, May 15, 2008 7:26 AM
  • Hi Pascal,

    To check if a VM is running or not, or generlly speaking to check a state of a VM, you should use the EnabledState property of the VM object itself.
    From your script:

    $VM = get-wmiobject -query $query -namespace "root\virtualization" -computername "."


    Then : If ($VM.EnabledState -eq 2)  ....










    Saturday, May 17, 2008 2:20 PM
  • Hi Dung,
    thank you!
    Such a dumb mistake.
    Now it works well. But one things left. Is there a command to wait until the previous command has finished. Or do I have to check it with loops/ifs?

    Regards, Pascal
    Saturday, May 17, 2008 6:56 PM
  • Hi Pascal,

    Were you ever able figure out how to make the robocopy command wait til the vm was shtudown?
    Wednesday, September 9, 2009 9:48 PM
  • Joe, if you don't find the answer here, you could also try The Official Scripting Guys Forum!
    Wednesday, September 9, 2009 10:04 PM