AddVirtualCenterServerChangeHostsToOK.ps1

適用於: Virtual Machine Manager 2008, Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

When you add a VMware VirtualCenter server to System Center Virtual Machine Manager (VMM) 2008, VMM discovers all VMware ESX Server hosts and clusters that the VirtualCenter server is managing and adds the objects to VMM. You can add a VirtualCenter server to VMM by using the Add-VirtualizationManager cmdlet. When you add the VirtualCenter server, you need to specify whether VMM should communicate with the ESX Server hosts in secure mode by using the SecureMode parameter. In secure mode, a certificate and public key are required for each ESX server host. If you want to trust communications and require only credentials for the hosts, set the SecureMode parameter to $FALSE. For more information about managing a VMware Infrastructure, see Managing a VMware Infrastructure in VMM (https://go.microsoft.com/fwlink/?LinkId=128921).

All newly added ESX Server hosts initially have OK (Limited) status in VMM. To fully manage the virtual machines on the hosts, you must provide credentials with appropriate authority by using the Associate-VMHost cmdlet. If you are managing the VMware environment in secure mode, you must also retrieve a security certificate and public key by using the Get-Certificate and Get-SshPublicKey cmdlets, respectively. In secure mode, you should ensure that the user verifies and accepts both the certificate and the public key for each non-embedded ESX Server host, or verifies and accepts the certificate for each embedded ESX Server host prior to changing the state of the host. After obtaining the appropriate credentials, use the Associate-VMHost cmdlet to change the state of the ESX Server host to OK. For information about the restrictions that apply to virtual machines on a host that has OK (Limited) status, see Supported Virtual Machine Actions for ESX Server Hosts (https://go.microsoft.com/fwlink/?LinkId=134489).

The following script adds a VirtualCenter server to VMM in secure mode, gets the ESX Server hosts, prompts the user to accept the certificate and public key, and then changes the state of the ESX Server hosts to OK.

Disclaimer

# Filename:       AddVirtualCenterServerChangeHostsToOK.ps1
# Description:    Adds a VMware VirtualCenter Server to VMM in
#                 secure mode and changes the status of the ESX 
#                 Server hosts from OK (Limited) to OK.


# Connect to the VMM server.
Get-VMMServer -ComputerName "VMMServer01.Contoso.com" | out-null

# Define the variables.
$VCSCredential = Get-Credential
$VCSName = "VirtualCenterServer01.Contoso.com"
$VCSCert= Get-Certificate -Computername $VCSName

# Verify that the certificate was obtained. If it wasn't, exit the script.
If ($VCSCert -eq $null)
{
   Return
}

# Require user to accept the certificate before adding the VirtualCenter Server.
Write-Host "Following is the certificate information for this VirtualCenter Server:"
$VCSCert
$AcceptCert = Read-Host "Do you accept this certificate? If you accept, enter Y."
If ($AcceptCert -eq "Y")
{
     # Add the Virtual Center Server.
     # The following command adds the Virtual Center Server in secure mode, which requires
     # a certificate and public key for each ESX Server host. To trust communications and 
     # require only credentials, change the value for -SecureMode to $FALSE.

     Add-VirtualizationManager -ComputerName $VCSName -Certificate $VCSCert –TCPPort 443 -Credential $VCSCredential -SecureMode $TRUE | out-null
}
Else
{
   Write-Host "The script cannot continue unless the certificate is verified. Ending script."
   Return
}

# Get the ESX Server hosts.
$ESXCredential = Get-Credential
$Manager = Get-VirtualizationManager -ComputerName $VCSName
$ESXServers = Get-VMHost | Where { $_.VirtualizationManager -eq $Manager }

# If the VirtualCenter Server was added in secure mode, loop through the ESX Server hosts and 
# get the certificate and the public key for each, and then require the user to accept both 
# before changing the state of the ESX Server host.

If ($Manager.SecureMode -eq $TRUE)
{
   ForEach ($ESXServer in $ESXServers)
   {
      $Cert = Get-Certificate -computername $ESXServer
      # The public key is present only on non-embedded ESX Server hosts.
      # For embedded ESX Server hosts, remove the checks for the public
      # key from the script.
      $PublicKey = Get-SshPublicKey -computername $ESXServer

      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")
               {
                  # If the ESX Server host hasn't finished adding, pause the script.
                  While ($ESXServer.OverallState -eq "Adding")
                  {
                     Start-Sleep 5
                  }
                  # Change the state of the ESX Server host.
                  Associate-VMhost -vmhost $ESXServer -credential $ESXCredential –certificate $Cert -sshpublickey $PublicKey | out-null
               }
               Else
               {
                  Write-Host "The script cannot update this ESX Server host unless the public key is verified. The host was not updated."
               }
            }
            Else 
            {
               Write-Host "The script cannot update this ESX Server host unless the certificate is verified. The host was not updated."
            }
   }
}

# If the VirtualCenter server was not added in secure mode, loop through the ESX Server hosts
# and change their state.

Else
{
   ForEach ($ESXServer in $ESXServers)
   {
      # If the ESX Server host hasn't finished adding, pause the script.
      While ($ESXServer.OverallState -eq "Adding")
      {
         Start-Sleep 5
      }

      Associate-VMhost -vmhost $ESXServer -credential $ESXCredential | out-null
   }
}

另請參閱

概念

AddESXHost.ps1
AddVirtualCenterServer.ps1