$displayRows = @( foreach ($nic in $report) { $vlanLabel = switch ([string]$nic.VlanMode) { 'Untagged' { 'VLAN オフ(タグなし)' } 'Access' { "VLAN $($nic.AccessVlanId)" } 'Trunk' { "Trunk(Native: $($nic.NativeVlanId)、Allowed: $($nic.AllowedVlanIds))" } default { "$($nic.VlanMode)(Primary: $($nic.PrimaryVlanId)、Secondary: $($nic.SecondaryVlanId)、List: $($nic.SecondaryVlanIds))" } } [pscustomobject]@{ SwitchId = $nic.SwitchId SwitchName = $nic.SwitchName IsManagementOS = ([string]$nic.IsManagementOS -eq 'True') ComputerName = $nic.ComputerName VMName = $nic.VMName NICName = $nic.NICName VlanLabel = $vlanLabel } } ) # $reportに含まれる仮想スイッチの一覧 $switches = @( $displayRows | Where-Object { -not [string]::IsNullOrWhiteSpace($_.SwitchName) } | Select-Object SwitchId, SwitchName -Unique | Sort-Object SwitchName ) # 仮想スイッチごとにツリー表示 foreach ($sw in $switches) { Write-Host "[ $($sw.SwitchName) ]" $connected = @( $displayRows | Where-Object { $_.SwitchId -eq $sw.SwitchId } ) $groups = @( $connected | Group-Object -Property VlanLabel | Sort-Object Name ) for ($i = 0; $i -lt $groups.Count; $i++) { $last = $i -eq $groups.Count - 1 $branch = if ($last) { '└ ' } else { '├ ' } $indent = if ($last) { ' ' } else { '│ ' } Write-Host "$branch$($groups[$i].Name)" $rows = @( $groups[$i].Group | Sort-Object @{ Expression = { -not $_.IsManagementOS } }, VMName, NICName ) for ($j = 0; $j -lt $rows.Count; $j++) { $nic = $rows[$j] $owner = if ($nic.IsManagementOS) { "HostOS ($($nic.ComputerName))" } else { $nic.VMName } $nicBranch = if ($j -eq $rows.Count - 1) { '└ ' } else { '├ ' } Write-Host "$indent$nicBranch$owner ─ $($nic.NICName)" } } }