VMware Cloud Community
chakoe
Enthusiast
Enthusiast
Jump to solution

Script to add multiple vPortGroups on a vSvitch on mulpitple Hosts

Hi,

i have a little script to add multiple vPortGroups on a vSvitch on mulpitple Hosts.

The Script looks like:

$ObjHosts = Get-VMHost host12* |Sort-Object -Property Name

Import-Csv "C:\vPortGroups.csv" -UseCulture | %{
    foreach($objHost in $ObjHosts){
        $strVSwitch = Get-Virtualswitch -VMHost (Get-VMHost $objHost) | where-object { $_.Name -match "$_.strVSwitch" }
        Write-Host "Adding Virtual Port Group" $_.strNewVPG "with VLAN Tag" $_.strNewVlanTag "to" $_.strVSwitch "on" $objHost
        New-VirtualPortGroup -Name "$_.strNewVPG" -VirtualSwitch "$_.strVSwitch" -VLanId "$_.strNewVlanTag"
    }
}

The input-file looks like:

strNewVPG;strVSwitch;strNewVlanTag
"4.244.151.0-24";"vSwitch1";120

If we use only the line " Write-Host "Adding Virtual Port Group" $_.strNewVPG "with VLAN Tag" $_.strNewVlanTag "to" $_.strVSwitch "on" $objHost"

the output is "Adding Virtual Port Group 4.244.151.0-24 with VLAN Tag 120 to vSwitch1 on d100spwesxc3101.d100.intern"

So, the input-file is correct and readable for the script.

If i use the complete script, the output-message is:

New-VirtualPortGroup : Cannot bind parameter 'VLanId'. Cannot convert value "@{strNewVPG=4.244.151.0-24; strVSwitch=vSwitch1; strNewVlanTag=120}.strNewVlanTag" to type "System.Int32". Error: "I
nput string was not in a correct format."
At C:\Dokumente und Einstellungen\user1\Eigene Dateien\VMWare PowerShell Scripte\add_VirtualPortGroup_by_inputfile.ps1:7 char:83
+         New-VirtualPortGroup -Name "$_.strNewVPG" -VirtualSwitch "$_.strVSwitch" -VLanId <<<<  "$_.strNewVlanTag"
    + CategoryInfo          : InvalidArgument: (:) [New-VirtualPortGroup], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVirtualPortGroup

What is going wrong in here?

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I made a few changes to your script. Can you try the new version?

$ObjHosts = Get-VMHost host12* |Sort-Object -Property Name
 
Import-Csv "C:\vPortGroups.csv" -UseCulture | %{
    $vPortGroup = $_
    foreach($objHost in $ObjHosts){
        $strVSwitch = Get-Virtualswitch -VMHost (Get-VMHost $objHost) | where-object { $_.Name -match $vPortGroup.strVSwitch }
        Write-Host "Adding Virtual Port Group" $vPortGroup.strNewVPG "with VLAN Tag" $vPortGroup.strNewVlanTag "to" $vPortGroup.strVSwitch "on" $objHost.Name
        New-VirtualPortGroup -Name $vPortGroup.strNewVPG -VirtualSwitch $strVSwitch -VLanId $vPortGroup.strNewVlanTag
    } 
}

One of the major problems was that $_ only represents the object from the last pipeline. So I added the line $vPortGroup = $_ to keep the line from the input file in the $vPortGroup variable.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
1 Reply
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I made a few changes to your script. Can you try the new version?

$ObjHosts = Get-VMHost host12* |Sort-Object -Property Name
 
Import-Csv "C:\vPortGroups.csv" -UseCulture | %{
    $vPortGroup = $_
    foreach($objHost in $ObjHosts){
        $strVSwitch = Get-Virtualswitch -VMHost (Get-VMHost $objHost) | where-object { $_.Name -match $vPortGroup.strVSwitch }
        Write-Host "Adding Virtual Port Group" $vPortGroup.strNewVPG "with VLAN Tag" $vPortGroup.strNewVlanTag "to" $vPortGroup.strVSwitch "on" $objHost.Name
        New-VirtualPortGroup -Name $vPortGroup.strNewVPG -VirtualSwitch $strVSwitch -VLanId $vPortGroup.strNewVlanTag
    } 
}

One of the major problems was that $_ only represents the object from the last pipeline. So I added the line $vPortGroup = $_ to keep the line from the input file in the $vPortGroup variable.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos