VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

foreach new-virtualportgroup

I am trying to create a virtualswitch and portgroups for multiple hosts but when I try this below, I only get the vswitches created on the hosts.

I do however get the first hosts portgroups created as well but not the other hosts

any idea ?

Connect-VIServer vc

$hostarray = @("host1","host2","host3")

foreach ($vmhost in $hostarray) {

New-VirtualSwitch -VMHost $vmhost -Name vSwitch4

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan10 -VLanId 10 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan15 -VLanId 15 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan17 -VLanId 17 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan20 -VLanId 20 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan30 -VLanId 30 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan40 -VLanId 40 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan50 -VLanId 50 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSWitch4 -Name vlan60 -VLanId 60 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan160 -VLanId 160 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSwitch4 -Name vlan162 -VLanId 162 -Confirm:$false

New-VirtualPortGroup -VirtualSwitch vSWitch4 -Name vlan220 -VLanId 220 -Confirm:$false

}

0 Kudos
1 Solution

Accepted Solutions
sneddo
Hot Shot
Hot Shot
Jump to solution

Not in front of a PC with access to test at the moment, but from memory you need to pass a vSwitch object to New-VirtualPortGroup. So something like this:

Connect-VIServer vc

$hostarray = @("host1","host2","host3")

foreach ($vmhost in $hostarray) {

   $vSwitch = New-VirtualSwitch -VMHost $vmhost -Name vSwitch4

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan10 -VLanId 10 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan15 -VLanId 15 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan17 -VLanId 17 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan20 -VLanId 20 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan30 -VLanId 30 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan40 -VLanId 40 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan50 -VLanId 50 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan60 -VLanId 60 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan160 -VLanId 160 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan162 -VLanId 162 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan220 -VLanId 220 -Confirm:$false

}

View solution in original post

0 Kudos
3 Replies
sneddo
Hot Shot
Hot Shot
Jump to solution

Not in front of a PC with access to test at the moment, but from memory you need to pass a vSwitch object to New-VirtualPortGroup. So something like this:

Connect-VIServer vc

$hostarray = @("host1","host2","host3")

foreach ($vmhost in $hostarray) {

   $vSwitch = New-VirtualSwitch -VMHost $vmhost -Name vSwitch4

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan10 -VLanId 10 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan15 -VLanId 15 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan17 -VLanId 17 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan20 -VLanId 20 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan30 -VLanId 30 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan40 -VLanId 40 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan50 -VLanId 50 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan60 -VLanId 60 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan160 -VLanId 160 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan162 -VLanId 162 -Confirm:$false

   New-VirtualPortGroup -VirtualSwitch $vSwitch -Name vlan220 -VLanId 220 -Confirm:$false

}

0 Kudos
tdubb123
Expert
Expert
Jump to solution

thanks that works. Can I run they all together instead of one host at a time?

0 Kudos
sneddo
Hot Shot
Hot Shot
Jump to solution

No, it's one at a time.

Well, technically you could, but not worth the effort as it isn't straight-forward Smiley Happy

0 Kudos