More

Get VM’s that require consolidation and approve consolidation

By Antti Hurme 16/08/2013 No Comments 2 Min Read

Wrote a small script that checks if there are any VM’s that require consolidation and asks for user action to consolidate them. You can choose between Yes and No on single VM’s or All to make the action automatic. It’s fairly simple to modify this script to make it automatic, if you so want to. It also catches any issues with consolidation so you don’t need to look at the vSphere Client while running the script.

#############################################################
###
### Consolidate VM HDD's
### Version 1
### 9.8.2013
###
##############################################################
###
### CONFIGURATION
###
$login_user = ""
$login_pwd = ""
$login_host = "vCenterServerHere"
##############################################################
### END
##############################################################
###Prepare variables
$consolidationlist = @()
$consolidationcount = 0;

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","No"
$yestoall = New-Object System.Management.Automation.Host.ChoiceDescription "&All","Consolidate All VMs that need consolidation"
$cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Exit Script"

###Check if we are connected to vCenter Server(s)
if($global:DefaultVIServers.Count -lt 1)
{
	echo "We need to connect first"
	#To connect using predefined username-password
	#Connect-VIServer $login_host -User $login_user -Password $login_pwd -AllLinked:$true
	
	#To connect using PowerCLI credential store
	Connect-VIServer $login_host -AllLinked:$true
}
else
{
	echo "Already connected"
}

get-view -ViewType VirtualMachine -Property Name, "Runtime", "LayoutEx.File"  | %{
	$vmName = $_.name
	if($_.runtime.consolidationNeeded) {
		$consolidation = $_.runtime.consolidationNeeded
		$consolidationcount++
		$deltadisks = ($_.LayoutEx.File | where {$_.name -like "*-000*.vmdk"}).count
		$consolidationlist += "" | select-object  @{n="VmName";e={$vmName}},@{n="Consolidation";e={$consolidation}},@{n="Delta Disks";e={$deltadisks}}
	}
}
if($consolidationcount -gt 0)
{
	write-host "VM's needing conslodation: $consolidationcount"
	#Print out our table
	$consolidationlist | format-table -autosize
	$caption = "Warning!"
	$message = "Do you want to proceed and consolidate Virtual Machines?"
	$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no,$yestoall)
	$result = $Host.UI.PromptForChoice($caption,$message,$choices,1)
	if($result -eq 2){ $autoaccept = 1 }
	if(($result -eq 0)-or($result -eq 2)) { 
		get-view -ViewType VirtualMachine -Property Name, "runtime" | where {$_.runtime.consolidationNeeded} | %{
		$vmName = $_.name
			if($_.runtime.consolidationNeeded) {
				if($autoaccept)
				{
					write-host "$vmName consolidation started"
					try {
						$_.ConsolidateVMDisks()
					}
					catch [Exception] {
						$Exception = $_.Exception.Message
					}
					if(!$Exception){
						write-host "Consolidation completed"
					}
					else {
						write-host "Consolidation failed with the message: $Exception"
						$exception = $false
					}
				}
				else
				{
					$caption = "$vmName"
					$message = "Do you want to proceed and consolidate $vmName ?"
					$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no,$cancel)
					$result = $Host.UI.PromptForChoice($caption,$message,$choices,1)
					if($result -eq 0) {
						write-host "$vmName consolidation started"
						try {
							$_.ConsolidateVMDisks()
						}
						catch [Exception] {
							$Exception = $_.Exception.Message
						}
						if(!$Exception){
							write-host "Consolidation completed"
						}
						else {
							write-host "Consolidation failed with the message: $Exception"
							$exception = $false
						}
					}
					elseif($result -eq 1) { 
						write-host "$vmName not consolidated"
					}
					elseif($result -eq 2) {
						write-host "Script execution stopped. Any started consolidations have not been terminated."
						exit
					}
				}
			}
		}
	}
	elseif($result -eq 1) { 
		write-host "No Virtual Machines consolidated!"
	}
}
else{
	write-host "No virtual machines require consolidation"
}
Written By

Who am I? | Linkedin

View All Articles
W
A
Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.