// // VirtualSwitchManagementService object. Logical wrapper class for Switch Management Service // function VirtualSwitchManagementService( Server, User, Password ) { // // Define instance fields. // this.m_VirtualizationNamespace = null; this.m_VirtualSwitchManagementService = null; // // Instance methods // VirtualSwitchManagementService.prototype.DeleteInternalEthernetPort = function( InternalEthernetPort ) /*++ Description: Deletes an internal ethernet port Arguments: InternalEthernetPort - Msvm_InternalEthernetPort to delete Return Value: SWbemMethod.OutParameters object. --*/ { var methodName = "DeleteInternalEthernetPort"; var inParams = this.m_VirtualSwitchManagementService.Methods_(methodName).inParameters.SpawnInstance_(); inParams.InternalEthernetPort = InternalEthernetPort.Path_.Path; return this.m_VirtualSwitchManagementService.ExecMethod_(methodName, inParams); } // // Utility functions // VirtualSwitchManagementService.prototype.WaitForNetworkJob = function( OutParams ) /*++ Description: WMI calls will exit with some type of return result. Some will require a little more processing before they are complete. This handles those states after a wmi call. Arguments: OutParams - the parameters returned by the wmi call. Return Value: Status code --*/ { if (OutParams.ReturnValue == 4096) { var jobStateStarting = 3; var jobStateRunning = 4; var jobStateCompleted = 7; var networkJob; do { WScript.Sleep(1000); networkJob = this.m_VirtualizationNamespace.Get(OutParams.Job); } while ((networkJob.JobState == jobStateStarting) || (networkJob.JobState == jobStateRunning)); if (networkJob.JobState != jobStateCompleted) { throw(new Error(networkJob.ErrorCode, networkJob.Description + " failed: " + networkJob.ErrorDescription)); } return networkJob.ErrorCode; } return OutParams.ReturnValue; } // // Aggregate functions // VirtualSwitchManagementService.prototype.DeleteInternalEthernetPortAndWait = function( InternalEthernetPort ) /*++ Description: Deletes an internal ethernet port Arguments: InternalEthernetPort - Msvm_InternalEthernetPort to delete Return Value: SWbemMethod.OutParameters object. --*/ { var outParams = this.DeleteInternalEthernetPort(InternalEthernetPort); var wmiRetValue = this.WaitForNetworkJob(outParams); if (wmiRetValue != 0) { throw(new Error(wmiRetValue, "DeleteInternalEthernetPortAndWait failed")); } } // // Constructor code // if (Server == null) { Server = WScript.CreateObject("WScript.Network").ComputerName; } // // Set Namespace fields // var locator = new ActiveXObject("WbemScripting.SWbemLocator"); this.m_VirtualizationNamespace = locator.ConnectServer(Server, "root\\virtualization", User, Password); // // Set Msvm_VirtualSwitchManagementService field // var physicalComputerSystem = this.m_VirtualizationNamespace.Get( "Msvm_ComputerSystem.CreationClassName='Msvm_ComputerSystem',Name='" + Server + "'"); var list = physicalComputerSystem.Associators_( "Msvm_HostedService", "Msvm_VirtualSwitchManagementService", "Dependent"); this.m_VirtualSwitchManagementService = list.ItemIndex(0) } // // main // var wshShell = WScript.CreateObject("WScript.Shell"); var g_NvspWmi = null; var g_CimV2 = null; Main(); function Main() { g_NvspWmi = new VirtualSwitchManagementService(); WScript.Echo("Looking for root\\cimv2..."); var locator = new ActiveXObject("WbemScripting.SWbemLocator"); g_CimV2 = locator.ConnectServer("", "root\\cimv2", "", ""); WScript.Echo(""); WScript.Echo("Looking for internal (parent) virtual nics..."); var list = g_NvspWmi.m_VirtualizationNamespace.ExecQuery("SELECT * FROM Msvm_InternalEthernetPort"); for (i = 0; i < list.Count; i++) { var next = list.ItemIndex(i); // find correpsonding Win32_NetworkAdapter var adapters = g_CimV2.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE GUID='" + next.DeviceID + "'"); for (j = 0; j < adapters.Count; j++) { var adapter = adapters.ItemIndex(j); if (adapter.NetEnabled == false) { WScript.echo("Deleting '" + next.ElementName + "' because it is disabled."); g_NvspWmi.DeleteInternalEthernetPortAndWait(next); } else { WScript.echo("Not deleting '" + next.ElementName + "' because it is enabled."); } } } WScript.Echo(""); WScript.Echo("Finished!"); }