Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programming Hyper-V Creation using WMI and VB.net

Status
Not open for further replies.

coffeysm

MIS
Oct 7, 2004
571
0
0
US
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.

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)
 
Nevermind I figured out what I was doing wrong and everything is working fine now. I was declaring my array wrong and needed it actually read:

Dim NewGUID() As String = {"{" & System.Guid.NewGuid.ToString() & "}"}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top