-
Notifications
You must be signed in to change notification settings - Fork 20
/
CloneTemplateToClusters.ps1
40 lines (36 loc) · 1.41 KB
/
CloneTemplateToClusters.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# PowerCLI Script For Cloning Templates to Multiple Datastores
# @davidstamen
# http://davidstamen.com
#Import PowerCLI Module
Add-PSSnapin VMware.VimAutomation.Core
#Define Variables
$Credential = Get-Credential
$vCenter = "vcenter.lab.local"
$clusters = "Cluster01","Cluster02"
$location = "Templates"
$templates = "Template1","Template2"
#Connect to vCenter
Connect-VIServer $vCenter -Credential $Credential
foreach ($template in $templates){
foreach ($cluster in $clusters){
#Check if Template exists
Try{Get-Template $template-$cluster -ErrorAction Stop;$TemplateExists = $true}Catch {$TemplateExists = $false}
#Create VM
If($TemplateExists -eq $true){
#Remove Old Template
Get-Template -Name $template-$cluster |Remove-Template -DeletePermanently -Confirm:$false
#Clone the Template
New-VM -Name $template-$cluster -Template $template -ResourcePool $cluster -Datastore $cluster-DSC -Location $location
#Convert to Template
Set-VM -VM $template-$cluster -ToTemplate -Confirm:$false
}
ElseIf($TemplateExists -eq $false){
#Clone the Template
New-VM -Name $template-$cluster -Template $template -ResourcePool $cluster -Datastore $cluster-DSC -Location $location
#Convert to Template
Set-VM -VM $template-$cluster -ToTemplate -Confirm:$false
}
}
}
#Disconnect from vCenter
Disconnect-VIServer $vCenter -Force -Confirm:$false