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: Parameters
This 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.dll
Now, 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 + "\Resources\VSWrapperForPSH.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.
Step Three: Create the Virtual Sever COM Instance
Next, we need to create the Virtual Server COM object (instance). To do this use the following code:
$MSVS = new-object -comObject VirtualServer.Application
Step Four: Set the security on the VS COM object
Once 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 VMs
Now 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{
$HostName = $Env:COMPUTERNAME
$URL = "http://$($HostName):1024/VirtualServer/VSWebApp.exe?view=1"
$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, '(?<=&vm=)[^"\r\n]*(?=" )')
# 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.
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:
Publications:
Other Stuff: