Automation

 View Only
  • 1.  Script adding a host to a distributed switch not running

    Posted Sep 26, 2014 07:57 PM

    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?



  • 2.  RE: Script adding a host to a distributed switch not running

    Posted Sep 26, 2014 08:04 PM

    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 ?



  • 3.  RE: Script adding a host to a distributed switch not running

    Posted Sep 26, 2014 08:12 PM

    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.



  • 4.  RE: Script adding a host to a distributed switch not running
    Best Answer

    Posted Sep 26, 2014 08:39 PM

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



  • 5.  RE: Script adding a host to a distributed switch not running

    Posted Sep 26, 2014 09:30 PM

    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?!?!



  • 6.  RE: Script adding a host to a distributed switch not running

    Posted Sep 26, 2014 09:36 PM

    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!



  • 7.  RE: Script adding a host to a distributed switch not running

    Posted Sep 26, 2014 09:43 PM

    Too much credit :smileygrin:

    Glad you found the solution though.

    Have a nice WE !