Wednesday, August 22, 2012

Failed to remove VM: null Fix


When you perform a restore of a vCenter server that supports a VDI environment, you may encounter an issue where you start getting desktops showing up as “Error: Failed to delete VM: null”.  Here are the procedures to correct this problem

1. First disable provisioning on the VDI pool
a. Open View Administrator
b. Click on the pool with the problem
c. Click on the “Status” button
d. Select “Disable Provisioning”

2. Delete all desktops from the pool
a. Click on the “Inventory” tab
b. Shift + Click the first and last desktops
c. Click the “Remove” button

3. Wait till View has a chance to process the deletions (about 5 min)

4. After the 5 min. go into vCenter and manually delete any VMs that may have not gotten deleted. (Do not delete the template VM)

5. Go into ADUC and delete any computer objects that may not have been removed. (Do not delete the template VM computer object)

6. RDP into the View Connection Broker server and follow the instructions on this VMware KB article link to remove any orphaned objects from the ADAM DB on the Connection Broker.
______________________________________________________________________________
Removing the virtual machine from the ADAM database
Note: Before removing entries from the ADAM database, note the virtual machine name of the desktops that are being removed for reference when editing the Composer Database.

1. Connect to the View ADAM Database. For more information, see Connecting to the View ADAM Database (2012377).

2. Locate the GUID of the virtual machine.

To locate the GUID of the virtual machine:

a. Right-click the Connection View ADAM Database [localhost:389], and click New > Query.
b. Under Root of Search, click Browse.. and select the Servers organizational unit.
c. Click OK.
d. In the Query String, paste this search string:

(&(objectClass=pae-VM)(pae-displayname=VirtualMachineName))

Where VirtualMachineName is the name of the virtual machine for which you are trying to locate the GUID. You may use * or ? as wildcards to match multiple desktops.

e. Click OK to create the query.
f. Click the query in the left pane. The virtual machines that match the search are be displayed in the right pane.

3. Record the GUID in cn=
______________________________________________________________________________
Delete the pae-VM object from the ADAM database:

1. Locate the OU=SERVERS container.

2. Locate the corresponding virtual machine's GUID (from above) in the list which can be sorted in ascending or descending order, choose Properties and check the pae-DisplayName Attribute to verify the corresponding linked clone virtual machine object.

3. Delete the pae-VM object.

Notes:
Check if there are entries under OU=Desktops and OU=Applications in the ADAM database.
A broken pool that does not contain any desktops can be removed from View Manager by removing the pool entry from both the Server Groups and Applications organizational units. However, removing one entry and not the other from the ADAM database results in the java.lang.nullpointerexception error when attempting to view the pools or desktops inventory in View Manager.
______________________________________________________________________________
7. Re-enable provisioning on the VDI pool.

8. Monitor the pool to ensure desktops are created successfully

Friday, August 10, 2012

DRS Rule Export \ Import

As the title says, this is a Powershell Method for exporting all of your DRS rules to a .txt file that can later be used to import the rules back into a cluster.

It is great for cases where you wish to keep a back of your rules or are looking to migrate to a new vCenter server and do not wish to manually create all your DRS rules again.

Export: 
$outfile = "C:\rules.txt"
$rules = get-cluster | Get-DrsRule
foreach($rule in $rules){   $line = (Get-View -Id $rule.ClusterId).Name   $line += ("," + $rule.Name + "," + $rule.Enabled + "," + $rule.KeepTogether)   foreach($vmId in $rule.VMIds){     $line += ("," + (Get-View -Id $vmId).Name)   }   $line | Out-File -Append $outfile }

Import: (Assumes your export was written to c:\rules.txt)
$file = "C:\rules.txt"
$rules = Get-Content $file
foreach($rule in $rules){   $ruleArr = $rule.Split(",")   if($ruleArr[2] -eq "True"){$rEnabled = $true} else {$rEnabled = $false}   if($ruleArr[3] -eq "True"){$rTogether = $true} else {$rTogether = $false}   get-cluster $ruleArr[0] | `     New-DrsRule -Name $ruleArr[1] -Enabled $rEnabled -KeepTogether $rTogether\     -VM (Get-VM -Name ($ruleArr[4..($ruleArr.Count - 1)])) }

Delete VM then Clone VM Powershell script

I recently found myself in a situation where I needed to create an automated taks that would create a clone of a VM on a weekly basis.

Now I know there are tools and software out there that can take full VM backup and I run vRanger on the majority of my VMs, but this was a special case senerio.

So below is a script that will first check to see if there is already a snapshot of my vm, if so it deletes it, then it will make a new clone. If there are any errors in the process it will email me.

$date = Get-Date
$vcServer = "vcenter.you.com"
$fromVMname = "SourceVM"
$newVMName = "CloneVM"
$tgtEsxName = "ESXi.you.com"
$tgtDatastoreName = "Datastore1"
$userName = "account"
$passWord = "password"
$notes = ("Clone of: " + $fromVMname + " for backup. Created on: " + $date)

# Connect to the vCenter Server

Connect-VIServer -Server $vcServer -User $username -Password $passWord




#
# Test to see if $newVMname already exists in vCenter Server and 
# set $vmExist to a value other than $null if it does
$vmExist = Get-VM -Name $newVMname -ErrorAction SilentlyContinue


#
# Delete the VM $newVMname if $vmExist returns a value other than $null
if ($vmExist -ne $null)
{
 Remove-VM -deletefromdisk -VM $newVMName -confirm:$false
}
#
# Create a clone of $fromVMname to $newVMName, email if there is an error
$cloneTask = New-VM -Name $newVMName -VM (Get-VM $fromVMname) -VMHost (Get-VMHost $tgtEsxName) -Datastore (Get-Datastore $tgtDatastoreName) -RunAsync
Wait-Task -Task $cloneTask -ErrorAction SilentlyContinue
Get-Task | where {$_.Id -eq $cloneTask.Id} | %{
     if($_.State -eq "Error"){
          $event = Get-VIEvent -Start $_.FinishTime | where {$_.DestName -eq $newVMName} | select -First 1
          $emailFrom = VDIPowerCLI@you.com
          $emailTo = me@you.com
          $subject = "Clone of " + $newVMName + " failed"
          $body = $event.FullFormattedMessage
          $smtpServer = smtp.you.com
          $smtp = new-object Net.Mail.SmtpClient($smtpServer)
          $smtp.Send($emailFrom, $emailTo, $subject, $body)
     }
}
#
# Set the notes field of the VM $notes
Set-VM -VM $newVMName -Notes $notes -confirm:$false
#
#Disconnect from the vCenter Server
Disconnect-VIServer -Server $vcServer -Confirm:$false 


With the script created I setup a Windows Scheduled task on my vCenter server to run this script once a week.

So far it has been working well.

Helpful VMware View PowerShell Scripts

Disable Provisioning in all linked clone pools:
$pools = get-pool
$pools | Update-AutomaticLinkedClonePool -isProvisioningEnabled:$false
Gets list of all View related cmdlets:

get-command | where {$_.modulename -match 'vmware.view'}
#Below is so you can include it into the 
#add-pssnapin.ps1 under your View installation directory
function get-viewcommand {get-command | where {$_.modulename -match 'vmware.view'}}
Gets list of pools, max number of VM's, and datastores associated:

#Helps in making sure your pools are distributed across evenly among datastores.
Get-Pool | Select Pool_ID, MaximumCount, DatastorePaths