Check vm vmnic vlan

I wanted to get a list of vms that are using specific vlan so i wrote this one:

foreach($vm in (get-vm)){
if (Get-NetworkAdapter -vm $vm.name |?{$_.NetworkName -eq "network23" -or $_.NetworkName -eq "network65"}){$vm.name}
}

Ok, that was working but it was VERY VERY VERY VERY VERY slow…

So i wrote it using get-view :

$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
}
}
}

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.