VMware Cloud Community
ArrowSIVAC
Enthusiast
Enthusiast

POWER CLI - Add vSphere 5.5u2 Host to Cluster

Been googling around for some time and not found a method to accomplish total task of using a script to create and deploy cluster

1) Script base to read from CSV and variable creation

############### Variables ########################################

Import-CSV vmhosts.csv | ForEach-Object {

$sacluster = $_.sacluster

$vcenter = $_.vcenter

$cluster = $_.cluster

$datacenter = $_.datacenter

$sapassword = $_.sapassword

$esxpassword = $_.esxpassword

}

################ End Variables ###################################

############## Connect to vCenter ################################

connect-viserver $vcenter

############ End Connect to vCenter ##############################

2) create Cluster

Write-Host "Creating Datacenter $datacenter" -ForegroundColor green

$location = Get-Folder -NoRecursion

New-Cluster -Location $datacenter -Name $cluster -DRSEnabled -DrsAutomationLevel FullyAutomated

3)  Add hosts into cluster object / container

Import-CSV vmhosts.csv | ForEach-Object {

$hostname = $_.vmhost

  Write-Host "Adding ESX Host $hostname to $datacenter" -ForegroundColor green

  Add-VMHost -Name $hostname -Location (Get-cluster $cluster) -User root -Password $esxpassword -Force -RunAsync -Confirm:$false

  # Move vSphere host from datacenter level under cluster level

  ## << Bug in that i want it to place hosts in "$cluster" >>

  ## Set-VMHost -VMHost $hostname -State "Maintenance"

  Get-VMHost -Name $hostname | Move-VMHost -Destination $cluster

 

}

** Above step three is broken in that "Get-cluster"  as location does not work.

cluster object target

PowerCLI C:\HCScripts> Add-VMHost -Name $hostname -Location (Get-Datacenter $cluster) -User root -Password $esxpassword

-Force -RunAsync -Confirm:$false

Get-Datacenter : 8/1/2016 1:30:03 PM    Get-Datacenter        Datacenter with name 'cluster' was not found using the

specified filter(s).

At line:1 char:39

+ Add-VMHost -Name $hostname -Location (Get-Datacenter $cluster) -User root -Passw ...

+                                       ~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-Datacenter], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat

   acenter

Add-VMHost : 8/1/2016 1:30:03 PM    Add-VMHost        Value cannot be found for the mandatory parameter Location

At line:1 char:1

+ Add-VMHost -Name $hostname -Location (Get-Datacenter $cluster) -User root -Passw ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Add-VMHost], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.AddVMHost

but if I change to dump the hosts NOT into a cluster, but a datacenter, it works fine.

datacenter object target

PowerCLI C:\HCScripts> Add-VMHost -Name $hostname -Location (Get-Datacenter $datacenter) -User root -Password $esxpassw

rd -Force -RunAsync -Confirm:$false

Name                           State      % Complete Start Time   Finish Time

----                           -----      ---------- ----------   -----------

AddStandaloneHost_Task         Running             0 01:31:53 PM

PowerCLI C:\HCScripts>

PowerCLI C:\HCScripts>

It creates Datacenter, Cluster object, but I can't get the hosts to add into the context of the cluster. 

pastedImage_12.png

I read that the hosts may have to be in maintenance mode and this can have some other script implications, I can address that but at this stage their are no VMs... fresh installations. 

Thanks,

Tags (2)
0 Kudos
4 Replies
LucD
Leadership
Leadership

With Add-VMHost you add the ESXi node to the vCenter, and you can only specify in which Datacenter.

With Move-VMHost you can move the ESXi host to a cluster, by using the Destination parameter


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

0 Kudos
LucD
Leadership
Leadership

By coincidence today there is a blog post on the PowerCLI blog on the subject, see Using PowerCLI to Configure Hosts


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

0 Kudos
ArrowSIVAC
Enthusiast
Enthusiast

Thanks for the reponse but that is the issue I have.

I can add the host, but I can't get syntax correctly to move it.

Here is what I get

I read these all from CSV. but for debug I just set variables manually and then run script

Add host to cluster

$vcenter = "vc1"

$cluster = "Production"

$datacenter = "Datacenter1"

$esxpassword = "password"

$hostname = "esx4.aessatl.arrow.com"

PowerCLI C:\HCScripts> Add-VMHost -Name $hostname -Location (Get-Datacenter $datacenter) -User root -Password $esxpassword -Force -RunAsync -Confirm:$false

Name                           State      % Complete Start Time   Finish Time

----                           -----      ---------- ----------   -----------

AddStandaloneHost_Task         Running             0 09:18:31 AM

PowerCLI C:\HCScripts> Move-VMHost -Name $hostname -Destination $cluster -Confirm:$false

Move-VMHost : A parameter cannot be found that matches parameter name 'Name'.

At line:1 char:13

+ Move-VMHost -Name $hostname -Destination $cluster -Confirm:$false

+             ~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Move-VMHost], ParameterBindingException

    + FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVMHost

PowerCLI C:\HCScripts> Move-VMHost -Name $hostname -Destination (Get-Cluster $cluster) -Confirm:$false

Move-VMHost : Cannot convert 'System.Object[]' to the type

'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Destination'. Specified method is

not supported.

At line:1 char:42

+ Move-VMHost -Name $hostname -Destination (Get-Cluster $cluster) -Confirm:$false

+                                          ~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Move-VMHost], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVMHost

I watched that video.. and half a dozen others, and they never do this step.. I have all the other parameters of the adding and setup of the hosts worked out, just not this last step to move it into a cluster.

0 Kudos
LucD
Leadership
Leadership

Try like this.

It uses the pipeline to pass the VMHost object that is generated by the Add-VMHost to the Move-VMHost cmdlet (the VMHost parameter on Move-VMHost accepts pipeline input)

$vcenter = "vc1"

$cluster = "Production"

$datacenter = "Datacenter1"

$esxpassword = "password"

$hostname = "esx4.aessatl.arrow.com"

Add-VMHost -Name $hostname -Location (Get-Datacenter $datacenter) -User root -Password $esxpassword -Force -RunAsync -Confirm:$false |

Move-VMHost -Destination $cluster -Confirm:$false


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

0 Kudos