option explicit
dim objWMIService
dim switchService
dim fileSystem
const wmiStarted = 4096
const wmiSuccessful = 0
Main()
'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
dim name, friendlyName, learnableAddress
dim computer, objArgs, createdSwitch
set objArgs = WScript.Arguments
if WScript.Arguments.Count = 3 then
name = objArgs.Unnamed.Item(0)
friendlyName = objArgs.Unnamed.Item(1)
learnableAddress = objArgs.Unnamed.Item(2)
else
WScript.Echo "usage: cscript CreateSwitch.vbs name friendlyName learnableAddress"
WScript.Echo "Example: CreateSwitch FirstSwitch ""My First Switch"" 1024"
WScript.Quit(1)
end if
set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
computer = "."
set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization")
set switchService = objWMIService.ExecQuery("select * from Msvm_VirtualSwitchManagementService").ItemIndex(0)
set createdSwitch = CreateSwitch(name, friendlyName, learnableAddress)
if createdSwitch Is Nothing then
WriteLog "CreateSwitch failed."
WScript.Quit(1)
else
WriteLog "Done"
WScript.Quit(0)
end if
End Sub
'-----------------------------------------------------------------
' Create a virtual switch by calling CreateSwitch WMI method
'-----------------------------------------------------------------
Function CreateSwitch(name, friendlyName, learnableAddress)
dim objInParam, objOutParams
set CreateSwitch = Nothing
set objInParam = switchService.Methods_("CreateSwitch").InParameters.SpawnInstance_()
objInParam.FriendlyName = friendlyName
objInParam.Name = name
objInParam.NumLearnableAddresses = learnableAddress
objInParam.ScopeofResidence = null
set objOutParams = switchService.ExecMethod_("CreateSwitch", objInParam)
if objOutParams.ReturnValue = wmiSuccessful then
set CreateSwitch = objWMIService.Get(objOutParams.CreatedVirtualSwitch)
else
WriteLog Format1("CreateSwitch failed with error code {0}", objOutParams.ReturnValue)
end if
End Function
'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
dim fileStream
set fileStream = fileSystem.OpenTextFile(".\CreateSwitch.log", 8, true)
WScript.Echo line
fileStream.WriteLine line
fileStream.Close
End Sub
'------------------------------------------------------------------------------
' The string formating functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format1(myString, arg0)
Format1 = Replace(myString, "{0}", arg0)
End Function