Solved

PowerShell script in VMM 2012 R2 to remove "Unassigned" logical networks from a host.

Posted on 2014-12-02
5
395 Views
Last Modified: 2014-12-08
Folks -

I am in the process of migrating my Hyper-V hosts from VMM 2008 R2 to VMM 2012 R2.  I've noticed that if I have the "Trunk all VLAN IDs" checkbox checked in the VMM 2008 R2 properties of the host, when I remove the host and add it to 2012 R2, I end up with a huge list of unassigned subnets in my logical network list - in addition to my legitimately defined logical networks in the new environment.

I'd like to remove/cleanup all these "Unassigned" definitions (see the screenshot).  Any ideas for a way to do this with PowerShell?

Thanks.

Host Properties
0
Comment
Question by:amendala
  • 4
5 Comments
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 40477274
use powershell
http://bit.ly/1zMNOCM Get-SCLogicalNetworkDefinition

$LogicalNetwork = Get-SCLogicalNetwork "LogicalNetwork01"
PS C:\> Remove-SCLogicalNetwork -LogicalNetwork $LogicalNetwork
http://bit.ly/1zMOpo1

I don't have scvmm available at this time but these 2 commands should get you going something on the line of
Get-SCLogicalNetworkDefinition | where (unassigned = $true} | remove-scLogicalNetwork
0
 

Author Comment

by:amendala
ID: 40477335
They're actually settings attached to network adapters on each host so Get-SCLogicalNetworkDefinition won't do it, that's for a global logical network definition for the entire cloud.

I did find that Get-VMHostNetworkAdapter has a property named "UnassignedVlans".  So what I'm working on is a way to get all of those network adapters that have values there and to purge them.  The Set-VMHostNetworkAdapter cmdlet has a "-RemoveUnassignedVlan" parameter but it requires that you specify each one you wish to remove.  I'd like to say "all".

So I need to figure out how to get the adapters, find the ones that have values in UnassignedVlans, and blank them all.  Must be some way to Foreach it I'd think...
0
 

Author Comment

by:amendala
ID: 40477350
This works for a single VLAN, but now I need to iterate through all the list entries.  As there are thousands, I need to write a loop now of some sort.  It does not accept wildcards.

Get-VMHostNetworkAdapter | Where UnassignedVlans -ne "" | Set-VMHostNetworkAdapter - RemoveUnassignedVlan 1
Get-VMHostNetworkAdapter | Where UnassignedVlans -ne "" | Set-VMHostNetworkAdapter - RemoveUnassignedVlan 2
Get-VMHostNetworkAdapter | Where UnassignedVlans -ne "" | Set-VMHostNetworkAdapter - RemoveUnassignedVlan 3
... etc.
0
 

Accepted Solution

by:
amendala earned 0 total points
ID: 40479015
I ended up taking the time to write up a set of loops to do this...

The following code will loop through all registered hosts and remove any UnassignedVlans from any VMHostNetworkAdapter objects without touching anything else.  It will loop until completion and remove any VLANs as listed in the UnassignedVlans property.

Works perfectly.

Import-Module VirtualMachineManager

# Replace MyVMMServer with your server name.
Get-VMMServer MyVMMServer
$VMHosts = Get-SCVMHost

foreach ($VMHost in $VMHosts)
{
	$vmHostNetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost $VMHost | Where {$_.UnassignedVlans.Count -ne 0}

	if ($vmHostNetworkAdapter -ne $NULL)
	{
		$i = $vmHostNetworkAdapter.UnassignedVlans.Count - 1
		$vlanID = $vmHostNetworkAdapter.UnassignedVlans[$i]

		do
		{
			Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $vmHostNetworkAdapter -RemoveUnassignedVlan $vlanID
			$i--
			$vlanID = $vmHostNetworkAdapter.UnassignedVlans[$i]
		}
		while ($i -ne -1)
	}
}

Open in new window

0
 

Author Closing Comment

by:amendala
ID: 40486393
Correct solution with correct cmdlets.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question