Just leave out the DistributedSwitch parameter.
Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -Portgroup $portgroup
You got that $portgroup earlier in the script, and it implicitely knows which VDSwitch it is connected to.
Just leave out the DistributedSwitch parameter.
Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -Portgroup $portgroup
You got that $portgroup earlier in the script, and it implicitely knows which VDSwitch it is connected to.