Last active
December 18, 2021 20:03
-
-
Save kapb14/3a0b1595b072ca33a1fdd159d1c90582 to your computer and use it in GitHub Desktop.
Get vSphere VM VLANs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# для каждого VMHost-а | |
$var = ForEach ($dstHost in $(Get-VMHost)) | |
{ | |
# для каждой включенной виртуалки | |
ForEach ($vm in $($dstHost | Get-VM | Where-Object -Property PowerState -EQ "PoweredOn")) | |
{ | |
# найдем портгруппу | |
$vmVlan = $vm | Get-VirtualPortGroup | |
# если у виртуалки больше одного порта, создаём объект на каждый | |
if (($vmVlan | Measure-Object).Count -gt 1) | |
{ | |
ForEach ($vlan in $vmVlan) | |
{ | |
[pscustomobject]@{ | |
vmHost = $vm.Host | |
vmVlanId = $vlan.VLanId | |
vmName = $vm.Name | |
} | |
} | |
} | |
else | |
{ | |
[pscustomobject]@{ | |
vmHost = $vm.Host | |
vmVlanId = $vmVlan.VLanId | |
vmName = $vm.Name | |
} | |
} | |
} | |
} | |
$var | |
# экспортнём всё это в наглядную Excel табличку | |
$var | Export-Excel -Path C:\temp\vmvlans.xlsx ` | |
-Title "список включеных виртуалок и вланов которые они используют" ` | |
-TableName "ActiveVmVlans" ` | |
-WorksheetName "ActiveVmVlans" ` | |
-AutoSize ` | |
-Show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment