AnkurS11
Contributor
Contributor

Script to create standard portgroup on multiple hosts in cluster

Jump to solution

Hi All,

I am creating a script to create standard portgroup on multiple hosts in cluster. below is the script but its throws an error.

Clear

Connect-VIServer MYVCName -ErrorAction Continue

$Hosts = Get-cluster -Name "ClusterName" |Get-VMHost

foreach ($H in $Hosts){

$vSwitch = Get-VirtualSwitch -VMHost $Hosts -Name "vSwitch0"

$vPortG = New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "v111" -VLanId 236 -Confirm

}

Getting the below error:

New-VirtualPortGroup : Cannot convert 'System.Object[]' to the type

'VMware.VimAutomation.ViCore.Types.V1.Host.Networking.VirtualSwitch' required by parameter 'VirtualSwitch'.

Specified method is not supported.

Below cmd works well if i gave the host name

Clear

Connect-VIServer MYVCName -ErrorAction Continue

$vSwitch = Get-VirtualSwitch -VMHost "myhostname FQDN name" -Name "vSwitch0"

$VLAN1 = New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "v111" -VLanId 236  -Confirm

Can someone help me to rectify the issue in the first script where i used foreach loop. i have to create multiple portgroups on multiple hosts in a cluster.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Try like this

$Hosts = Get-cluster -Name "ClusterName" |Get-VMHost

foreach ($H in $Hosts){

    $vSwitch = Get-VirtualSwitch -VMHost $H -Name "vSwitch0"

    $vPortG = New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "v111" -VLanId 236 -Confirm

}


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

Try like this

$Hosts = Get-cluster -Name "ClusterName" |Get-VMHost

foreach ($H in $Hosts){

    $vSwitch = Get-VirtualSwitch -VMHost $H -Name "vSwitch0"

    $vPortG = New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "v111" -VLanId 236 -Confirm

}


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

0 Kudos
AnkurS11
Contributor
Contributor

Thank you very much it was a very small mistake may be an typo which i didnt caught i should have used $H instead of $Hosts. After correcting that it worked like as expected.

0 Kudos