共用方式為


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

Comments

  • Anonymous
    April 24, 2007
    I must say that I have been having incredible challenges (without success) in getting Virtual PC networking to function. I have tried comments you have recommended to others but without any luck. In the XP SP 2 environment I would think that VPC 2007 would work fine but nothing appears to enable the guest access to the network. Why is this so difficult and what can you recommend to address this? Of the virtual solution for x86 with which I am working, this is by far the most difficult to get working, yet I would think that Microsoft with Microsoft would be the easiest. Thanks in advance for the comments.

  • Anonymous
    May 18, 2011
    Hi Ben, Really your job is excellent! I have one problem, because I would like to switch the "Network" on one of my "Network Adapters", change the "Network" for other one using a script (PS or VBS). Do you know how I can do it? Regards, David.