26 March 2015

VMM Powershell Assign Logical Network to VMhost adapters

One step that cannot easily be automated to multiple hosts is assigning the logical network a VM host has been grated access to to a physical adapter on the host.  The following PowerShell allows you to set this.



$logicalNetwork = Get-SCLogicalNetwork -Name "Virtual Machine VLANS"
$vmHostNetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost hyperv02 | where {$_.ConnectionName -eq "VM Traffic"
Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $vmHostNetworkAdapter -AddOrSetLogicalNetwork $logicalNetwork

You can also string this together for multiple network on host group members

You can get the group names with

Get-VMHostGroup | select path | Sort-Object path

And the logical network names with

get-sclogicalnetwork | select name | Sort-Object name

This will give you what you need to complete the $netlist and $members sections


$netlist = "Hyper-V Management","Virtual Machine VLANS", "Core Infrastructure VLANS","F5 VLANS"

foreach ($net in $netlist)
{
$logicalNetwork = Get-SCLogicalNetwork -Name $net
    $members = Get-VMHost | where {$_.VMHostGroup -like "All Hosts\DC2 Data Center\Best - HA EMC"}
    foreach ($M IN $members)
    {
    $vmHostNetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost $m.Name | where {$_.ConnectionName -eq "VM Traffic"
    Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $vmHostNetworkAdapter -AddOrSetLogicalNetwork $logicalNetwork
    }

}


No comments:

Post a Comment