Ok, I am working on a program for work creating VMs using WMI with VB.Net primarily as the user interface. I have been pretty successful so far, but have a hit a minor roadblock and was hoping someone can help me out. The following code works under PowerShell 2.0 when adding MS Synthetic Ethernet Port.
I converted it to VB.Net with the following code and I get an error in Event Viewer about error number 14140 and it doesn't add the NIC. I Googled this error and other problems and couldn't find anything helpful.
Code:
$vm = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName='$vmName'"
# Allocate the new ethernet port
$allocationCapabilities = gwmi -namespace root\virtualization Msvm_AllocationCapabilities ` -filter "ResourceType = 10 AND ResourceSubtype = 'Microsoft Synthetic Ethernet Port'"
$allocationPath = $allocationCapabilities.__PATH.replace("\","\\")
$default = gwmi -namespace root\virtualization Msvm_SettingsDefineCapabilities ` -filter "ValueRange = 0 AND GroupComponent = '$allocationPath'"
$synthEth = [wmi]$default.PartComponent
$synthEth.ElementName = $ethPortName
$newGuid = [System.Guid]::NewGuid().Guid
$synthEth.VirtualSystemIdentifiers = @("{$newGuid}")
# Attach the new NIC (ethernet port) to the virtual machine
$vmms = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService
$result = $vmms.AddVirtualSystemResources($vm,@($synthEth.GetText(1)))
I converted it to VB.Net with the following code and I get an error in Event Viewer about error number 14140 and it doesn't add the NIC. I Googled this error and other problems and couldn't find anything helpful.
Code:
'Add NIC
Dim SynthEth
Dim AllocationCapabilities, AllocationPath, DefaultCapabilities
AllocationCapabilities = objWMI.ExecQuery("Select * from Msvm_AllocationCapabilities WHERE ResourceType = 10 AND ResourceSubType = 'Microsoft Synthetic Ethernet Port'").ItemIndex(0)
AllocationPath = AllocationCapabilities.Path_.Path
Dim ModAllocationPath As String = AllocationPath.replace("\", "\\")
DefaultCapabilities = objWMI.ExecQuery("Select * from Msvm_SettingsDefineCapabilities WHERE ValueRange = 0 AND GroupComponent = '" & ModAllocationPath & "'").ItemIndex(0)
SynthEth = objWMI.Get(DefaultCapabilities.PartComponent)
Dim strSynEth(1)
Dim NewGUID(1) As String
NewGUID(0) = "{" & System.Guid.NewGuid.ToString() & "}"
SynthEth.ElementName = strVMName
SynthEth.StaticMacAddress = "false"
SynthEth.VirtualSystemIdentifiers = NewGUID
strSynEth(0) = SynthEth.GetText_(1)
objInParam = VSMS.Methods_("AddVirtualSystemResources").InParameters.SpawnInstance_()
objInParam.ResourceSettingData = strSynEth
objInParam.TargetSystem = objComputerSystem.Path_.Path
objOutParams = VSMS.ExecMethod_("AddVirtualSystemResources", objInParam)