Enable virtual LAN identification

Mar 11, 2010 at 9:36 PM
Edited Mar 17, 2010 at 12:56 AM

The Hyper-V module that you have created is excellent! 

I am working on a project for deploying a few hundred servers, where all Hyper-V virtual servers, required applications, and configurations are automatically preset with scripts.  So far I have almost everything automated, except the “Enable virtual LAN identification” and setting a VLAN ID number for each virtual machine individual network adapter. 

Any help on this topic would be greatly appreciated.

 

Here are some of the scripts that I am using, that some of you might find beneficial.

 

[Install Hyper-V modules (VBS Script)] 

ON ERROR RESUME NEXT
Set objShell = WScript.CreateObject("WScript.Shell")      'To run the shell

FolderName = chr(34) & "c:\Program Files\modules\HyperV" & chr(34)
objShell.Run "%COMSPEC% /c md "&FolderName&"", 2, true      'Create modules\HyperV folders
objShell.Run "%COMSPEC% /c copy d:\scripts\HyperV\HyperV_Install\*.* "&FolderName&"", 2, true 'Copy Hyper-V modules
objShell.Run "%COMSPEC% /c regedit.exe /s d:\scripts\HyperV\PS_Console.REG", 2, true  'Update registry

 

[CMD script that calls the Configure_VHD.ps1]

start %windir%\System32\WindowsPowerShell\v1.0\powershell.exe -noExit -Command "Set-ExecutionPolicy bypass -force ; Import-Module 'c:\program files\modules\Hyperv\Hyperv.psd1' -force ; D:\scripts\Configure_VHD.ps1"

 

 [Configure_VHD.ps1 script]

#===============SERVER -1-=============== START

 

 

$vmVirtualSwitch1 = "Network for VLAN 1"

 

# Create virtual switch VLAN 1

New-VMExternalSwitch -VirtualSwitchName $vmVirtualSwitch1 -ext "VLAN 1:NIC TEAM" -force

# Declare variables

 

$vmName = "BLAH1"

$vhdPath_C = "D:\BLAH_C.vhd"

$vhdPath_D = "D:\BLAH_D.vhd"

 

# Create a new virtual machine

New-VM $vmName

 

# Set memory size

Set-VMMemory -vm $vmName -memory 8GB

 

# Add a default VMBus (non-legacy) network adapter

Add-VMNIC -vm $vmName -VirtualSwitch $vmVirtualSwitch1

 

# Add hard drives

Add-VMDisk -vm $vmName -controllerID 0 0 -vhdPath $vhdPath_C

Add-VMDisk -vm $vmName -controllerID 0 1 -vhdPath $vhdPath_D

 #Start VM

start-vm $vmName

#===============SERVER -1-=============== END

Mar 17, 2010 at 3:52 PM

 

       I figured it out.  Here is the command that did the trick.

 

      get-VMnic -VM "Server Name" | Set-VMNICVLAN -VLANID "1"              (example for VLAN 1)

Feb 27, 2012 at 3:06 PM

I am having difficulties getting this to work with a VM that has more than one vnic.

$var = get-vmnic "server"

$var[0] | set-vmnicvlan -vlanid "12"

