Automation

 View Only
  • 1.  Review foreach comamnd to add hosts to a vCenter

    Posted Dec 02, 2023 05:01 PM

    Want to preface that scripting and automation is weak but doing my best to learn. 

    I have 3 hosts I want to add to a cluster that is already created in vCenter. While the following can be better this is the best I can scrap together..haha

    ##Adding ESXi hosts to a Cluster Object already created in vCenter##
    foreach ($securehost in $securehosts) {
    Add-VMHost -Server vcenter.khizeran.com -Name $securehosts -Location "Secure Cluster" -User root -Password "VMware1!" -Force }

     

    When I ran this line, with the following, it ran but only added one host, but something else went weird with my variable, I'm getting confused with what is in my foreach command. I know I typed in the same variable in it, and that does not make sense.

    PS C:\Windows\system32> foreach ($securehosts in $securehosts) {
    Add-VMHost -Server vcenter.khizeran.com -Name $securehosts -Location "Secure Cluster" -User root -Password "<password>" -Force }


    Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version
    ---- --------------- ---------- ------ ----------- ----------- ------------- ------------- -------
    esxi16.khizeran.com Connected PoweredOn 2 0 4798 0.000 7.999 7.0.3

    Any help would be greatly appreciated.

     



  • 2.  RE: Review foreach comamnd to add hosts to a vCenter
    Best Answer

    Posted Dec 02, 2023 05:37 PM

    Your loop variable is $securehost, but you used $securehosts on the Add-VMHost cmdlet.

    Add-VMHost -Server vcenter.khizeran.com -Name $securehost -Location "Secure Cluster" -User root -Password "VMware1!" -Force

     



  • 3.  RE: Review foreach comamnd to add hosts to a vCenter

    Posted Dec 02, 2023 06:05 PM

    That did it! Thank you so much  !