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?
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
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
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?
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}
... to be more descriptive I'll paste my test on this command:
PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows> 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> 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> 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
<Now connecting to another host:>
PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows> $h = Get-vmhost
PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows> $h
Name State Id
-
-
--
10.23.114.195 Connected HostSys...
PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows> 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>
This in fact confirms it is an aggregation.