-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathACDLab.psm1
474 lines (370 loc) · 14.8 KB
/
ACDLab.psm1
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<#
.SYNOPSIS
Creates a lab environemnt contained in it's own Azure Resource Group by copying a snapshot VHD from
a parent resource group.
.PARAMETER LabNumber
Specifies the number of labs to create. The default is 1.
.PARAMETER LabName
Speciies the name of a lab when creating a single instance.
.PARAMETER VMSize
Specifies the size of the Azure VM. Possible choices are Standard_D4_v3 and Standard_D8_v3.
The default is Standard_D4_v3
.PARAMETER SnapshotResourceGroup
Specifies the resource group to copy the parent snapshot from.
The default is is parentResourceGroup.
.PARAMETER SnapShotName
Specifies the name of the parent snapshot VHD to copy the lab from.
.Example
PS C:\> Connect-AzureRmAccount
Name : [[email protected], 1e32f512-e00a-4c31-a31f-7c84d2bc66a1]
Account : [email protected]
SubscriptionName : Consortium Lab Azure Environment
TenantId : 257c335d-a2e7-5b65-9736-1a043ea0d3f7
Environment : AzureCloud
PS C:\>New-ACDLab -LabNumber 5
This example demonstrates connecting to an Azure account and then creating 5 ACD training labs
.EXAMPLE
PS C:\>New-ACDLab -SnapshotResourceGroup parentKaliLab -SnapShotName KaliLab
This example demonstrates creating a training lab using a custom (non-default) parent VHD
.EXAMPLE
PS C:\>New-ACDLab -LabName Student8
This example demonstrates create a single training lab with the name of Student8
#>
function New-ACDLab
{
[CmdletBinding(DefaultParameterSetName = 'Default')]
param
(
[Parameter(Position = 1, ParameterSetName = 'Default')]
[int]
$LabNumber = 1,
[Parameter(Position = 1, ParameterSetName = 'SingleInstance')]
[string]
$LabName,
[Parameter(Position = 2, ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'SingleInstance')]
[string]
$SnapshotResourceGroup = 'parentResourceGroup',
[Parameter(Position = 3, ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'SingleInstance')]
[ValidateSet('Standard_D4_v3', 'Standard_D8_v3')]
[string]
$VMSize = 'Standard_D4s_v3',
[Parameter(Position = 4, ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'SingleInstance')]
[string]
$SnapShotName = 'CyberLabParent'
)
$azureContext = Get-AzureRmContext
# Check if a subscription is found in the context. If not, login.
if ($null -eq $azureContext)
{
Connect-AzureRmAccount
}
if ($PSCmdlet.ParameterSetName -eq 'SingleInstance')
{
$labNumber = 1
}
$labCount = 1..$LabNumber
foreach ($lab in $labCount)
{
# lets try to get the Snapshot first and fail if not found
$snapShot = Get-AzureRmSnapshot -ResourceGroupName $SnapshotResourceGroup -SnapshotName $SnapShotName -ErrorAction Stop
if ($PSCmdlet.ParameterSetName -eq 'SingleInstance')
{
$studentName = $LabName
}
else
{
$studentName = 'Student' + $lab
}
New-AzureRmResourceGroup -Name $studentName -Location EastUS -ErrorAction Stop
$diskConfig = New-AzureRmDiskConfig -Location $snapShot.Location -SourceResourceId $snapShot.Id -CreateOption Copy
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $studentName -DiskName ($studentName + 'OS')
$virtualNetwork = New-AzureRmVirtualNetwork -ResourceGroupName $studentName -Location EastUS -Name cyberVnet -AddressPrefix '10.0.0.0/16'
$subnetConfig = Add-AzureRmVirtualNetworkSubnetConfig -Name default -AddressPrefix '10.0.0.0/24' -VirtualNetwork $virtualNetwork
$subnetConfig | Set-AzureRmVirtualNetwork
$virtualMachine = New-AzureRmVMConfig -VMName HyperV -VMSize $VMSize
$virtualMachine = Set-AzureRmVMOSDisk -VM $virtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows
$publicIp = New-AzureRmPublicIpAddress -Name hyperv_ip -ResourceGroupName $studentName -Location $snapShot.Location -AllocationMethod Dynamic
$vnet = $virtualNetwork | Get-AzureRmVirtualNetwork
$nic = New-AzureRmNetworkInterface -Name hyperv_nic -ResourceGroupName $studentName -Location $snapShot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$virtualMachine = Add-AzureRmVMNetworkInterface -VM $virtualMachine -Id $nic.Id
New-AzureRmVM -VM $virtualMachine -ResourceGroupName $studentName -Location $snapShot.Location -AsJob
}
# We have to wait for the VMs to be provisioned or Set-ACDLabAutoShutdown will fail
Get-Job | Wait-Job
Set-ACDLabAutoShutdown
}
<#
.SYNOPSIS
Configures ACD training labs to automatic shutdown at the specified time.
Current this is hard coded to 1900 Easter Standard Time.
.PARAMETER ResourceGroupSuffix
The Set-ACDLabAutoShutdown function will apply the auto shutdown resource to all the VMs in a specified resource.
A regular expression is used to filter the desired resource groups to apply the auto shutdown policy to.
The default is ^Student, which will apply the policy to all resource groups the start with "Student".
.PARAMETER ShutdownTimeZone
Specifies the time zone the VM is in. Default is Eastern Standard Time.
.EXAMPLE
PS C:\> Set-ACDLabAutoShutdown
This example will apply the auto shutdown policy to all VMs a resource groups that starts with Student.
.EXAMPLE
PS C:\> Set-ACDLabAutoShutdown -ResouceGroupName Student5 -ShutdownTime 2100
This example will apply the auto shutdown policy to the VM in the Student5 resource group at 2100 (9 PM)
#>
function Set-ACDLabAutoShutdown
{
[CmdletBinding()]
param
(
[Parameter()]
[string]
$ResourceGroupName = '^Student',
[Parameter()]
[int]
$ShutdownTime = 1900,
[Parameter()]
[string]
$ShutdownTimeZone = 'Eastern Standard Time'
)
if ($PSBoundParameters.ContainsKey('ResourceGroupName'))
{
$ResourceGroupName = '^' + $ResourceGroupName + '$'
}
$resourceGroups = Get-AzureRmResourceGroup | Where-Object -Property ResourceGroupName -Match $ResourceGroupName
foreach ($resourceGroup in $resourceGroups.ResourceGroupName)
{
$virtualMachines = Get-AzureRmVm -ResourceGroupName $resourceGroup
foreach ($virtualMachine in $virtualMachines)
{
$vmName = $virtualMachine.Name
$properties = @{
"status" = "Enabled"
"taskType" = "ComputeVmShutdownTask"
"dailyRecurrence" = @{ "time" = $shutdownTime }
"timeZoneId" = $shutdownTimezone
"notificationSettings" = @{
"status" = "Disabled"
"timeInMinutes" = 30
}
"targetResourceId" = $virtualMachine.Id
}
$azureRmResourceParameters = @{
ResourceId = ("/subscriptions/{0}/resourceGroups/{1}/providers/microsoft.devtestlab/schedules/shutdown-computevm-{2}" -f (Get-AzureRmContext).Subscription.Id, $resourceGroup, $vmName)
Location = $virtualMachine.Location
Properties = $properties
Force = $true
}
New-AzureRmResource @azureRmResourceParameters
}
}
}
<#
.SYNOPSIS
Removes/deletes a ACD training lab environments
.PARAMETER ResourceGroupName
The Remove-ACDLab function will apply the auto shutdown resource to all the VMs in a specified resource.
A regular expression is used to filter the desired resource groups to apply the auto shutdown policy to.
The default is ^Student, which will apply the policy to all resource groups the start with "Student".
.PARAMETER Force
Use when it is intened to remove all labs without interaction.
.EXAMPLE
PS C:\> Remove-ACDLab -ResourceGroupName Student3
This example will remove the lab in the Student3 resource group.
.EXAMPLE
PS C:\>Remove-ACDLab
Confirm
Are you sure you want to remove resource group 'student6'
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): y
True
.EXAMPLE
PS C:\>Remove-ACDLab -Force
This example will remove all the ACD training labs without interaction.
#>
function Remove-ACDLab
{
[CmdletBinding(SupportsShouldProcess=$true)]
param
(
[Parameter()]
[string]
$ResourceGroupName = '^Student',
[Parameter()]
[switch]
$Force
)
$azureContext = Get-AzureRmContext
# Check if a subscription is found in the context. If not, login.
if ($null -eq $azureContext)
{
Connect-AzureRmAccount
}
# If an argument is passed to ResourceGroupName we need to pad it with regex anchors to prevent accidental deletion
if ($PSBoundParameters.ContainsKey('ResourceGroupName'))
{
$ResourceGroupName = '^' + $ResourceGroupName + '$'
}
$resourceGroupsToDelete = Get-AzureRmResourceGroup | Where-Object -Property ResourceGroupName -Match $ResourceGroupName
foreach ($resourceGroup in $resourceGroupsToDelete)
{
$parameters = @{
ResourceGroupName = $resourceGroup.ResourceGroupName
}
if ($Force)
{
$parameters.AsJob = $true
$parameters.Force = $true
}
Remove-AzureRmResourceGroup @parameters
}
}
<#
.SYNOPSIS
Downloads the RDP file for all the provisioned ACD training labs.
Defaults to OneDrive\CyberLab
.EXAMPLE
PS C:\>Get-ACDLabDesktopFile
This demonstrates how to download all the RDP files for the provisioned labs to $env:HOMEPATH\OneDrive\CyberLab
.EXAMPLE
PS C:\>Get-ACDLabDesktopFile -Path 'C:\labRdpFiles'
This example is downloading the RDP files for all the provisioned labs to C:\labRdpFiles
#>
function Get-ACDLabDesktopFile
{
[CmdletBinding()]
param
(
[Parameter()]
[string]
$Path
)
if (-not $Path)
{
$oneDrivePath = Resolve-Path -Path '~\OneDrive'
$cyberLabPath = Join-Path -Path $oneDrivePath -ChildPath 'CyberLab'
}
else
{
$cyberLabPath = $Path
}
if (Test-Path -Path $cyberLabPath)
{
$resourceGroups = Get-AzureRmResourceGroup | Where-Object -Property ResourceGroupName -Match '^Student'
$vms = $resourceGroups | Get-AzureRmVm
foreach ($vm in $vms)
{
$outputPath = Join-Path -Path $cyberLabPath -ChildPath ($vm.ResourceGroupName + '.rdp')
$getRdpFileParameters = @{
ResourceGroupName = $vm.ResourceGroupName
Name = $vm.Name
LocalPath = $outputPath
}
Get-AzureRmRemoteDesktopFile @getRdpFileParameters
}
}
else
{
throw "$cyberLabPath not found"
}
}
<#
.SYNOPSIS
After the lab parent VHD is updated a snapshot needs to be taken before a training lab
can be created from it. This function creates a new VHD snapshot.
.PARAMETER ResourceGroupName
Specifies the resource group name the parent VHD is in.
The default is parentResourceGroup.
.PARAMETER VMName
Specifies the name of the parent VM the snapshot is taken from.
The default is LabParent
.EXAMPLE
PS C:\>New-ACDLabSnapShot
This examples shows how to create a new snapshot with default parameter values.
.EXAMPLE
PS C:\>New-ACDLabSnapshot -ResourceGroupName KaliLab -VMName KaliLab -SnapshotName KaliLabParent
This example creates a VHD snapshot from the KaliLab resource group,
with the KaliParent VM
#>
function New-ACDLabSnapshot
{
[CmdletBinding()]
param
(
[Parameter()]
[string]
$ResourceGroupName = 'parentResourceGroup',
[Parameter()]
[string]
$VMName = 'LabParent',
[Parameter()]
[string]
$SnapshotName = 'CyberLabParent'
)
$vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName
$snapShot = New-AzureRmSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id -Location EastUS -CreateOption Copy
New-AzureRmSnapshot -Snapshot $snapShot -SnapshotName $SnapshotName -ResourceGroupName $ResourceGroupName
}
<#
.SYNOPSIS
Update the ACD lab snapshot that acts as the parent image the labs are created from
.PARAMETER ResourceGroupName
Specifies the resource group name the parent VHD is in.
The default is parentResourceGroup.
.PARAMETER VMName
Specifies the name of the parent VM the snapshot is taken from.
The default is LabParent
.PARAMETER SnapshotName
Specifies the name of the Snapshot being created.
The default is CyberLabParent
.EXAMPLE
PS C:\>Update-ACDLabSnapshot
This example updates the VHD snapshot with defualt values. The parentResourceGroup resource group with VMName LabParent.
.EXAMPLE
PS C:\>Update-ACDLabSnapshot -ResourceGroupName KaliLab -VMName KaliLab -SnapshotName KaliLabParent
This example updates the VHD snapshot from the KaliLab resource group,
with the KaliParent VM
#>
function Update-ACDLabSnapshot
{
[CmdletBinding(SupportsShouldProcess=$true)]
param
(
[Parameter()]
[string]
$ResourceGroupName = 'parentResourceGroup',
[Parameter()]
[string]
$VMName = 'LabParent',
[Parameter()]
[string]
$SnapshotName = 'CyberLabParent'
)
Write-Warning "To update the lab parent snapshot you must remvoe the old snapshot and recreate a new snapshot."
Remove-AzureRmSnapshot -ResourceGroupName $ResourceGroupName -SnapshotName $SnapshotName
New-ACDLabSnapshot -ResourceGroupName $ResourceGroupName -VMName $VMName -SnapshotName $SnapshotName
}
<#
.SYNOPSIS
Shuts down an ACD training lab
.PARAMETER ResourceGroupName
Specifies the resource group of lab name to shutdown.
The default is Student
.Example
PS C:\>Stop-ACDLab -ResourceGroupName Student3
This example turn off the lab for Student3
.Example
PS C:\>Stop-ACDLab
When ran without arguments (default) all training labs will be turned off
#>
function Stop-ACDLab
{
[CmdletBinding()]
param
(
[Parameter()]
[string]
$ResourceGroupName = '^Student'
)
Get-AzureRmResourceGroup | Where-Object -Property ResourceGroupName -Match $ResourceGroupName | Get-AzureRmVm | Stop-AzureRmVM -AsJob -Force
}