Skip Links

Network World

Tyson Kopczynski

Using PowerShell to take Virtual Server snapshots… (Part Three)

By tyson.kopczynski on Sun, 11/04/07 - 12:01pm.
Newsletter Signup

 

This is my third posting in this series.  In the first posting, I described the different components that would be needed for this script.  Then in the second posting, I started explaining how the first part of the script worked.  Now, in this third posting, we will dive into a meatier portion of the script and figure out the code for taking the VM snap shots.  Let's get started!

To kick things off, we first loop through the VMs and pause each one:

foreach ($Server in $Servers){
   # First we need to create an object for the VM itself.
   $VM = $MSVS.findVirtualMachine($Server.Name)
  
   # Like the VS object we need to also set the security on the VM object.
   $Null = [Microsoft.VirtualServer.Interop.Powershell]::SetSecurity($VM) 
  
   # We also need to grab some information where the VMs files are.
   # Note that this script will only work if all the files are located
   # where the VMC file is.
   $Drive = split-path $VM.File -qualifier
   $Path = split-path $VM.File

   # To store the path information we extend the VM's 
   # object with location info (kinda of like a squirrel hiding its nuts!)
   add-member -inputObject $Server -membertype noteProperty `
      -name "Drive" -value $Drive
   add-member -inputObject $Server -membertype noteProperty `
       -name "Path" -value $Path

   # Next we use some logic to see if the VM is running.
   if ($VM.State -eq 5){
      # If the VM is running we need to pause it.
      $Progress = $VM.Save()
      $Null = `
      [Microsoft.VirtualServer.Interop.Powershell]::SetSecurity($Progress)

      # Now for some progress fun!
      while ($Progress.PercentCompleted -lt 100){
         write-progress -Activity "Saving - $($Server.Name)" `
         -Status "Progress:" `
         -PercentComplete ($Progress.PercentCompleted)
            }
      # Need to state if the VM was paused by the script.
         add-member -inputObject $Server -membertype noteProperty `
         -name "Paused" -value "Yes"
         write-progress -Status "Done" -completed $True
         }
      else{
         # Need to state if the VM was not paused by the script.
         add-member -inputObject $Server -membertype noteProperty `
         -name "Paused" -value "No"
         }
   }

Ok, that was a bit of code just to pause the VMs.  The next step is to take a snapshot for each VM:

$Drives = $Servers | select Drive -unique
foreach ($Drive in $Drives){
   write-progress -Activity "Taking shapshot - $($Drive.Drive)" `
   -Status "Stand by..."

   # Here is where we start using vshadow.exe
   $Null = .\Resources\vshadow.exe -script=var -p $Drive.Drive

   # Here is something a little different.  I'm using the script 
   # switch parameter to write shadow copy set ID to a VAR file.
   $ID = get-content var

   # Then using some RegEx magic we strip that information from the file.
   $ID = [RegEx]::Matches($ID, '(?<=SHADOW_ID_1={)[^"\r\n]*(?=})')

   # Next we add that information to the Drive object.
   add-member -inputObject $Drive -membertype noteProperty `
      -name "ID" -value $ID[0].toString()
   # We also delete that var file
   remove-item var
   }

Ok, that's it for this posting.  In my next posting we will finish up the script.  Please remember to mind the formatting.  Also, items in this script are not optimized.  I'm doing this for a reason, one it's easier to read.  Two, I stripped out all of the messaging, error handling, and logging.  My production scripts tend to be a little overkill on handling things.  But, then again... if this becomes a backup solution then it needs to work flawlessly.  With that in mind, if this code is used, you may want to add that stuff back in.

Tags

Copy the vhd....

0

I am seriously confused as why MS does not have a decent backup system in place for Virtual Machines. This is a lot of work to get a snapshot. Ideally, I would just like a script that uses Powershell, pauses a VM, and copies it to a location of my choosing, then resumes the VM. Isnt this a viable solution to backing up a VM? Atleast this way, you have a full VM copy in case of a critical hardware failure.

My script does that...

0

The PowerShell script that I posted does take a snapshot and copy the VHD to a location that you choose. However... yes you are right... there should be a set of cmdlets that make this much easier.

- T

I don't understand why you

0

I don't understand why you use vshadow.exe instead of just making a regular copy with robocopy... can you explain why we take the extra step?

Here is my reply

0

Well, you don’t have to take the extra step if you don’t want to. But, I take the step so that I can minimize the amount of time that the VM is paused. So instead of 10+ mins of downtime (duration of the file copy), the VM is only down for a minute or two. Does this make sense?

- T

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Welcome, visitor. Register Log in
Advertisement:
About Hidden Microsoft

With more than ten years of experience in IT, Tyson Kopczynski has become a specialist in Active Directory, Information Assurance, Windows automation, PKI, and IT security practices. Tyson is also the founding author of the Windows PowerShell Unleashed series and has been a contributing author for such books as Microsoft Internet Security and Acceleration (ISA) Server 2006 Unleashed and Microsoft Windows Server 2008 Unleashed. He has also written many detailed technical papers and guides covering various technologies. As a consultant at Convergent Computing, Tyson works with and provides feedback for next generation Microsoft technologies since their inception and has also played a key role in expanding the automation and security practices at CCO. Tyson also holds such certifications as the Certified Information Systems Security Professional (CISSP), the SANS Security Essentials Certification (GSEC) and SANS Certified Incident Handler (GCIH), and the MCTS (Application Platform, Active Directory, and Network Infrastructure).


Certifications:

  • Certified Information Systems Security Professional (CISSP)
  • SANS Security Essentials Certification (GSEC)
  • SANS Certified Incident Handler (GCIH)
  • MCTS (Application Platform, Active Directory, and Network Infrastructure)
  • Microsoft Certified Systems Engineer (MCSE) Security
  • CompTIA Security+

Publications:


Other Stuff:

  • Blogger NetworkWorld.com from June 2007
  • GIAC Advisory Board from 2009
  • SANS GSEC Local Mentor (a long time ago)
  • CompTIA Security+ SME (a long time ago)
  • Judge, Imagine Cup 2005 Int'l IT Competition
  • Judge, Imagine Cup 2007 Int'l IT Competition