Automation

 View Only
  • 1.  PowerCLI Script to move Standard vSwitch port groups to vDS

    Posted Jan 30, 2017 04:33 PM

    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.



  • 2.  RE: PowerCLI Script to move Standard vSwitch port groups to vDS

    Posted Jan 30, 2017 05:01 PM

    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

     



  • 3.  RE: PowerCLI Script to move Standard vSwitch port groups to vDS

    Posted Jan 30, 2017 06:55 PM

    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.



  • 4.  RE: PowerCLI Script to move Standard vSwitch port groups to vDS

    Posted Jan 30, 2017 08:11 PM

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



  • 5.  RE: PowerCLI Script to move Standard vSwitch port groups to vDS

    Posted Jan 30, 2017 08:13 PM

    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



  • 6.  RE: PowerCLI Script to move Standard vSwitch port groups to vDS
    Best Answer

    Posted Jan 30, 2017 09:16 PM

    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

    }



  • 7.  RE: PowerCLI Script to move Standard vSwitch port groups to vDS

    Posted Jan 31, 2017 04:27 PM

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

    Thanks for all the help.