-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployToClass.ps1
47 lines (42 loc) · 1.28 KB
/
deployToClass.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
41
42
43
44
45
46
47
# Arsh Chauhan
# Last Edited: 11/14/2016
# deployToClass.ps1: Deploy a VM to a class.
# A class is an arbitary folder in vcenter
. .\utils.ps1
If ($MyInvocation.line.substring(0,2) -ne ". ") {
$vmName = Read-Host -Prompt "Base VM to Deploy"
$class = Read-Host -Prompt "Class to deploy to"
ConnectToVcenter
$students = ListFolderSubfolders($class)
$vm = Get-VM "Base $vmName"
If ($($vm | Get-View).snapshot -eq $null) {
Write-Host "Cannot clone VM '$vmName' because it has no snapshot(s)."
exit
}
Write-Host "Deploying VM's"
foreach($student in $students)
{
if ([string]$student -ne "Infrastructure") #Do not deploy to infrastructure folder
{
try
{
$cloneName = "$vmName - $student"
DeployVm $student $cloneName $vm
}
catch
{}
}
}
#Move all VM's to ESXI2 and consolidate disks
Write-Host "Moving VM's"
foreach($student in $students)
{
try
{
Get-VM -Name "$vmName - $student" | Move-VM -Destination "esxi2.csc.uaf.edu" -Datastore "ESXI2"
(Get-VM -Name "$vmName - $student").ExtensionData.ConsolidateVMDisks_Task() >$devnull
}
catch
{}
}
}