Tuesday, July 2, 2013

View 5.1 PowerCLI powershell script to automate the Link Clone creation

Here's a script we used to create hundred of VMWare View link clone pool for our VDI project. While the user interface of View Administrator is very good for creating link clone, it becomes a nightmare when you need to create 100+ VM pools

This script basically do the following:-

  1. Remove all old the snapshot of the Base Image except the latest snapshot
  2. Create a new snapshot on the Base Image for the link clone. The name of the snapshot is the current date and time.
  3. Create a new link clone pool using the latest snapshot (just created) using the following name as parameters
    • $cluster - Cluster
    • $pool_id - Pool ID 
    • $displayname - Display Name
    • $datastore - The Datastore for storing the Link Clone
    • $minimumcount - Minimum number of VMs
    • $maximumcount - Maximum number of VMs
    • $vcenteraddy - The full path of the Vcenter Server
    • $folder - the folder of the base image (we use the same location to keep the Link clone folder)
    • $ou - the Organizational unit in the Active Directory
    • $entitlementName - the group name in Active Directory that are entitled to this group
    • $parentvm - the VM name of the base image
  4. There are a few policy has been set by default and you could modify that in the first section of the script.
    • $headroomCount = 2 
    • $refreshpolicytype = "Never"
    • $deletepolicy = "DeleteOnUse" 
    • $powerpolicy = "AlwaysOn" 
    • $autologofftime = 540 
    • $IsUserResetAllowed = $true
    • $nameprefix = $pool_id + "{n:fixed=2}"
  5. Pre-requisite for this script to work for you
    • You are happy to have the LC folders under the same folder of your Base Image are contained.
    • All base image should be turned off and do ipconfig /release before doing so.
    • You should have planned all the parameters e.g datastore, clusters,...etc on a spreadsheet for all the pools, then you can use a simple formula in Excel to create the script.
  6. Here's an example of using this script (assume this script is saved as LC.ps1)
./LC.ps1 -cluster "ClusterA" -pool_id "VMD-1" -displayname "VM testing pool 1" -datastore "Datastore1" -minimumcount 2 -maximumcount 5 -vcenteraddy "vcenter.server.com" -folder "Pilot" -OU "OU=VDI" -entitlementName "VMUsers Group" -parentvm "VMDesktop"

Here's the detail of the script. Enjoy!!!

------Start of the scripts--------

param ($cluster, $pool_id, $displayname, $datastore, $minimumcount, $maximumcount, $vcenteraddy, $folder, $OU, $entitlementName, $parentvm)  

#Connect to vcenter and set some other variables
Connect-VIServer $vcenteraddy
Get-Datacenter | set-Variable -name datacenter

$headroomCount = 2 
$refreshpolicytype = "Never" 
$deletepolicy = "DeleteOnUse" 
$powerpolicy = "AlwaysOn" 
$autologofftime = 540 
$IsUserResetAllowed = $true 
$nameprefix = $pool_id + "{n:fixed=2}"

set-variable -name vmfolderpath -value /$datacenter/vm/$folder

$VM = Get-Folder $folder | Get-VM -Name $parentvm

$p = get-date.Year + get-date.Month + get-date.Day + get-date.Hour + get-date.minute + get-date.second 

Get-Snapshot -VM $VM -WarningAction SilentlyContinue | select Id, Created | Sort-Object Created -descending
$Snap = Get-Snapshot -VM $VM -WarningAction SilentlyContinue | Sort-Object Created -descending

$count = 0

foreach ($s in $Snap){

if ($count -gt 0) {

Write-host "Removing Snapshot"
Remove-Snapshot -Snapshot $s -confirm:$false

$count = $count - 1
Write-Host $count


Write-Host $p
Get-VM $VM | New-Snapshot -Name $p
Write-Host "New Snapshot created"

$snapshots = Get-Snapshot -VM $VM -WarningAction SilentlyContinue | select VM, Name, @{Name="Age";Expression={((Get-Date)-$_.Created).Hours}} | Sort-Object Age 

$parentsnapshotpath = ""

foreach ($s in $snapshots){

$parentsnapshotpath = "/" + $s.Name + $parentsnapshotpath


write-host $parentsnapshotpath

get-composerdomain | add-automaticlinkedclonepool -pool_id $pool_id -displayname $displayname -nameprefix $nameprefix -vmfolderpath $vmfolderpath -parentvmpath /$datacenter/vm/$folder/$parentvm -resourcepoolpath /$datacenter/host/$cluster/Resources -parentsnapshotpath $parentsnapshotpath -datastorespecs "[Conservative,OS,data]/$datacenter/host/$cluster/$datastore" -persistence Nonpersistent -organizationalUnit $OU -minimumcount $minimumcount -maximumcount $maximumcount -headroomCount $headroomCount -refreshpolicytype $refreshpolicytype -deletepolicy $deletepolicy -powerpolicy $powerpolicy -autologofftime $autologofftime -IsUserResetAllowed $IsUserResetAllowed 

Write-Host "Pool created sucessfully.  Now I will entitle it."

$entitlement =  Get-User -name $entitlementName 

Get-Pool -pool_id $pool_id | Add-PoolEntitlement -sid $entitlement.sid

Write-Host "Entitled and building VMs."

Write-Host "THE END"

---------end of the script-----------

No comments:

Post a Comment