Solved

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

Posted on 2014-12-02
5
428 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 82

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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

If your vDisk VHD file gets deleted from the image store accidentally or on purpose, you won't be able to remove the vDisk from the PVS console. There is a known workaround that is solid.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…

632 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