This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bulk_CreateLab_ARM.ps1
215 lines (175 loc) · 9.17 KB
/
Bulk_CreateLab_ARM.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
[CmdletBinding()]
param(
[parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string] $CsvConfigFile,
[parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string] $CsvOutputFile,
[parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string] $ARMFile,
[parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[switch] $publish,
[parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[switch] $force
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Make sure the input file does exist
if (-not (Test-Path -Path $CsvConfigFile)) {
Write-Error "Input CSV File must exist, please choose a valid file location..."
}
# Make sure the output file doesn't exist
if ((Test-Path -Path $CsvOutputFile) -and (-not $force.IsPresent)) {
Write-Error "Output File cannot already exist, please choose a location to create a new output file..."
}
Import-Module ".\Utilities.psm1" -Force
$scriptstartTime = Get-Date
Write-Host "Executing Lab Creation Script, starting at $scriptstartTime" -ForegroundColor Green
$labs = $CsvConfigFile | Import-LabsCsv
$jobs = @{}
ForEach ($lab in $labs) {
# Reduce image get cost.
if (-not [boolean](Get-Variable $($lab.LabPlanName) -ErrorAction SilentlyContinue)) {
Write-Host "Getting new images $($lab.LabPlanName)"
New-Variable -Name $lab.LabPlanName -Value $(Get-AzLabServicesPlanImage -ResourceGroupName $lab.ResourceGroupName -LabPlanName $lab.LabPlanName)
}
#Handle tags
$tagParam = @{}
if ($lab.Tags) {
$tags = $lab.Tags.Split(";")
foreach($tag in $tags) {
$tagParts = $tag.Split("=")
$tagParam.Add($tagParts[0],$tagParts[1])
}
}
$hashParam = @{LabName = $($lab.LabName); `
Title = $($lab.Title); `
Tags = $($tagParam); `
LabPlanName = $($lab.LabPlanName); `
Location = $($lab.Location); `
AadGroupId = $($lab.AadGroupId); `
SkuSize = $($lab.Size); `
Capacity = [int]$($lab.MaxUsers); `
UsageQuota = $($lab.UsageQuota); `
SharedPassword = $($lab.SharedPassword); `
DisconnectDelay = $(if ($lab.idleOsGracePeriod) {$lab.idleOsGracePeriod} else {"0"}); `
NoConnectDelay = $( if ($lab.idleNoConnectGracePeriod) {$lab.idleNoConnectGracePeriod} else {"0"}); `
IdleDelay = $(if ($lab.idleGracePeriod) {$lab.idleGracePeriod} else {"0"}); `
AdminUser = $($lab.UserName); `
AdminPassword = $($lab.Password); }
if ($lab.GpuDriverEnabled -eq "Enabled") {
$hashParam.Add("GpuDrivers", "Enabled")
} else {
$hashParam.Add("GpuDrivers", "Disabled")
}
if ($lab.UsageMode -eq "Restricted"){
$hashParam.Add("SecurityOpenAccess","Disabled")
} else {
$hashParam.Add("SecurityOpenAccess","Enabled")
}
if ($lab.SharedGalleryId) {
$image = Get-AzGalleryImageDefinition -ResourceId $lab.SharedGalleryId
$hashParam.Add("ImageOffer", $image.Identifier.Offer)
$hashParam.Add("ImagePublisher", $image.Identifier.Publisher)
$hashParam.Add("ImageSku", $image.Identifier.Sku)
$hashParam.Add("ImageVersion", "latest")
} else {
$images = Get-Variable -Name $lab.LabPlanName -ValueOnly
$image = $images | Where-Object { ($_.DisplayName -eq $lab.ImageName) -and ($_.EnabledState.ToString() -eq "Enabled")}
if (-not ($image)) {
Write-Host "Unable to find an image with display name $($lab.ImageName)"
exit
}
if (($image | Measure-Object).Count -gt 1) {
Write-Host "Found multiples images with display name $($lab.ImageName)"
exit
}
if ($image.EnabledState.ToString() -ne "Enabled") {
Write-Host "Image $($image.DisplayName) not enabled, enabling."
Update-AzLabServicesPlanImage -LabPlanName $lab.LabPlanName -ResourceGroupName $lab.ResourceGroupName -Name $image.Name -EnabledState "Enabled"
New-Variable -Name $lab.LabPlanName -Value $(Get-AzLabServicesPlanImage -ResourceGroupName $lab.ResourceGroupName -LabPlanName $lab.LabPlanName) -Force
}
$hashParam.Add("ImageOffer", $image.Offer)
$hashParam.Add("ImagePublisher", $image.Publisher)
$hashParam.Add("ImageSku", $image.Sku)
$hashParam.Add("ImageVersion", $image.Version)
}
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# Create Users array
$hashParam.Add("LabUsers", $lab.Emails)
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# Create Schedules Array
$scheduleArray = @()
if ($lab.Schedules) {
foreach($schedule in $lab.Schedules) {
$sdate = [datetime]::Parse($schedule.FromDate)
$stime = [datetime]::Parse($schedule.StartTime.Replace('"',''))
$startd = [datetime]::New($sdate.Year, $sdate.Month, $sdate.Day, $stime.Hour, $stime.Minute, 0)
$fullStart = $startd.ToString('u')
$etime = [datetime]::Parse($schedule.EndTime.Replace('"',''))
$endd = [datetime]::New($sdate.Year, $sdate.Month, $sdate.Day, $etime.Hour, $etime.Minute, 0)
$fullEnd = $endd.ToString('u')
$edate = [datetime]::Parse($schedule.ToDate.Replace('"',''))
$duntil = [datetime]::New($edate.Year, $edate.Month, $edate.Day, 23, 59, 59)
$fullUntil = $duntil.ToString('u')
$weekdays = @() #$null
foreach ($day in ($schedule.WeekDays -Split ";")) {
$weekdays += $day.Trim("""").ToString()
}
if ($schedule.Frequency -eq "Once") {
$singleEvent = @{
startAt = $fullStart
stopAt = $fullEnd
timeZoneId = $($schedule.TimeZoneId)
notes = $($schedule.Notes)
}
$scheduleArray += $singleEvent
} else {
$repeatEvent = @{
startAt = $fullStart
stopAt = $fullEnd
timeZoneId = $($schedule.TimeZoneId)
notes = $($schedule.Notes)
recurrencePattern = @{
frequency = $($schedule.Frequency)
weekDays = $weekdays
interval = 1
expirationDate = $(Get-Date $fullUntil)
}
}
$scheduleArray += $repeatEvent
}
}
$hashParam.Add("LabSchedules", $scheduleArray)
} else {
$hashParam.Add("LabSchedules", @())
}
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Write-Host "Start creating lab $($lab.LabName)"
$key = $lab.ResourceGroupName + ":" + $lab.LabName
$value = New-AzResourceGroupDeployment -Name $lab.LabName -AsJob -ResourceGroupName $($lab.ResourceGroupName) -TemplateFile $ARMFile -TemplateParameterObject $hashParam | Get-Job
$jobs.Add($key,$value)
}
Watch-Jobs -Labs $labs -Jobs $jobs -ResultColumnName "CreateLabResult" | Out-Null
Write-Host "All Labs Creation finished, total duration $([math]::Round(((Get-Date) - $scriptstartTime).TotalMinutes, 1)) minutes" -ForegroundColor Green
$labs | Export-LabsCsv -CsvConfigFile $CsvOutputFile -Force:$true
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
if ($true) {
#if ($publish.IsPresent) {
$jobs = @{}
$pubscriptstartTime = Get-Date
Write-Host "Executing Lab Creation Publish Script, starting at $pubscriptstartTime" -ForegroundColor Green
foreach ($lab in $labs) {
$key = $lab.ResourceGroupName + ":" + $lab.LabName
$value = Publish-AzLabServicesLab -Name $lab.LabName -ResourceGroupName $lab.ResourceGroupName -AsJob | Get-Job
$jobs.Add($key,$value)
}
Watch-Jobs -Labs $labs -Jobs $jobs -ResultColumnName "PublishLabResult" | Out-Null
$labs | Export-LabsCsv -CsvConfigFile $CsvOutputFile -Force:$true
Write-Host "Lab Publish finished, total duration $([math]::Round(((Get-Date) - $pubscriptstartTime).TotalMinutes, 1)) minutes" -ForegroundColor Green
}
Write-Host "Completed running Bulk Lab ARM Creation script, total duration $([math]::Round(((Get-Date) - $scriptstartTime).TotalMinutes, 1)) minutes" -ForegroundColor Green