54
54
55
55
. EXAMPLE
56
56
57
- & '.\Step 4A-1. Build Azure VM.ps1' -VMName DTOLAB-WK21 -OSType Workstation -JoinDomain -Domain CONTOSO.local -Credentials (Get-Credential)
57
+ & '.\Step 4A-1. Build Azure VM.ps1' -VMName CONTOSO-WK1 -OSType Workstation -JoinDomain -Domain CONTOSO.local -DomainJoinCreds (Get-Credential)
58
58
59
59
RESULT: Builds a Windows 10 VM named CONTOSO-WK1 and attempts to join it to domain CONTOSO.local using credentials
60
60
61
61
. EXAMPLE
62
62
63
- & '.\Step 4A-1. Build Azure VM.ps1' -VMName DTOLAB-WK21 -OSType Workstation -JoinDomain -Domain CONTOSO.local -Credentials (Get-Credential) -OU "OU=Workstations,OU=Region1,DC=CONTOSO,DC=LOCAL"
63
+ & '.\Step 4A-1. Build Azure VM.ps1' -VMName CONTOSO-WK1 -OSType Workstation -JoinDomain -Domain CONTOSO.local -DomainJoinCreds (Get-Credential) -OU "OU=Workstations,OU=Region1,DC=CONTOSO,DC=LOCAL"
64
64
65
65
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
66
66
71
71
[string ]$VMName ,
72
72
73
73
[ValidateSet (' Workstation' , ' Server' )]
74
- [string ]$OSType ,
74
+ [string ]$OSType = ' Server ' ,
75
75
76
76
[Parameter (ParameterSetName = ' JoinDomain' )]
77
77
[switch ]$SecureVM ,
87
87
[string ]$OU ,
88
88
89
89
[Parameter (Mandatory = $true , ParameterSetName = ' JoinDomain' )]
90
- [SecureString ] $Credentials
90
+ [System.Management.Automation.PSCredential ] $DomainJoinCreds
91
91
)
92
92
93
93
$ErrorActionPreference = " Stop"
@@ -272,30 +272,29 @@ $VMConfig = Set-AzVMOperatingSystem -VM $VMConfig -Windows -ComputerName $AzureS
272
272
$VMConfig = Add-AzVMNetworkInterface - VM $VMConfig - Id $NIC.Id
273
273
274
274
# Set VM operating system parameters
275
- Switch ($OSType ){
276
-
277
- ' Workstation' {
278
- $VMConfig = Set-AzVMSourceImage - VM $VMConfig `
279
- - PublisherName ' MicrosoftWindowsDesktop' `
280
- - Offer ' Windows-10' `
281
- - Skus ' rs5-enterprise' `
282
- - Version latest
283
- }
284
- ' Server' {
285
- $VMConfig = Set-AzVMSourceImage - VM $VMConfig `
286
- - PublisherName ' MicrosoftWindowsServer' `
287
- - Offer ' WindowsServer' `
288
- - Skus ' 2016-Datacenter' `
289
- - Version latest
290
- }
291
- default {
292
- $VMConfig = Set-AzVMSourceImage - VM $VMConfig `
293
- - PublisherName ' MicrosoftWindowsServer' `
294
- - Offer ' WindowsServer' `
295
- - Skus ' 2016-Datacenter' `
296
- - Version latest
275
+ If ($OSType ){
276
+ Switch ($OSType ){
277
+
278
+ ' Workstation' {
279
+ $VMConfig = Set-AzVMSourceImage - VM $VMConfig `
280
+ - PublisherName ' MicrosoftWindowsDesktop' `
281
+ - Offer ' Windows-10' `
282
+ - Skus ' rs5-enterprise' `
283
+ - Version latest
284
+ }
285
+ ' Server' {
286
+ $VMConfig = Set-AzVMSourceImage - VM $VMConfig `
287
+ - PublisherName ' MicrosoftWindowsServer' `
288
+ - Offer ' WindowsServer' `
289
+ - Skus ' 2016-Datacenter' `
290
+ - Version latest
291
+ }
297
292
}
298
293
}
294
+ Else {
295
+ $VMConfig = Set-AzVMSourceImage - VM $VMConfig - PublisherName ' MicrosoftWindowsServer' - Offer ' WindowsServer' - Skus ' 2016-Datacenter' - Version latest
296
+ }
297
+
299
298
300
299
301
300
@@ -347,7 +346,6 @@ If($SecureVM){
347
346
}
348
347
Catch {
349
348
Write-Host (" Failed: {0}" -f $_.Exception.message ) - ForegroundColor Black - BackgroundColor Red
350
- Break
351
349
}
352
350
}
353
351
Else {
@@ -371,58 +369,21 @@ If($SecureVM){
371
369
# Advisor Recommendation (Medium): Windows Defender Exploit Guard should be enabled on machines
372
370
# Advisor Recommendation (Low): Azure Backup should be enabled for virtual machines
373
371
}
374
- # region Reset VM password (Not working)
375
- <#
376
- #Re-reset password. Sometimes password set during deployment does not work
377
- $VM = Get-AzVM -ResourceGroupName $AzureSimpleConfig.ResourceGroupName -Name $AzureSimpleVM.Name
378
-
379
- Get-AzVM -ResourceGroupName $AzureSimpleConfig.ResourceGroupName -VMName $AzureSimpleVM.Name -Status
380
- #must grab the VM Computer Type handler
381
- $typeParams = @{
382
- 'PublisherName' = 'Microsoft.Compute'
383
- 'Type' = 'VMAccessAgent'
384
- 'Location' = $AzureSimpleConfig.LocationName
385
- }
386
- $typeHandlerVersion = (Get-AzVMExtensionImage @typeParams | Sort-Object Version -Descending | Select-Object -first 1).Version
387
-
388
- #remove the access extension
389
- Remove-AzVMAccessExtension -ResourceGroupName $AzureSimpleConfig.ResourceGroupName -VMName $AzureSimpleVM.Name -Name 'enablevmaccess' -Force
390
-
391
- #build params
392
- $extensionParams = @{
393
- Credential = $Credential
394
- VMName = $AzureSimpleVM.Name
395
- ResourceGroupName = $AzureSimpleConfig.ResourceGroupName
396
- Name = 'enablevmaccess'
397
- Location = $AzureSimpleConfig.LocationName
398
- TypeHandlerVersion = $typeHandlerVersion
399
- }
400
- #add enablevmaccess back with new creds
401
- Set-AzVMAccessExtension @extensionParams
402
- #Set-AzVMAccessExtension -Credential $Credential -ResourceGroupName $AzureSimpleConfig.ResourceGroupName -VMName $AzureSimpleVM.Name `
403
- -Name 'enablevmaccess' -TypeHandlerVersion $typeHandlerVersion -Location $AzureSimpleConfig.LocationName
404
- Update-AzVM -ResourceGroupName $AzureSimpleConfig.ResourceGroupName -VM $VM
405
- Restart-AzVM -ResourceGroupName $AzureSimpleConfig.ResourceGroupName -Name $AzureSimpleVM.Name
406
-
407
- #Reset the Remote Desktop Services configuration
408
- #Set-AzVMAccessExtension -ResourceGroupName $AzureSimpleConfig.ResourceGroupName -VMName $AzureSimpleVM.Name -Name "VMRDPAccess" `
409
- -Location $AzureSimpleConfig.LocationName -typeHandlerVersion "2.0" -ForceRerun:$true
410
- #>
411
- # endregion
372
+
412
373
413
374
If ($JoinDomain ){
414
375
# https://docs.microsoft.com/en-us/powershell/module/az.compute/set-azvmaddomainextension?view=azps-7.1.0
415
376
If ($OU ){
416
377
$DomainParams = @ {
417
378
DomainName = $Domain
418
- Credential = $credential
379
+ Credential = $DomainJoinCreds
419
380
JoinOption = 0x00000001
420
381
OUPath = $OU
421
382
}
422
383
}Else {
423
384
$DomainParams = @ {
424
385
DomainName = $Domain
425
- Credential = $credential
386
+ Credential = $DomainJoinCreds
426
387
JoinOption = 0x00000001
427
388
}
428
389
}
@@ -437,8 +398,8 @@ If($JoinDomain){
437
398
438
399
}
439
400
440
-
441
- Write-Host (" Done creating virtual machine [{0}]" -f $AzureSimpleVM.Name ) - ForegroundColor Green
442
- Write-Host " =================================================" - ForegroundColor Green
401
+ Write-Host " ================================================= " - ForegroundColor Black - BackgroundColor Green
402
+ Write-Host (" Done creating virtual machine [{0}]" -f $AzureSimpleVM.Name ) - ForegroundColor Black - BackgroundColor Green
403
+ Write-Host " =================================================" - ForegroundColor Black - BackgroundColor Green
443
404
444
405
Stop-Transcript
0 commit comments