VMware Cloud Community
adamjg
Hot Shot
Hot Shot
Jump to solution

Script adding a host to a distributed switch not running

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?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

When you load the script in an editor (the PowerShell ISE for example) and run if from there, do the vDS cmdlets get executed ?


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Just to make sure I get the issue here, you placed the code in a PS1 file.

But when you execute the PS1 file, nothing happens ?

Do you start the script from the PowerCLI prompt ? Or from an IDE, like for example PowerGui ?

Do other scripts work flawlessly when you start them that way ?


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

0 Kudos
adamjg
Hot Shot
Hot Shot
Jump to solution

LucD wrote:

Just to make sure I get the issue here, you placed the code in a PS1 file.

But when you execute the PS1 file, nothing happens ?

Do you start the script from the PowerCLI prompt ? Or from an IDE, like for example PowerGui ?

Do other scripts work flawlessly when you start them that way ?

Sorry, I wasn't sure how to explain this.  I have this script in a PS1 file.  I open up a standard Powershell window, change to the directory and run the PS1 file.  I include the PSSnapins in the code.  The script runs just fine and the first 90% of the script works (NTP, syslog, VAAI settings, vSwitch0 settings).  It gets to the point in the code from my first message where it disconnects from all viservers then connects to the vcenter.  But then it skips over those last lines connecting it to the vDS.

Now, when the script finishes I can paste in the lines that create the variables, then paste in those lines at the end, and it works just fine, connects to the switch then adds vmnic5/6 to it.  So the code works, it just doesn't run at the end of the script for some reason.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you load the script in an editor (the PowerShell ISE for example) and run if from there, do the vDS cmdlets get executed ?


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

0 Kudos
adamjg
Hot Shot
Hot Shot
Jump to solution

I ran it in PowerGUI and got the same thing.  It doesn't output any errors at all.  I even put pause lines in between each so I could see, and no errors, nothing, and it doesn't add the host to the distributed switch.  I then pasted the variable lines in and copied in the 5 or so lines that I listed and it added it just fine.  This is truly bizarre and I have to be missing something?!?!

0 Kudos
adamjg
Hot Shot
Hot Shot
Jump to solution

Gahhhhh I figured it out.  At some point higher up in the script I did a $VMhost = Get-VMhost $VMhost and that screwed it up.  I took that out and now it works.  Since it's late on a Friday and much later for you Luc, I'm gonna say that the mere presence of you in this thread is what fixed it.  Have a good weekend!

LucD
Leadership
Leadership
Jump to solution

Too much credit :smileygrin:

Glad you found the solution though.

Have a nice WE !


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

0 Kudos