Tuesday, March 6, 2012

Applying SCVMM 2012 Logical network assignment with PowerShell

This is a little script that I needed to write in order to quickly set up a test environment of mine.

One thing that I am going to do here is contrast what I did with what the SCVMM view script gave me.

The known issues at hand:

I have a logical network.  I have a large number of hosts that all need to be associated with this logical network.  I have to find the correct NIC (as a logical network is associated with the hardware NIC, not with a virtual network, so it is always an External Virtual Network).

Here is what I have:

$vmHost = Get-SCVMHost

foreach ($e in $vmHost) {
    $vmHostNetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost $e | where {$_.IPAddresses -match "10.233.40"}

    $logicalNetwork = Get-SCLogicalNetwork -Name "Shared"
   
    $JobGroupID = [System.Guid]::NewGuid().ToString()

    Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $vmHostNetworkAdapter -AddOrSetLogicalNetwork $logicalNetwork -JobGroup $JobGroupID

    Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $vmHostNetworkAdapter -AvailableForPlacement $true  -JobGroup $JobGroupID

    Set-SCVMHost -VMHost $e -JobGroup $JobGroupID -RunAsynchronously
}

Breaking it down:

  • I get the all the host objects.
  • I query for the physical NIC, selecting on the IP address that it has (I want the logical network in this IP range).
  • I get the logical network object. (previously created manually)
  • Then I assign the Logical network to the NIC.
  • I also enable placement.

I could have selected on different parameters to find the proper NIC of the Hyper-V Server to create the Logical Network on.

A particular SCVMHostNetworkAdapter has the following properties:

Name                           : Intel(R) 82579LM Gigabit Network Connection
IPAddresses                    : {10.233.40.156, fe80::b428:524d:463:3ce0}
DHCPEnabled                    : True
IPSubnets                      : {10.233.40.0/21, fe80::/64}
DefaultIPGateways              : {10.233.40.1}
MacAddress                     : 18:03:73:D2:53:4C
PhysicalAddress                : 18:03:73:D2:53:4C
NetworkLocation                : Shared
VirtualNetwork                 : Shared
VLanTags                       : {0}
UsableVLanTags                 : {0}
VLanMode                       : Access
DesiredVLanMode                : Access
VLanEnabled                    : False
ConnectionState                : Connected
ConnectionName                 : Local Area Connection 3
Description                    :
PrimaryDNSSuffix               :
MaxBandwidth                   : 1000
VMHost                         : threel5.pvs.brianeh.local
LogicalNetworkMap              : {[Shared, System.Collections.Generic.List`1[Microsoft.SystemCenter.VirtualMachineManager.SubnetVLan]]}
UsableVlanMode                 : Access
ModelAccessibleSubnetVLans     : {[Shared, System.Collections.Generic.List`1[Microsoft.SystemCenter.VirtualMachineManager.SubnetVLan]]}
LogicalNetworks                : {Shared}
SubnetVLans                    : {10.233.40.0/24}
UnassignedVLans                : {}
AvailableForPlacement          : True
UsedForManagement              : True
LogicalNetworkCompliance       : Compliant
LogicalNetworkComplianceErrors : {}
BDFLocationInformation         : PCI bus 0, device 25, function 0
ServerConnection               : Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerConnection
ID                             : fcb44659-06ca-44e8-9932-c9488897437a
IsViewOnly                     : False
ObjectType                     : VMHostNetworkAdapter
MarkedForDeletion              : False
IsFullyCached                  : True

For me it made sense to select on the following:

IPAddresses : {10.233.40.156, fe80::b428:524d:463:3ce0}

or

IPSubnets : {10.233.40.0/21, fe80::/64}

But you could select on any of the properties of the object.  A bit of background;

In SCVMM the Logical Network is associated with a physical NIC. For the same reason that I selected on the IP. It is a way to tie the VM to the physical topology of your network. So you always end up on the same subnet.

This gets around historic Windows behavior of not always ordering the NICs the same as they are in the chassis. You never can guarantee a relationship between the physical and the ordering in the OS.

I mentioned at the top that I would also look at what the SCVMM View Script button in the wizard gave me.  I have always been critical of how verbose the View Script option is of SCVMM wizards and this is no exception.  The primary reason that I am critical is that it is hard to consume and turn into something universal, especially if you are not familiar with PowerShell.

The thing to pay attention to is where you are setting parameters of an object that already has these settings applied.  In other words, wasting cycles re-setting the exact same value.

Here is what SCVMM gave me with ‘View Script’:

$vmHost = Get-SCVMHost -ID "dcbcaa95-7627-45a1-afbe-69ff970e40ea" -ComputerName "threeb1"

$vmHostNetworkAdapter = Get-SCVMHostNetworkAdapter -Name "Intel(R) 82579LM Gigabit Network Connection" -VMHost $vmHost

# Shared
$logicalNetwork = Get-SCLogicalNetwork -ID "cee53020-cece-48f4-afea-496030292466"
Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $vmHostNetworkAdapter -AddOrSetLogicalNetwork $logicalNetwork -JobGroup "b5856f43-6539-4d68-9a0f-5ebe1241499a"

Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $vmHostNetworkAdapter -Description "" -JobGroup "b5856f43-6539-4d68-9a0f-5ebe1241499a" -VLanMode "Access" -AvailableForPlacement $true -UsedForManagement $true


Set-SCVMHost -VMHost $vmHost -JobGroup "b5856f43-6539-4d68-9a0f-5ebe1241499a" -RunAsynchronously

Breaking it down:

  • The host (hypervisor) is selected using its ID - this is the only way to guarantee you get the right host, and only one host. And is commonly used in this precise way.
  • The host network adapter is selected by name - if you spell out the entire adapter name this works, if you rename your adapters this works. Considering that there is no correlation between the NIC and the patch panel I am going with the IP range or subnet.
  • The Logical network is queried.
  • The Host network adapter is modified to apply the setting of the logical network, then it is again set to apply a description, set the vlan mode and enable for placement.
  • In my case I had already set the VLAN mode and the placement flag was on, why set them again..
  • Then the entire change job is applied to the host.
  • You will notice a JobGroup in here. A JobGroup in SCVMM is an array of commands that are executed in order from 0 to the end. Some cmdlets support it, some do not, no you need to check your cmdlets.

No comments: