Solved

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

Posted on 2014-12-02
5
383 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 79

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Last article we focus in how to VMware: How to create and use VMs TAGs – Part 1 so before follow this article and perform the next tasks, you should read the first article how to create the TAG before using them in Veeam Backup Jobs.
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.
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…
In this video tutorial I show you the main steps to install and configure  a VMware ESXi6.0 server. The video has my comments as text on the screen and you can pause anytime when needed. Hope this will be helpful. Verify that your hardware and BIO…

770 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