Condividi tramite


Back to Basics with Virtual Server Scripting - Part 1

Over the past few weeks, I have had a number of people asking me for some basic control functionality within Virtual Server Scripting. For this I decided to take a number of basic tasks and give you the VB script code to achieve these. So, to start at the beginning:

Notes: All of the scripts are in a similar format. You will notice in some scripts that there is extra code in the scripts to allow you to specify the Virtual Server host and in most cases a choice of outputting results to a text file rather than the screen. The reason I did this will be revealed in a later post where I will put a VB front end to the scripts - watch this space!

I will start at the beginning...

How do you create a new Virtual Machine from a vbscript?

Well, the answer is that there are several components to each virtual machine:

      Virtual Hard Disk (.vhd)

      Virtual Machine Configuration file (.vmc)

      Virtual Network Configuration File (.vnc)

When configuring Virtual Hard Disks, there are a number of scenarios we can leverage:

      Fixed Size Hard Disks; these can be used to assist with performance and allow you to stop your VM's growing beyond the capacity of your storage space

      Dynamic Hard Disks; these are the default and start off life as a 35k file and grow dynamically to the maximum size you specify (default 16Gb). Be aware though that these files can become fragmented on your physical drives - defrag regularly!

      Differencing Disks; These are an awesome way of using a parent vhd with an existing operating system etc and quickly building new machines based on the parent or base configuration. Changes made to the Virtual Machine are saved in to the child or differencing disk. I use this scenario to quickly deploy new machines as they help save on space and can increase performance if you use the parent and child drives on different physical drives / arrays. I normally Sysprep my parent images to enable me to quickly deploy unique machines :-)

      Linked Disks; these were designed for 2 purposes, linking a physical drive to a virtual machine and assisting physical to virtual migration.

Okay, I now want to leverage vbscripts to create virtual hard drives... The first script creates a fixed sized hard disk using the following command: https://msdn.microsoft.com/library/default.asp?url=/library/en-us/msvs/msvs/ivmvirtualserver_createfixedvirtualharddisk.asp

  HRESULT CreateFixedVirtualHardDisk(
  BSTR imagePath ,
  long size ,
  IVMTask** diskTask );

'Start of Script

On Error Resume Next

'This script is used to create fixed size hard disks.

'Get virtual machine from command-line parameter
newvm = WScript.Arguments(0)

'Get virtual hard disk size in Mb from command-line parameter
vhdsize = WScript.Arguments(1)

'Get virtual server host name from command-line parameter
'vmhost = WScript.Arguments(2)
vmhost = "localhost"

'set virtual machine path for creating my new virtual machines
vmPath = "C:\vms\"

'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application", vmhost)

'Create new fixed sized disk and wait for completion
Wscript.Echo "New " & vhdsize & "Mb Fixed Disk being Created: " & vmPath & newvm & "\" & newvm & ".vhd"

set vmTask = virtualServer.CreateFixedVirtualHardDisk(vmPath & newvm & "\" & newvm & ".vhd", vhdsize)
vmTask.WaitForCompletion(-1)

Wscript.Echo "New " & vhdsize & "Mb Fixed Disk Successfully Created: " & vmPath & newvm & "\" & newvm & ".vhd"

'End of Script

--------------------------------------------------------------------------------------

The next script creates dynamic hard disks from the following command: https://msdn.microsoft.com/library/default.asp?url=/library/en-us/msvs/msvs/ivmvirtualserver_createdynamicvirtualharddisk.asp

 HRESULT CreateDynamicVirtualHardDisk( 
  BSTR  imagePath  , 
  long  size  , 
  IVMTask**   diskTask  ); 

'Start of Script

On Error Resume Next

'This script is used to create dynamic hard disks.

'Get virtual machine from command-line parameter
newvm = WScript.Arguments(0)

'Get virtual hard disk size from command-line parameter
vhdsize = WScript.Arguments(1)

'Get virtual server host name from command-line parameter
'vmhost = WScript.Arguments(2)
vmhost = "localhost"

'set virtual machine path for creating my new virtual machines
vmPath = "C:\vms\"

'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application", vmhost)

'Create new dynamic disk and wait for completion
set vmTask = virtualServer.CreateDynamicVirtualHardDisk(vmPath & newvm & "\" & newvm & ".vhd", vhdsize)
vmTask.WaitForCompletion(-1)

Wscript.Echo "New " & vhdsize & "Mb Dynamic Disk Successfully Created: " & vmPath & newvm & "\" & newvm & ".vhd"

'End of Script

--------------------------------------------------------------------------------------

This last script creates a differenceing disk from the following command: https://msdn.microsoft.com/library/default.asp?url=/library/en-us/msvs/msvs/ivmvirtualserver_createdifferencingvirtualharddisk.asp

 HRESULT CreateDifferencingVirtualHardDisk( 
  BSTR  imagePath  , 
  BSTR  parentPath  , 
  IVMTask**   diskTask  ); 

'Start of Script

On Error Resume Next

'This script is used to create differencing disks.

'Get virtual machine from command-line parameter
newvm = WScript.Arguments(0)

'Get virtual machine base image name from command-line parameter
basevm = WScript.Arguments(1)

'Get virtual server host name from command-line parameter
'vmhost = WScript.Arguments(2)
vmhost = "localhost"

'set virtual machine path for creating my new virtual machines
vmPath = "C:\vms\"

'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application", vmhost)

'Create new differencing disk and wait for completion
'note that the base images are held in the '%vmpath%\base\' directory
set vmTask = virtualServer.CreateDifferencingVirtualHardDisk(vmPath & newvm & "\" & newvm & ".vhd", vmPath & "\base\" & basevm)
vmTask.WaitForCompletion(-1)

Wscript.Echo "New Differencing Disk Created: " & vmPath & newvm & "\" & newvm & ".vhd"

'End of Script

Watch this space for more scripts...