VMware Cloud Community
GuruAnt
Contributor
Contributor
Jump to solution

Get-VirtualPortGroup - Trying to get only port groups attached to one switch.

I've got the following as part of a script I'm working on. I'm wanting to list all the port groups on the VMSwitch on all servers.

ForEach($objHost in $ObjAllHosts){
$strVSwitch = Get-Virtualswitch -VMHost (Get-VMHost $objHost) | where-object { $_.Name -match "VMswitch" }
$objPortGroups = Get-VirtualPortGroup -VMHost (Get-VMHost $objHost) -VirtualSwitch $strVSwitch | Sort-Object -Property Name
}

The problem is that this doesn't seem to populate the $objPortGroups variable. Dot-sourcing, and investigating further, I saw that:

Get-VirtualPortGroup -VMHost (Get-VMHost $objHost) -VirtualSwitch $strVSwitch

Gives me a list of all the port groups, on all the switches.

Get-VirtualPortGroup -VMHost (Get-VMHost $objHost) | Where-Object {$_.VirtualSwitch -match "VMSwitch"} 

Gives me nothing.

Get-VirtualPortGroup -VMHost (Get-VMHost $objHost) | Where-Object {$_.VirtualSwitch -like "*"} 

Gives me all port groups on all switches, whereas:

Get-VirtualPortGroup -VMHost (Get-VMHost $objHost) | Where-Object {$_.VirtualSwitch -like "VM*"} 

Gives me nothing.

Is there something special about the .VirtualSwitch property that prevents this from working properly? Is there some other way of getting a list of virtual port groups on "VMSwitch", and not vSwitch0 or vMotion?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I think the name of the property in the VirtualPortGroupImpl object you want is VirtualSwitchName not VirtualSwitch.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

I think the name of the property in the VirtualPortGroupImpl object you want is VirtualSwitchName not VirtualSwitch.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
GuruAnt
Contributor
Contributor
Jump to solution

Ah, that would explain that!

It's working now using the Where-Object, but this doesn't explain why:

Get-VirtualPortGroup -VMHost (Get-VMHost $objHost) -VirtualSwitch $strVSwitch

Is reporting all port groups on all switches. Or does it not do what I think it does?

0 Kudos
ggochkov
VMware Employee
VMware Employee
Jump to solution

I hope to explain:

Get-VirtualPortGroup -VMHost (Get-VMHost $objHost) -VirtualSwitch $strVSwitch
in fact this is an aggregation of the:<span class="jive-thread-reply-body-container">{code:plain}
'Get-VirtualPortGroup -VMHost (Get-VMHost $objHost)' and 'Get-VirtualPortGroup -VirtualSwitch $strVSwitch'{code}

ggochkov
VMware Employee
VMware Employee
Jump to solution

... to be more descriptive I'll paste my test on this command:

PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; Get-VirtualPortGroup -VirtualSwitch $vs[0]

Name Key VirtualSwitch VLanId

-


--- -


-


Service... key-vim.host.PortGroup-Serv... vSwitch0 0

VM Network key-vim.host.PortGroup-VM N... vSwitch0 0

PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; Get-VirtualPortGroup -VMHost $h -VirtualSwitch $vs[0]

Name Key VirtualSwitch VLanId

-


--- -


-


VM Network key-vim.host.PortGroup-VM N... vSwitch0 0

Service... key-vim.host.PortGroup-Serv... vSwitch0 0

Service... key-vim.host.PortGroup-Serv... vSwitch1 0

VMkernel key-vim.host.PortGroup-VMke... vSwitch1 0

PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; connect-viserver 10.23.113.118 -user Administrator -Password '...'

There were one or more problems with the server certificate:

  • A certification chain processed correctly, but terminated in a root certificat

e which isn't trusted by the trust provider.

  • The certificate's CN name does not match the passed value.

Name Port User

-


-


-


10.23.113.118 443 Administrator

&lt;Now connecting to another host:&gt;

PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; $h = Get-vmhost

PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; $h

Name State Id

-


-


--

10.23.114.195 Connected HostSys...

PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; Get-VirtualPortGroup -VMHost $h -VirtualSwitch $vs[0]

Name Key VirtualSwitch VLanId

-


--- -


-


VM Network key-vim.host.PortGroup-VM N... vSwitch0 0

New-VMH... key-vim.host.PortGroup-New-... vSwitch0 0

SetVmHo... key-vim.host.PortGroup-SetV... vSwitch0 0

Service... key-vim.host.PortGroup-Serv... vSwitch0 0

VMkernel key-vim.host.PortGroup-VMke... vSwitch1 0

Service... key-vim.host.PortGroup-Serv... vSwitch0 0

VM Network key-vim.host.PortGroup-VM N... vSwitch0 0

PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt;

This in fact confirms it is an aggregation.

0 Kudos