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

Analysis
Oct 28, 20073 mins

Ok… as promised, I’m doing a follow up to my VS snapshots posting last week.  In this posting I will go over the basic code snippets that are needed to construct the VS snapshots script.  To do this, I will match each snippet with a step that was listed in my previous posting.  Let’s get started!

Step One: ParametersThis is simple, and might look like the following:

param([string] $DestPath, [string] $ImportFile)

$DestPath is the path to copy the backups to and $ImportFile will be used to import a list of servers (if needed).  In addition, I might also want to check that values provided are valid:

write-host “Checking Backup Path” -NoNewLine

if (!(test-path $DestPath -pathType Container)){

    write-host `t`t “Is not a valid path!” -Foregroundcolor Red

    write-host

    Break

    }

else{

    write-host `t`t “[OK]” -Foregroundcolor Green

    }

if ($ImportFile){

    write-host “Checking Import File” -NoNewLine

    if (!(test-path $ImportFile -pathType Leaf)){

        write-host `t`t “Is not a valid file!” -Foregroundcolor Red

        write-host

        Break

        }

    else{

        write-host `t`t “[OK]” -Foregroundcolor Green

        }

    }

Step Two: Load the VSWrapperForPSH.dllNow, we need to load the VSWrapperForPSH.dll into the current PowerShell session.  The code for doing this is as follows:

$ScriptPath = split-path -Parent $MyInvocation.MyCommand.Path

    $Null = `

        [System.Reflection.Assembly]::LoadFrom($ScriptPath + “ResourcesVSWrapperForPSH.dll”)

Here the VSWrapperForPSH.dll is loaded using the LoadFrom method found in the System.Reflection.Assembly class.  Also note that the VSWrapperForPSH.dll is located under the Resources folder.

Next, we need to create the Virtual Server COM object (instance).  To do this use the following code:Step Three: Create the Virtual Sever COM Instance

$MSVS = new-object -comObject VirtualServer.Application

Step Four: Set the security on the VS COM objectOnce the VS COM object is created we use the SetSecurity method to set the security on the object (if you fail to do this, you cannot access the object that you just created):

$Null = [Microsoft.VirtualServer.Interop.Powershell]::SetSecurity($MSVS)

Step Five: Get a list of VMsNow we need to get a list of the VMs that will be backed up.  We can either get the list from the $ImportFile:    if ($ImportFile){

        $Script:Servers = import-csv $ImportFile

        }

Or, we can get the list from the Virtual Server itself:

    else{http://$($HostName):1024/VirtualServer/VSWebApp.exe?view=1

        $HostName = $Env:COMPUTERNAME

        $URL = “

        $Webclient = new-object Net.WebClient

        $Webclient.UseDefaultCredentials = $True

        $Data = $Webclient.DownloadString(“$URL”)

The previous code is actually really cool!  The Virtual Server management web site is a good source of information.  So, considering that PowerShell is built on the .NET Framework, we can very easily access just pull information from this web server.

        # This Regex gets a list of server entries from the data returned

        $Servers = [Regex]::Matches($Data, ‘(?

        # There are many duplicates so we need to group them

        # Plus this gives us a better name for our property :>)

        $Script:Servers = $Servers | group Value | select Name

        }

Ok… that’s enough for tonight.  Posting source code is a pain through the NetworkWorld blog.  I will finish this up early next week.  If you have questions or comments please let me know.  BTW – mind the formatting.