AddESXHost.ps1

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

The following script is the complete script for adding a VMware ESX Server host and changing its status from OK (Limited) to OK. For an explanation of the steps in the script, see How to Add an ESX Server Host Using a Script. To add a VMware VirtualCenter Server, see AddVirtualCenterServer.ps1

Disclaimer

# 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.
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"}

# 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"

# 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;
}

# 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."
}