Hey all,
I am looking to make a script to output a report that shows if any VMs have invalid network backings or just blank network labels. We have had some issues in the past where VMs have trouble and come back online and either have invalid or nothing in the network field and we don't find them fast enough. If I had a report this might save the day.
Thanks all!
How would you determine an invalid networkname ?
Just by looking at the list of all available portgroups ?
If yes, you might do something like this
$pgs = Get-VirtualPortGroup | Select -ExpandProperty Name
Get-VM | where{$pgVM = Get-NetworkAdapter -VM $_ | Select -ExpandProperty NetworkName;
$pgs -notcontains $pgVM -or $pgVM -eq $null -or $pgVM -eq ''} |
Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I would determine if it is invalid if it shows a greyed out network label and says invalid. The script you have there just outputted all my VMs names, not even their portgroups. Not sure where the mix up is, but thank you for giving me a shout!
Can you try like this ?
Btw, do your VMs have more than 1 vNIC ?
$pgs = Get-VirtualPortGroup | Select -ExpandProperty Name
foreach($vm in Get-VM){
$pgVM = Get-NetworkAdapter -VM $vm | select -ExpandProperty NetworkName
if($pgs -notcontains $pgVM){
Select @{N='VM';E={$vm.Name}},@{N='Portgroup';E={$pgVM}}
}
}
You should also check what is in $pgs.
That should contain the names of all the Portgroups in your environment (to which you are connected).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That outputs nothing...but that might be correct as there are no invalid backings now.
And can you simulate an invalid backing ?
Just to test
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Great, so what does the following return ?
Get-VirtualPortGroup -Name Corp_130
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That would mean it's not in the $pgs array, and the VM with that portgroup should show up from the report.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
We are using D switches so that might be a reason.
Is that portgroup returned by Get-VDPortgroup ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It does show with that command.
Give this one a try
$pgs = @()
$pgs += Get-VirtualPortGroup | Select -ExpandProperty Name
$pgs += Get-VDPortgroup | select -ExpandProperty Name
Get-VM | Get-NetworkAdapter | where{$pgs -notcontains $_.NetworkName} |
Select @{N='VM';E={$_.Parent.Name}},NetworkName
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference