Solved

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

Posted on 2014-12-02
5
355 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 78

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
A procedure for exporting installed hotfix details of remote computers using powershell
In this Micro Tutorial viewers will learn how to restore their server from Bare Metal Backup image created with Windows Server Backup feature. As an example Windows 2012R2 is used.
In this Micro Tutorial viewers will learn how to restore single file or folder from Bare Metal backup image of their system. Tutorial shows how to restore files and folders from system backup. Often it is not needed to restore entire system when onl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now