-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStep 4B-2. Build Azure VM - TenantB.ps1
More file actions
480 lines (397 loc) · 20.4 KB
/
Copy pathStep 4B-2. Build Azure VM - TenantB.ps1
File metadata and controls
480 lines (397 loc) · 20.4 KB
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
475
476
477
478
479
480
<#
.SYNOPSIS
Builds a Azure VM
.DESCRIPTION
Builds a Azure VM for Site 2
.NOTES
1. Build VM configurations
2. Create a resource group
3. Create Storage Account
4. Creating a new NSG
5. Attach VM nic to subnet
6. Build local admin credentials
7. Set VM Configuration and boot info
8. Deploying virtual machine
9. Set Autoshutdown
10. Join Domain (optional)
TODO
- Prompt for VM name
- Prompt for hub or spoke
.PARAMETER VMName
STRING
Specifies an custom VM name; if already found increments name by 1
.PARAMETER OSType
SET
Decides to deploy the latest Windows 10 or latest Windows Server operating system
.PARAMETER SecureVM
SWITCH
.PARAMETER JoinDomain
SWITCH
.PARAMETER Domain
MANDATORY (if JoinDomain switch is used)
.PARAMETER OU
STRING (if JoinDomain switch is used)
.PARAMETER Credentials
MANDATORY (if JoinDomain switch is used)
.EXAMPLE
& '.\Step 4B-2. Build Azure VM -Region 2.ps1'
RESULT: Builds a Windows Server VM
.EXAMPLE
& '.\Step 4B-2. Build Azure VM -Region 2.ps1' -VMName CONTOSO-WK1 -OSType Workstation
RESULT: Builds a Windows 10 VM named CONTOSO-WK1
.EXAMPLE
& '.\Step 4B-2. Build Azure VM -Region 2.ps1' -VMName CONTOSO-WK1 -OSType Workstation -JoinDomain -Domain CONTOSO.local -DomainJoinCreds (Get-Credential)
RESULT: Builds a Windows 10 VM named CONTOSO-WK1 and attempts to join it to domain CONTOSO.local using credentials
.EXAMPLE
& '.\Step 4B-2. Build Azure VM -Region 2.ps1' -VMName CONTOSO-WK1 -OSType Workstation -JoinDomain -Domain CONTOSO.local -DomainJoinCreds (Get-Credential) -OU "OU=Workstations,OU=Region1,DC=CONTOSO,DC=LOCAL"
RESULT: Builds a Windows 10 VM named CONTOSO-WK1 and attempts to join it to domain CONTOSO.local in Region 1 workstation OU using credentials
.EXAMPLE
& '.\Step 4B-1. Build Azure VM -Region 1.ps1' -ConfigurationFile configs-gov.ps1 -VMName CONTOSO-WK1 -OSType Workstation -JoinDomain -Domain CONTOSO.local -DomainJoinCreds (Get-Credential) -OU "OU=Workstations,OU=Region1,DC=CONTOSO,DC=LOCAL"
RESULT: Builds a Windows 10 VM named CONTOSO-WK1 and attempts to join it to domain CONTOSO.local in Region 1 workstation OU using credentials in Azure Gov
#>
[CmdletBinding(DefaultParameterSetName = 'Workgroup')]
Param(
[Parameter(Mandatory = $false)]
[ArgumentCompleter( {
param ( $commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters )
$Configs = Get-Childitem $_ -Filter configs* | Where Extension -eq '.ps1' | Select -ExpandProperty Name
$Configs | Where-Object {
$_ -like "$wordToComplete*"
}
} )]
[Alias("config")]
[string]$ConfigurationFile = "configs.ps1",
[ValidatePattern("^(?![0-9]{1,64}$)[a-zA-Z0-9-]{1,64}$")]
[string]$VMName,
[ValidateSet('Workstation', 'Server')]
[string]$OSType = 'Server',
[switch]$SecureVM,
[Parameter(ParameterSetName = 'JoinDomain')]
[switch]$JoinDomain,
[Parameter(Mandatory = $true,ParameterSetName = 'JoinDomain')]
[string]$Domain,
[Parameter(Mandatory = $false,ParameterSetName = 'JoinDomain')]
[ValidatePattern("(?=OU)(.*\n?)(?<=.)")]
[string]$OU,
[Parameter(Mandatory = $true,ParameterSetName = 'JoinDomain')]
[System.Management.Automation.PSCredential]$DomainJoinCreds
)
$ErrorActionPreference = "Stop"
#Requires -Modules Az.Accounts,Az.Compute,Az.Resources,Az.Storage,Az.Network
Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings "true" | Out-Null
#https://docs.microsoft.com/en-us/azure/virtual-machines/linux/quick-create-powershell#create-a-virtual-machine
#https://docs.microsoft.com/en-us/azure/virtual-machines/windows/quick-create-powershell
#https://docs.microsoft.com/en-us/powershell/module/az.compute/new-azvm?view=azps-2.8.0
#region Grab Configurations
If($PSScriptRoot.ToString().length -eq 0)
{
Write-Host ("File not ran as script; Assuming its opened in ISE. ") -ForegroundColor Red
Write-Host (" Run configuration file first (eg: . .\$ConfigurationFile)") -ForegroundColor Yellow
Break
}
Else{
Write-Host ("Loading {0}..." -f "$PSScriptRoot\$ConfigurationFile") -ForegroundColor Yellow -NoNewline
. "$PSScriptRoot\$ConfigurationFile" -NoVyosISOCheck
}
#endregion
#start transcript
$LogfileName = "$SiteBName-BuildAzureVMSetup-$(Get-Date -Format 'yyyy-MM-dd_Thh-mm-ss-tt').log"
Try{Start-transcript "$PSScriptRoot\Logs\$LogfileName" -ErrorAction Stop}catch{Start-Transcript "$PSScriptRoot\$LogfileName"}
#install Devlabs for Arm Templates support
If((Get-AzResourceProvider -ProviderNamespace Microsoft.DevTestLab).RegistrationState -eq 'NotRegistered'){
Register-AzResourceProvider -ProviderNamespace Microsoft.DevTestLab | Out-Null
}
#region Build VM configurations
$VMs = Get-AzVM -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -ErrorAction SilentlyContinue
If($VMName)
{
If($VMName -in $VMs.Name){
Write-Host ("Name already exists. You must specify a different vm name other than [{0}]" -f $VMName) -ForegroundColor Red
do {
$VMresponse = Read-host "Whats the new VM name?"
} until ($VMresponse -match "^(?![0-9]{1,64}$)[a-zA-Z0-9-]{1,64}$" -and $VMresponse -ne $VMName)
$VMName = $VMresponse
}
#Azure vm can be 64 characters long but the computername cannot
$computername = $VMName | Set-TruncateString -length 15
$newVMname = $VMName.ToLower()
$newNIC = $VMName.ToLower() + '-ni'
}
Else{
#Increment VM name and nic
$i=1
do {
$computername = ($AzureVMSiteB.ComputerName -replace '\d+$', $i)
$newVMname = ($AzureVMSiteB.Name -replace '\d+$', $i)
#only replace last digit in name (incase multiple digits exist)
$newNIC = ($AzureVMSiteB.NICName -replace '\d(?!.*\d)', $i)
$i++
} until ($newVMname -notin $VMs.Name)
}
#Update Names in config
$AzureVMSiteB['ComputerName'] = $computername
$AzureVMSiteB['Name'] = $newVMname
$AzureVMSiteB['NICName'] = $newNIC
Write-Host ("Virtual Machine name will be [{0}]" -f $AzureVMSiteB.Name) -ForegroundColor Green
#endregion
#region 1. Create a resource group:
If(-Not(Get-AzResourceGroup -Name $AzureAdvConfigSiteB.ResourceGroupName -ErrorAction SilentlyContinue))
{
Write-Host ("Creating Azure resource group [{0}]..." -f $AzureAdvConfigSiteB.ResourceGroupName) -ForegroundColor White -NoNewline
Try{
New-AzResourceGroup -Name $AzureAdvConfigSiteB.ResourceGroupName -Location $AzureAdvConfigSiteB.LocationName | Out-Null
Write-Host "Done" -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
}
}Else{
Write-Host ("Using Azure resource group [{0}]" -f $AzureAdvConfigSiteB.ResourceGroupName) -ForegroundColor Green
}
#endregion
#region Create Storage Account
#build random char for storage name
If(-Not($StorageAccount = Get-AzStorageAccount -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName `
-ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Where {$_.Sku.Name -eq $AzureAdvConfigSiteB.StorageSku -and $_.StorageAccountName -like "$($SiteBName -replace '\W')*"} | Select -First 1)){
$randomChar = (-join ((65..90) + (97..122) | Get-Random -Count 5 | % {[char]$_})).ToString()
$storageName = ($SiteBName +'-' + $randomChar).ToLower() -replace '[\W]', ''
Write-Host ("Creating Azure storage account [{0}]..." -f $storageName) -ForegroundColor White -NoNewline
Try{
$StorageAccount = New-AzStorageAccount -Name $storageName -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -SkuName $AzureAdvConfigSiteB.StorageSku `
-Location $AzureAdvConfigSiteB.LocationName -Kind Storage | Out-Null
$AzureAdvConfigSiteB['StorageAccount'] = $storageName
Write-Host "Done" -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
Break
}
}
Else{
Write-Host ("Using Azure storage account [{0}]" -f $StorageAccount.StorageAccountName) -ForegroundColor Green
$AzureAdvConfigSiteB['StorageAccount'] = $StorageAccount.StorageAccountName
}
#endregion
#region Creating a new NSG to allow PS Remoting Port 5986 and RDP Port 3389
#grab Vnet for NSG and NIC configurations
$vNet = Get-AzVirtualNetwork -Name $AzureVMSiteB.VNetName -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -ErrorAction SilentlyContinue
If(-Not($NSG = Get-AzNetworkSecurityGroup -Name $AzureVMSiteB.NSGName -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue)){
Write-Host ("Creating Azure network security group [{0}]..." -f $AzureVMSiteB.NSGName) -ForegroundColor White -NoNewline
Try{
$NSG = New-AzNetworkSecurityGroup -Name $AzureVMSiteB.NSGName -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -Location $AzureAdvConfigSiteB.LocationName | Out-Null
$NSG | Add-AzNetworkSecurityRuleConfig -Name "Allow_Port_3389" -Priority 1200 -Protocol TCP -Access Allow -SourceAddressPrefix * `
-SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Direction Inbound | Set-AzNetworkSecurityGroup | Out-Null
Set-AzVirtualNetworkSubnetConfig -Name $AzureAdvConfigSiteB.VnetSpokeSubnetName -VirtualNetwork $vNet -AddressPrefix $AzureAdvConfigSiteB.VnetSpokeSubnetAddressPrefix[0] `
-NetworkSecurityGroup $NSG -WarningAction SilentlyContinue | Out-Null
$vNet | Set-AzVirtualNetwork -WarningAction SilentlyContinue | Out-Null
Write-Host "Done" -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
Break
}
}
Else{
Write-Host ("Using Azure network security group [{0}]" -f $AzureVMSiteB.NSGName) -ForegroundColor Green
}
#endregion
#region Attach VM to second subnet which should be defaultsubnet
$VMSubnet = $vNet.Subnets | Where Name -eq $AzureVMSiteB.SubnetName
Write-Host ("Attaching VM's network interface [{0}] to subnet [{1}]..." -f $AzureVMSiteB.NICName,$AzureVMSiteB.SubnetName) -ForegroundColor White -NoNewline
Try{
$NIC = New-AzNetworkInterface -Name $AzureVMSiteB.NICName -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName `
-Location $AzureAdvConfigSiteB.LocationName -SubnetId $VMSubnet.Id -Force
Write-Host "Done" -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
Break
}
#endregion
#region Build local admin credentials for VM
If($VMAdminPassword -notmatch '((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*()]).{8,123})' -or $VMAdminPassword -match 'password')
{
Write-Host ("You must specify a more complex password other than [{0}]" -f $VMAdminPassword) -ForegroundColor Red
$ChangePassword = Read-host "Would you like to set a new password? [Y or N]"
If($ChangePassword -eq 'Y'){
do {
$NewPassword = Read-host "Whats the new password?"
} until ($NewPassword -match '((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*()]).{8,123})')
$AzureVMSiteB['LocalAdminPassword'] = $NewPassword
}
Else{
Write-Host ("Unable to continue. Change config.ps1 variable [`$VMAdminPassword] value") -ForegroundColor Black -BackgroundColor Red
Break
}
}
Write-Host ("Building [{0}] credentials for VM [{1}]..." -f $AzureVMSiteB.LocalAdminUser,$AzureVMSiteB.Name) -ForegroundColor White -NoNewline
$LocalAdminSecurePassword = ConvertTo-SecureString $AzureVMSiteB.LocalAdminPassword -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($AzureVMSiteB.LocalAdminUser, $LocalAdminSecurePassword)
Write-Host "Done" -ForegroundColor Green
#endregion
#Set VM Configuration
$VMConfig = New-AzVMConfig -VMName $AzureVMSiteB.Name -VMSize $AzureVMSiteB.Size
#Set VM operating system parameters
$VMConfig = Set-AzVMOperatingSystem -VM $VMConfig -Windows -ComputerName $AzureVMSiteB.ComputerName -Credential $Credential `
-ProvisionVMAgent -EnableAutoUpdate
#Set VM network interface
$VMConfig = Add-AzVMNetworkInterface -VM $VMConfig -Id $NIC.Id
#Set VM operating system parameters
If($OSType){
Switch($OSType){
'Workstation' {
$VMConfig = Set-AzVMSourceImage -VM $VMConfig `
-PublisherName 'MicrosoftWindowsDesktop' `
-Offer 'Windows-10' `
-Skus 'rs5-enterprise' `
-Version latest
}
'Server' {
$VMConfig = Set-AzVMSourceImage -VM $VMConfig `
-PublisherName 'MicrosoftWindowsServer' `
-Offer 'WindowsServer' `
-Skus '2019-datacenter-gensecond' `
-Version latest
}
}
}
Else{
$VMConfig = Set-AzVMSourceImage -VM $VMConfig -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2016-Datacenter' -Version latest
}
#Set boot diagnostic storage account
$VMConfig = Set-AzVMBootDiagnostic -Enable -VM $VMConfig -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -StorageAccountName $AzureAdvConfigSiteB.StorageAccount -ErrorAction SilentlyContinue
Try{
Write-Host ("Deploying virtual machine [{0}]..." -f $AzureVMSiteB.Name) -ForegroundColor White -NoNewline
New-AzVM -VM $VMConfig -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -Location $AzureAdvConfigSiteB.LocationName | Out-Null
Write-Host "Done" -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
Break
}
#endregion
#region set autoshutdown (using custom function)
If($AzureVMSiteB.EnableAutoShutdown){
#determine is notification is by email or webhookurl; set the appropiate param
$EmailRegex = '^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$'
$URLRegex = '(http[s]?|[s]?ftp[s]?)(:\/\/)([^\s,]+)'
$ShutdownParam = @{Time=$AzureVMSiteB.ShutdownTime;TimeZone=$AzureVMSiteB.ShutdownTimeZone}
If($AzureVMSiteB.AutoShutdownNotificationType -match $EmailRegex){$ShutdownParam += @{Email=$AzureVMSiteB.AutoShutdownNotificationType}}
If($AzureVMSiteB.AutoShutdownNotificationType -match $URLRegex){$ShutdownParam +=@{WebhookUrl=$AzureVMSiteB.AutoShutdownNotificationType}}
Try{
Write-Host ("Setting AutoShutdown on virtual machine [{0}]..." -f $AzureVMSiteB.Name) -ForegroundColor White -NoNewline
Set-AzVMAutoShutdown -Enable -Name $AzureVMSiteB.Name -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName @ShutdownParam | Out-Null
Write-Host "Done" -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
}
}
#endregion
If($SecureVM){
# Advisor Recommendation (high): Virtual machines should encrypt temp disks, caches, and data flows between Compute and Storage resources
#https://docs.microsoft.com/en-us/azure/virtual-machines/windows/disk-encryption-powershell-quickstart
$KeyVaultName = ($LabPrefix + 'vmdiskkeys')
If(-Not($AzKeyVault = AzKeyVault -Name $KeyVaultName -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue)){
Write-Host ("Creating Azure Keyvault [{0}]..." -f $KeyVaultName) -ForegroundColor White -NoNewline
Try{
$AzKeyVault = New-AzKeyVault -Name $KeyVaultName -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -Location $AzureAdvConfigSiteB.LocationName -EnabledForDiskEncryption | Out-Null
Write-Host "Done" -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
Break
}
}
Else{
Write-Host ("Using Azure Azure Keyvault [{0}]" -f $KeyVaultName) -ForegroundColor Green
}
#$AzKeyVault = Get-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName
Write-Host ("Enabling Disk encryption on virtual Machine [{0}]..." -f $AzureVMSiteB.Name) -ForegroundColor White -NoNewline
Try{
Set-AzVMDiskEncryptionExtension -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -VMName $AzureVMSiteB.Name -DiskEncryptionKeyVaultUrl $AzKeyVault.VaultUri -DiskEncryptionKeyVaultId $AzKeyVault.ResourceId -Force
Write-Host ("Done. Key is stored in Azure KeyVault: {0}" -f $KeyVaultName) -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
}
#TODO
# Advisor Recommendation (high): Install endpoint protection solution on virtual machines
# Advisor Recommendation (high): SQL IaaS Agent should be installed in full mode
# Advisor Recommendation (Medium): Network traffic data collection agent should be installed on Windows virtual machines
# Advisor Recommendation (Medium): Windows Defender Exploit Guard should be enabled on machines
# Advisor Recommendation (Low): Azure Backup should be enabled for virtual machines
}
#region Reset VM password (Not working)
<#
#Re-reset password. Sometimes password set during deployment does not work
$VM = Get-AzVM -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -Name $AzureVMSiteB.Name
Get-AzVM -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -VMName $AzureVMSiteB.Name -Status
#must grab the VM Computer Type handler
$typeParams = @{
'PublisherName' = 'Microsoft.Compute'
'Type' = 'VMAccessAgent'
'Location' = $AzureAdvConfigSiteB.LocationName
}
$typeHandlerVersion = (Get-AzVMExtensionImage @typeParams | Sort-Object Version -Descending | Select-Object -first 1).Version
#remove the access extension
Remove-AzVMAccessExtension -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -VMName $AzureVMSiteB.Name -Name 'enablevmaccess' -Force
#build params
$extensionParams = @{
Credential = $Credential
VMName = $AzureVMSiteB.Name
ResourceGroupName = $AzureAdvConfigSiteB.ResourceGroupName
Name = 'enablevmaccess'
Location = $AzureAdvConfigSiteB.LocationName
TypeHandlerVersion = $typeHandlerVersion
}
#add enablevmaccess back with new creds
Set-AzVMAccessExtension @extensionParams
#Set-AzVMAccessExtension -Credential $Credential -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -VMName $AzureVMSiteB.Name `
-Name 'enablevmaccess' -TypeHandlerVersion $typeHandlerVersion -Location $AzureAdvConfigSiteB.LocationName
Update-AzVM -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -VM $VM
Restart-AzVM -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -Name $AzureVMSiteB.Name
#Reset the Remote Desktop Services configuration
#Set-AzVMAccessExtension -ResourceGroupName $AzureAdvConfigSiteB.ResourceGroupName -VMName $AzureVMSiteB.Name -Name "VMRDPAccess" `
-Location $AzureAdvConfigSiteB.LocationName -typeHandlerVersion "2.0" -ForceRerun:$true
#>
#endregion
If($JoinDomain){
#https://docs.microsoft.com/en-us/powershell/module/az.compute/set-azvmaddomainextension?view=azps-7.1.0
$DomainParams = @{
Name='domainjoinextension'
VMName=$AzureVMTenantA.Name
ResourceGroupName=$AzureAdvConfigTenantA.ResourceGroupName
}
If($OU){
$DomainParams += @{
DomainName=$Domain
Credential=$DomainJoinCreds
JoinOption=0x00000001
OUPath=$OU
}
}
Else{
$DomainParams += @{
DomainName=$Domain
Credential=$DomainJoinCreds
JoinOption=0x00000001
}
}
Try{
Write-Host ("Attempting to join [{0}] to domain [{1}]..." -f $AzureVMTenantA.Name,$Domain) -ForegroundColor White -NoNewline
Set-AzVMADDomainExtension @DomainParams -Restart -Verbose
Write-Host "Done" -ForegroundColor Green
}
Catch{
Write-Host ("Failed: {0}" -f $_.Exception.message) -ForegroundColor Black -BackgroundColor Red
}
}
Write-Host "=================================================" -ForegroundColor Black -BackgroundColor Green
Write-Host (" Done creating virtual machine [{0}]" -f $AzureVMSiteB.Name) -ForegroundColor Black -BackgroundColor Green
Write-Host "=================================================" -ForegroundColor Black -BackgroundColor Green
Stop-Transcript