Sending custom parameters into virtual machines under Virtual Server

In a previous post (http://blogs.msdn.com/virtual_pc_guy/archive/2005/01/24/359650.aspx) – I discussed how Virtual PC / Virtual Server provide basic information about the host computer in the registry of the guest operating system.  Virtual Server extends this concept and allows users to specify custom parameters to be created in the guest operating system.  This is a very useful ‘one way’ communication channel – and is helpful in most situations where you want to run automated processes inside of the virtual machine.  Here is a very simple VBScript that takes three command line parameters – and uses them to create a custom entry in the registry of the guest operating system:

‘ || Script begins

‘ Connect to Virtual Server

Set virtualServer = CreateObject(“VirtualServer.Application”)

‘ Get virtual machine from the first command-line parameter
Set vm = virtualServer.FindVirtualMachine(WScript.Arguments(0))

‘ Set a registry parameter in the guest operating (where the key name is the
‘ second command-line parameter and the data stored in the key is the third
‘ command-line paramater

Result = vm.GuestOS.SetParameter(WScript.Arguments(1), WScript.Arguments(2))

‘ || Script ends

Once you have done this – you can check the value inside of the guest operating system with this script:

‘ || Script begins

‘ Setup constant

const HKEY_LOCAL_MACHINE = &H80000002

‘ Setup registry object (this is a single line)
Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\.rootdefault:StdRegProv”)

‘ Set the key path and values to look at
strKeyPath = “SOFTWAREMicrosoftVirtual MachineGuestParameters”
strValueName1 = WScript.Arguments(0)

‘ Get the values from the registry
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName1, dwValue1

‘ Display the results
WScript.Echo “The value for ” & WScript.Arguments(0) & ” is: ” & dwValue1

‘ || Script ends

The only downside to this technique is that ‘vm.GuestOS.SetParameter’ can only be called when the virtual machine is actively running and Virtual Machine Additions is loaded.

Cheers,
Ben