VMware Cloud Community
Assyrian
Contributor
Contributor
Jump to solution

Review foreach comamnd to add hosts to a vCenter

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.

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

 


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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

 


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

Reply
0 Kudos
Assyrian
Contributor
Contributor
Jump to solution

That did it! Thank you so much @LucD !

Reply
0 Kudos