Check vm vmnic vlan

by Grzegorz Kulikowski

I wanted to get a list of vms that are using specific vlan so i wrote this one:
[sourcecode language=”powershell”]
foreach($vm in (get-vm)){
if (Get-NetworkAdapter -vm $vm.name |?{$_.NetworkName -eq "network23" -or $_.NetworkName -eq "network65"}){$vm.name}
}
[/sourcecode]
Ok, that was working but it was VERY VERY VERY VERY VERY slow…

So i wrote it using get-view :
[sourcecode language=”powershell”]
$vms=get-view -viewtype virtualmachine
foreach($vmview in $vms )
{
$ntwkview=get-view -id $vmview.network
foreach($ntwkentry in $ntwkview){
if ($ntwkentry.name -eq "network23" -or $ntwkentry.name -eq "network65")
{
$vmview.name
}
}
}
[/sourcecode]
This one needed only 3 minutes to complete on a ~1000vm infrastructure. I stop the first script after 30minutes of running as it would probably finish next day …

The script is not perfect… yet 😉 but it will do the job and what’s most important quickly. I hope this will save you some time on reporting.

You may also like

1 comment

suresh May 18, 2018 - 6:25 am

Hello – I tried running your script to get the vla info but getting an error saying get-view is not recognized as the name of cmdlet.

Reply

Leave a Reply

Chinese (Simplified)EnglishFrenchGermanHindiPolishSpanish