This one is bizarre and I'm sure it's something simple. I created a script that configures NTP and syslog on a host. I added on configuring the standard vswitch0, and that part works fine. I set the MTU, add a vMotion and FT port and that part works fine. I wanted to add on a section of code to add the host to our distributed switch. Environment is vSphere 5.5 U2, switch is a 5.5 distributed switch. For some reason, the code doesn't error out at all but just doesn't do anything. However, if I paste the code into a Powershell window it works just fine. Same code, same host, same switch. I'm out of ideas as to why it works manually pasting the code into a window but doesn't work running the script itself. Here's the relevant code that isn't working in the script:
#Adds vmnic5 and vmnic6 to the distributed switch
Write-Host
Write-Host "Adding $VMHost to distributed switch $vds..." -ForegroundColor "Green"
Disconnect-VIServer * -Confirm:$false #earlier part of the script connects directly to the host
Connect-VIServer $vcenter | Out-Null #$vcenter variable created earlier in the script
Get-VDSwitch $vds | Add-VDSwitchVMHost -Server $vcenter -VMhost $VMhost #$vds and $VMhost variables gathered earlier in the script
$vmnic5 = Get-VMHost $VMhost | Get-VMHostNetworkAdapter -Physical -Name vmnic5
$vmnic6 = Get-VMHost $VMhost | Get-VMHostNetworkAdapter -Physical -Name vmnic6
Get-VDSwitch $vds | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $vmnic5 -confirm:$false
Get-VDSwitch $vds | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $vmnic6 -confirm:$false
Seems simple enough, but for some reason it just doesn't work. Any ideas?