Solved

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

Posted on 2014-12-02
5
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 81

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Will try to explain how to use the VMware feature TAGs in the VMs and create Veeam Backup Jobs using TAGs. Since this article is too long, I will create second article for the Veeam tasks.
In this Micro Tutorial viewers will learn how to use Windows Server Backup to create full image of their system. Tutorial shows how to install Windows Server Backup Feature on Windows 2012R2 and how to configure scheduled Bare Metal Recovery backup.…
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…

751 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