Export (0) Print
Expand All

How to Add an ESX Server Host Using a Script

Updated: October 21, 2008

Applies To: Virtual Machine Manager 2008, Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

The following example demonstrates how to add VMware ESX Server hosts to VMM. The example also demonstrates how to change their state from OK (Limited) to OK. The OK (Limited) state is the default state in which ESX Server hosts are imported when the VirtualCenter Server is added. To copy the entire script, see AddESXHost.ps1.

Explanation of AddESXHost.ps1

To start, define the variables. Decide which VirtualCenter Server you want to add the ESX Server host to, and then replace the VirtualCenterServer placeholder with the name of that VirtualCenter Server.

# Filename:      AddESXHost.ps1
# Description:   Adds a VMware ESX Server host to VMM, and changes the state of
#                the host from OK (Limited) to OK.

Connect to the VMM server that manages the VirtualCenter Server.

# Connect to the VMM server.
Get-VMMServer -ComputerName "VMMServer.Contoso.com"
# Define the variables.
$Credential = Get-Credential
$Manager = Get-VirtualizationManager -ComputerName "VirtualCenterServer"
$VMHostGroup = Get-VMHostGroup | where {$_.Path -eq "All Hosts\Group in VMCenter"}

Next, add the ESX Server host to VMM using the Add-VMHost cmdlet. In the script, replace the XX.XXX.XX.XX with the computer name of the ESX Server host you want to add.

# Add the ESX Server host. Replace the XXX.XXX.XX.XX placeholder with the host name
# or IP address.
Add-VMHost -ComputerName "XX.XXX.XX.XX" -Credential $Credential -VMHostGroup $VMHostGroup -VirtualizationManager $Manager -TCPPort 443 -SshTcpPort 22 –RunAsynchronously -JobVariable "AddESXHostJob"

The job that adds the host must be complete to get the certificate and the public key in the next step that is performed by the script. The following part of the script displays the current step the job is performing in the VMM command shell and makes the script wait for the job to stop running before continuing.


# Wait for the add-host job to finish.
$JobNameString = $AddESXHostJob.CmdletName+" "+$AddEsxHostJob.ResultName

while ($AddESXHostJob.status -eq "Running")
{
   Write-Progress -activity "$JobNameString" -Status $AddEsxHostJob.CurrentStep;
   Start-Sleep -seconds 5;
}

After the host is added, you can verify in the VMM Administrator Console that its status is OK (Limited). To change the status to OK, you must get the certificate and the public key from the host. Then, ask the user to verify the returned information for both the certificate and the public key before associating the host.

# Get the certificate and the public key, and require the user to accept
# both before changing the state of the ESX Server host.
# Replace the XX.XXX.XX.XX placeholder with the same host ID you used to
# add the host to VMM.  
$ESXhost = Get-VMhost -computername "XX.XXX.XX.XX"
$Cert = Get-Certificate -computername "XX.XXX.XX.XX"
$PublicKey = Get-SshPublicKey -computername "XX.XXX.XX.XX"

Write-Host "Following is the certificate information for this ESX Server host:"
$Cert
$AcceptCert = Read-Host "Do you accept this certificate? If you accept, enter Y."
If ($AcceptCert –eq “Y”)
{
   Write-Host "Following is the public key information for this ESX Server host:"
   $PublicKey
   $AcceptPK = Read-Host "Do you accept this public key? If you accept, enter Y."
   If ($AcceptPK –eq "Y")
   {
      # Change the state of the ESX Server host.

      Associate-VMhost -vmhost $ESXhost -credential $Credential –certificate $Cert -sshpublickey $PublicKey
   }
   Else
   {
      Write-Host "The script cannot continue unless the public key is verified. Ending script."
   }
}
Else 
{
   Write-Host "The script cannot continue unless the certificate is verified. Ending script."
}
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft