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.
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
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
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.
Where does that line go?
perhaps post the script again as it is now
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
That worked, looked like I needed to add the carriage return after the last "}" and it ran correctly.
Thanks for all the help.