Scripting Virtual Server with PowerShell - CAP-ISO.ps1
As promised, attached is the first fo the demo scripts that I used at the “Scripting Virtual Server with PowerShell” session I ran. I`m not going to go through the whole script but here is an over view and bit more detail on a few key sections.
CAP-ISO.ps1 stands for CreateAndPresent-ISO.ps1. This script can be used to create an ISO file comprising of a given set of files. This iso is then presented to a guest VM. I find this particularly useful to run on my laptop with Virtual Server installed. Essentially this is a quick way of getting files from a host to a guest without having to worry about the network setup.
I should probably give this section a more descriptive title, but you get the idea. Here’s some of the more interesting sections of the script.
Firstly, this script just wraps around an existing command line tool to create the ISO. This tool is called OSCDIMG.EXE and ships with the Windows Automated Installation Kit (WAIK). You will need to download the tool, and modify the script to point to its install location. The WAIK can be downloaded here: http://www.microsoft.com/downloads/details.aspx?familyid=C7D4BC6D-15F3-4284-9123-679830D629F2&displaylang=en
Let’s look at some code. I don’t want to duplicate what Virtual PC Guy, Ben Armstrong has already written so for a basic guide to scripting Virtual Server with PowerShell go here: http://blogs.msdn.com/virtual_pc_guy/archive/2006/06/13/630165.aspx
So, as Ben Armstrong discussed, you must first have the dll somewhere on the system, and then must reference it from the script. You also need to be running as admin (and elevated if in Vista). Here is how to reference the dll.
$result = [System.Reflection.Assembly]::LoadFrom(“$profdir\VSWrapperForPSH.dll”)
$profdir is a variable I have created in my profile that points the variable to my profile directory.
I then create a Virtual Server Com Instance like this:
$vs = new-object -com VirtualServer.Application -Strict
The next essential part is to set the security on the object. I do this using a function called setsecurity which can be called anytime. Here’s the function and an example call to it:
$result = [Microsoft.VirtualServer.Interop.PowerShell]::SetSecurity($Object)
$result = SetSecurity $VS
I then have to call this function every time I create a new Virtual Server based object. Whether it’s a VM object, DVD Drive, Hard Disk or any other object I have to set the security once I have created the object.
Have a look through the script and if you have any questions, just post them in the comment section.
For more information on scripting Virtual Server with PowerShell check out Ben Armstrong’s most excellent book, Professional Microsoft Virtual Server 2005, Wrox Publishing. Check out Ben’s blog for more information on the book: