Creating and Connecting a Virtual Network with VBScript
Okay - so this week I have a little VBScript to create and connect a virtual network under Virtual Server. This script asks for the name of the new virtual network, then asks the user to select a physical network adapter to connect the virtual network to, and finally creates and configures the virtual network.
Option Explicit
dim vs, newVNetName, counter, hostNics, hostNic, hostNicNumber, newVNet
'Connect to Virtual Server
Set vs = CreateObject("VirtualServer.Application")
'Get name for new virtual network
wscript.echo "Please enter the name of the new virtual network to create:"
newVNetName = wscript.StdIn.ReadLine
wscript.echo
'Display list of network adapters on the host
wscript.echo "Available physical network adapters:"
wscript.echo "===================================="
hostNics = vs.HostInfo.NetworkAdapters
counter = 0
for each hostNic in hostNics
wscript.echo cstr(counter) + ": " + hostNic
counter = counter + 1
next
wscript.echo cstr(counter) + ": No host connection"
wscript.echo
'Get a physical network adapter
wscript.echo "Please enter the number for the physical network adapter to use:"
hostNicNumber = wscript.StdIn.ReadLine
wscript.echo
'Check for valid input
if (cint(hostNicNumber) < 0) or (cint(hostNicNumber) > counter) then
wscript.echo "Invalid number specified"
wscript.quit
end if
'Create new virtual network in default location
set newVNet = vs.CreateVirtualNetwork(newVNetName, vs.DefaultVNConfigurationPath)
'Connect new virtual network if appropriate
if cint(hostNicNumber) < counter then
newVNet.HostAdapter = hostNics(hostNicNumber)
end if
wscript.echo "A new virtual network has been created"
A couple of interesting things to notice about this script:
- When attaching a virtual network to a physical network adapter, you need to be able to provide the identification string for the network card. This is a bit annoying, but you can get this information from vs.HostInfo.NetworkAdapters (which returns a collection of strings). If you want you can also just use vs.HostInfo.NetworkAdapters(0) to connect to the first physical network adapter in the computer (yes, the collection is 0 indexed).
- You can't specify anything when creating a virtual network, so you need to configure the virtual network after creating it.
- When creating a new virtual network you need to specify the path for the new virtual network. This is kind of odd - as all virtual networks are created in the same location, and there is no way for you to change this default location - but you can grab this path from vs.DefaultVNConfigurationPath
Cheers,
Ben