Get-WmiObject : Missing an argument for parameter 'Namespace'. Specify a parameter of type 'System.String' and try again.
At C:\Program Files\modules\HyperV\Network.ps1:524 char:72
+                                foreach-object {Get-WmiObject -Namespace <<<<  -ComputerName $Nic.__Server $HyperVName
space -q "associators of {$_} where assocClass=MSVM_NetWorkElementSettingData"}
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Property 'accessVlan' cannot be found on this object; make sure it exists and is settable.
At C:\Program Files\modules\HyperV\Network.ps1:525 char:29
+                $vlanSetting. <<<< accessVlan = $vlanID ; [wmi]$vlanSetting.put().path
    + CategoryInfo          : InvalidOperation: (accessVlan:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Program Files\modules\HyperV\Network.ps1:525 char:73
+                $vlanSetting.accessVlan = $vlanID ; [wmi]$vlanSetting.put <<<< ().path
    + CategoryInfo          : InvalidOperation: (put:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Feb 28, 2012 at 1:23 AM

Try calling it with the VM name and VM Host servernam:

 

$vmnics = Get-VMNIC -VM yourvirtualservername -Server YourVMHostserver

Feb 28, 2012 at 1:42 AM
Edited Feb 28, 2012 at 1:54 AM

Fuck if I can figure out how to make this work for my server though. I have two adapters, one VMbus and one Legacy.  I can't figure out how to get this tool to update it. I have tried everything.  What am I doing wrong:

 

      $vmnics = Get-VMNIC -VM $global:VMName -Server $global:VMHost -legacy -VMbus

      foreach ( $vmnic in $vmnics )
      {
            get-VMnic -VM $global:VMName -Server $global:VMHost | Set-VMNICVLAN -VLANID "1"
      }

 

This piece of shit says it needs a [<System.Management.ManagementObject>]???  What the hell is that? 

Feb 28, 2012 at 5:44 PM

Hello? Does anyone actually support this tool? 

Feb 28, 2012 at 9:49 PM
Edited Feb 28, 2012 at 9:50 PM

Well, here ya go.  Apparently the developers don't actually test their own code. In PSHyperV release R2 SP1, the problem is with the file Network.ps1.  If you open it up and edit line 524 from:

foreach-object {Get-WmiObject -Namespace -ComputerName $Nic.__Server $HyperVNamespace -q "associators of {$_} where assocClass=MSVM_NetWorkElementSettingData"}

to

foreach-object {Get-WmiObject -Namespace $hyperVNamespace -ComputerName $Nic.__Server -q "associators of {$_} where assocClass=MSVM_NetWorkElementSettingData"}

 

viola, it works.

Feb 29, 2012 at 4:29 PM

Thank you very much.  I know that the developer worked on this a couple years ago - I don't know if he is still active on this string anymore.  But, I know that if he is aware of this, he is the sort of guy that would definitely want to fix it.  My guess is he simply didn't test it for the particular situation where a VM has multiple NICs on different VLANs.  I did send him a note telling him there were some problems with this cmdlet.

Again, thanks for figuring this one out.

Feb 29, 2012 at 9:23 PM

Interesting side effect ...

I created a VM definition, just so I had something to play with.  Your fix works fine for External adaptors, but with internal adapters, it assigns the VLAN ID, but it responds with a nice long error.  Evidently the properties for an internal adapter are different than for an external.  Since what I am trying to set up is for external adapters, things will work fine, but it does make me just a tad hesitant.

Property 'DesiredEndpointMode' cannot be found on this object; make sure it exists and is settable.
At C:\Program Files\modules\Hyperv\Network.ps1:520 char:61
+                if ($vlan.DesiredEndpointMode -ne 5 ) {$vlan. <<<< DesiredEndpointMode=5   ; $vlan.put() | out-null }
    + CategoryInfo          : InvalidOperation: (DesiredEndpointMode:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Program Files\modules\Hyperv\Network.ps1:520 char:96
+                if ($vlan.DesiredEndpointMode -ne 5 ) {$vlan.DesiredEndpointMode=5   ; $vlan.put <<<< () | out-null }
    + CategoryInfo          : InvalidOperation: (put:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Get-WmiObject : Invalid object path
At C:\Program Files\modules\Hyperv\Network.ps1:521 char:42
+                $vlanSetting=Get-WmiObject <<<<  -ComputerName $Nic.__Server -Namespace $HyperVNamespace -q "associato
rs of {$vlan} where assocClass=MSVM_NetWorkElementSettingData"
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Property 'TrunkedVLANList' cannot be found on this object; make sure it exists and is settable.
At C:\Program Files\modules\Hyperv\Network.ps1:522 char:85
+                if ($vlanSetting.TrunkedVLANList -notContains $VlanID) {$vlanSetting. <<<< TrunkedVLANList += $vlanID
; $vlanSetting.put() | out-null}
    + CategoryInfo          : InvalidOperation: (TrunkedVLANList:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Program Files\modules\Hyperv\Network.ps1:522 char:130
+                if ($vlanSetting.TrunkedVLANList -notContains $VlanID) {$vlanSetting.TrunkedVLANList += $vlanID ; $vla
nSetting.put <<<< () | out-null}
    + CategoryInfo          : InvalidOperation: (put:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull