VMware Cloud Community
BKleiman72
Contributor
Contributor
Jump to solution

PowerCLI Script to move Standard vSwitch port groups to vDS

I have the following script I am trying to use to copy my Standard vSwitch port groups to vDS

$InputHost = Read-Host "Host Name"

$InputvSwitch = Read-Host "vSwitch Name"

$InputDvS = Read-Host "Distributed vSwitch Name"

$vmhost = Get-VMHost $InputHost

$vss = Get-VirtualSwitch -Name $InputvSwitch

$vssNumPorts = $vss.NumPorts

$standardpg = $vmhost | $vss | Get-VirtualPortGroup

$dvs = Get-VDSwitch $InputDvS

foreach ($i in $standardpg) {

$pvgname = $i.name.ToString()

$pvg = "dv-" + $pvgname

$vlan = $i.VLANID

When I run it I get an error "Expressions are only allowed as the first element of a pipeline." for the $standardpg = $vmhost | $vss | Get-VirtualPortGroup command

Anyone have any idea why.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.
Does this give any results, or produce any errors?

$InputHost = Read-Host "Host Name"

$InputvSwitch = Read-Host "vSwitch Name"

$InputDvS = Read-Host "Distributed vSwitch Name"

$vmhost = Get-VMHost $InputHost

$vss = Get-VirtualSwitch -Name $InputvSwitch -VMHost $vmhost

$vssNumPorts = $vss.NumPorts

$standardpg = Get-VirtualPortGroup -VirtualSwitch $vss

$dvs = Get-VDSwitch $InputDvS -VMHost $vmhost

foreach ($i in $standardpg) {

    $pvgname = $i.name.ToString()

    $pvg = "dv-" + $pvgname

    $vlan = $i.VLANID

    

    New-VDPortGroup -VDSwitch $dvs -Name $pvg -VLanId $vlan -PortBinding "Static" -NumPorts $vssNumPorts

}


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You're piping an object into an object, that will not work.

Try that part like this

$vmhost = Get-VMHost $InputHost

$vss = Get-VirtualSwitch -Name $InputvSwitch -VMHost $vmhost

$vssNumPorts = $vss.NumPorts

$standardpg = Get-VirtualPortGroup -VirtualSwitch $vss

$dvs = Get-VDSwitch $InputDvS -VMHost $vmhost

 


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

Reply
0 Kudos
BKleiman72
Contributor
Contributor
Jump to solution

That fixed the error but I missed a line in the script when I posted it.

Get-VDSwitch -Name $dvs | New-VDPortGroup -Name $pvg -VLanId $vlan -PortBinding "Static" -NumPorts $vssNumPorts

When I run this with the changes you suggested I get no output.

I just get the >> and nothing happens in vSphere.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Where does that line go?
perhaps post the script again as it is now


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

Reply
0 Kudos
BKleiman72
Contributor
Contributor
Jump to solution

Here is how the script looks now.

$InputHost = Read-Host "Host Name"

$InputvSwitch = Read-Host "vSwitch Name"

$InputDvS = Read-Host "Distributed vSwitch Name"

$vmhost = Get-VMHost $InputHost

$vss = Get-VirtualSwitch -Name $InputvSwitch -VMHost $vmhost

$vssNumPorts = $vss.NumPorts

$standardpg = Get-VirtualPortGroup -VirtualSwitch $vss

$dvs = Get-VDSwitch $InputDvS -VMHost $vmhost

foreach ($i in $standardpg) {

$pvgname = $i.name.ToString()

$pvg = "dv-" + $pvgname

$vlan = $i.VLANID

Get-VDSwitch -Name $dvs | New-VDPortGroup -Name $pvg -VLanId $vlan -PortBinding "Static" -NumPorts $vssNumPorts

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.
Does this give any results, or produce any errors?

$InputHost = Read-Host "Host Name"

$InputvSwitch = Read-Host "vSwitch Name"

$InputDvS = Read-Host "Distributed vSwitch Name"

$vmhost = Get-VMHost $InputHost

$vss = Get-VirtualSwitch -Name $InputvSwitch -VMHost $vmhost

$vssNumPorts = $vss.NumPorts

$standardpg = Get-VirtualPortGroup -VirtualSwitch $vss

$dvs = Get-VDSwitch $InputDvS -VMHost $vmhost

foreach ($i in $standardpg) {

    $pvgname = $i.name.ToString()

    $pvg = "dv-" + $pvgname

    $vlan = $i.VLANID

    

    New-VDPortGroup -VDSwitch $dvs -Name $pvg -VLanId $vlan -PortBinding "Static" -NumPorts $vssNumPorts

}


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

Reply
0 Kudos
BKleiman72
Contributor
Contributor
Jump to solution

That worked, looked like I needed to add the carriage return after the last "}" and it ran correctly.

Thanks for all the help.

Reply
0 Kudos