-
Notifications
You must be signed in to change notification settings - Fork 17
/
deploy-rds-templates.ps1
1527 lines (1290 loc) · 61.7 KB
/
deploy-rds-templates.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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
powershell script to test and deploy azure rds quickstart templates
.DESCRIPTION
powershell script to test and deploy azure rds quickstart templates
https://github.com/Azure/rds-templates/
to enable script execution, you may need to Set-ExecutionPolicy Bypass -Force
Copyright 2017 Microsoft Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
.NOTES
file author: jagilber
file name : deploy-rds-templates.ps1
version : 170825 v1.0
.EXAMPLE
.\deploy-rds-templates.ps1 -adminPassword changeme3240e2938r92 -resourceGroup rdsdeptest -location eastus -installOptions rds-deployment-uber
Example command to deploy ad-domain-only-test,rds-deployment-existing-ad,rds-update-certificate,rds-ha-broker,rds-ha-gateway with 2 rdsh, 2 rdcb, and 2 rdgw instances using A2 machines.
the resource group is rdsdeptest and domain fqdn is rdsdeptest.lab
.EXAMPLE
.\deploy-rds-templates.ps1 -adminPassword changeme3240e2938r92 -resourceGroup rdsdeptest -admin cloudadmin -numberOfRdshInstances 5 -rdshVmSize Standard_A4 -imagesku 2012-r2-Datacenter -installOptions rds-deployment -location westus
Example command to deploy rds-deployment with 5 instances using A4 machines. the resource group is rdsdeptest and domain fqdn is rdsdeptest.lab.
the admin account is cloudadmin and OS is 2012-r2-datacenter
.EXAMPLE
.\deploy-rds-templates.ps1 -useExistingJson -parameterFileRdsDeployment c:\temp\rds-deployment.azuredeploy.parameters.json -location centralUs -installOptions rds-deployment-existing-ad
Example command to deploy rds-deployment-existing-ad with a custom populated parameter json file c:\temp\rds-deployment.azuredeploy.parameters.json.
since rds-deployment-existing-ad requires an existing domain, it will prompt to also install ad-domain-only-test.
all properties from json file will be used. if no password is supplied, it will prompt.
.EXAMPLE
.\deploy-rds-templates.ps1 -adminPassword changeme3240e2938r92 -resourceGroup rdsdeptest -monitor -postConnect -location eastus
Example command to deploy rds-deployment,rds-ha-broker,rds-ha-gateway,rds-update-certificate with 2 instances using A2 machines.
the resource group is rdsdeptest and domain fqdn is rdsdeptest.lab
before calling New-AzureRmResourceGroupDeployment, the powershell monitor script will be called.
after successful deployment, the post connect powershell script will be called.
.PARAMETER adminUsername
the name of the administrator account.
default is 'cloudadmin'
.PARAMETER adminPassword
the administrator account password in clear text. password needs to meet azure password requirements.
use -credentials to pass credentials securely
default is 'Password(get-random)!'
.PARAMETER applicationPassword
password to create / use for certificate access
default is $adminPassword
.PARAMETER certificateName
name of certificate to create / use
default is "$($resourceGroup)Certificate"
.PARAMETER clientAccessName
rds client access name for HA only.
non-ha will use rdcb-01
default for HA is 'hardcb'
.PARAMETER credentials
can be used for administrator account password. password needs to meet azure password requirements.
.PARAMETER dnsLabelPrefix
public DNS name label prefix for gateway.
default is <%resourceGroup%>.
.PARAMETER dnsServer
DNS server OS name
default is addc-01
.PARAMETER domainName
new AD domain fqdn used for this deployment.
NOTE: base domain name for example 'contoso' can not be longer than 15 chars
default is contoso.com.
.PARAMETER imageSKU
default 2016-datacenter or optional 2012-r2-datacenter for OS selection type
NOTE: for HA Broker, 2016 is required
.PARAMETER installOptions
array deployment templates to deploy in order specified.
options are:
"ad-domain-only-test", (for testing purposes only)
"rds-deployment",
"rds-update-certificate",
"rds-deployment-ha-broker",
"rds-deployment-ha-gateway",
"rds-existing-ad",
"rds-deployment-uber",
"rds-update-rdsh-collection"
default is rds full rds-deployment: "rds-deployment", "rds-update-certificate", "rds-deployment-ha-broker", "rds-deployment-ha-gateway"
.PARAMETER location
is the azure regional datacenter location.
default will display list of locations for use
.PARAMETER monitor
will run "https://aka.ms/azure-rm-log-reader.ps1" before deployment in separate powershell process
.PARAMETER numberofRdshInstances
number of remote desktop session host instances to create.
default value is 2
.PARAMETER numberofWebGwInstances
number of additional remote desktop gateway instances to create for HA gateway mode.
default value is 1
.PARAMETER parameterFileRdsDeployment
path to template json parameter file for rds-deployment
if -useExistingJson, existing json parameter file will be used without validation or modification
default is $env:TEMP\rds-deployment.azuredeploy.parameters.json
if not exists and not -useExistingJson base template from $templateBaseRepoUri/rds-deployment/azuredeploy.parameters.json will be used
.PARAMETER parameterFileRdsDeployment
path to template json parameter file for rds-deployment-existing-ad
if -useExistingJson, existing json parameter file will be used without validation or modification
default is $env:TEMP\rds-deployment-existing-ad.azuredeploy.parameters.json
if not exists and not -useExistingJson base template from $templateBaseRepoUri/rds-deployment/azuredeploy.parameters.json will be used
.PARAMETER parameterFileRdsHaBroker
path to template json parameter file for rds-deployment-ha-broker
if -useExistingJson, existing json parameter file will be used without validation or modification
default is $env:TEMP\rds-deployment-ha-broker.azuredeploy.parameters.json
if not exists and not -useExistingJson base template from $templateBaseRepoUri/rds-deployment-ha-broker/azuredeploy.parameters.json will be used
.PARAMETER parameterFileRdsHaGateway
path to template json parameter file for rds-deployment-ha-gateway
if -useExistingJson, existing json parameter file will be used without validation or modification
default is $env:TEMP\rds-deployment-ha-gateway.azuredeploy.parameters.json
if not exists and not -useExistingJson base template from $templateBaseRepoUri/rds-deployment-ha-gateway/azuredeploy.parameters.json will be used
.PARAMETER parameterFileRdsUber
path to template json parameter file for rds-deployment-uber
if -useExistingJson, existing json parameter file will be used without validation or modification
default is $env:TEMP\rds-deployment-uber.azuredeploy.parameters.json
if not exists and not -useExistingJson base template from $templateBaseRepoUri/rds-deployment-uber/azuredeploy.parameters.json will be used
.PARAMETER parameterFileRdsUpdateCertificate
path to template json parameter file for rds-update-certificate
if -useExistingJson, existing json parameter file will be used without validation or modification
default is $env:TEMP\rds-udpate-certificate.azuredeploy.parameters.json
if not exists and not -useExistingJson base template from $templateBaseRepoUri/rds-deployment-update-certificate/azuredeploy.parameters.json will be used
.PARAMETER parameterFileRdsUpdateRdshCollection
path to template json parameter file for rds-update-rdsh-collection
if -useExistingJson, existing json parameter file will be used without validation or modification
default is $env:TEMP\rds-udpate-rdsh-collection.azuredeploy.parameters.json
if not exists and not -useExistingJson base template from $templateBaseRepoUri/rds-deployment-update-rdsh-collection/azuredeploy.parameters.json will be used
.PARAMETER pause
switch to enable pausing between deployments for verification
.PARAMETER pfxFilePath
path to existing certificate to use with rds-update-certificate
certificate should have private key
default will generate a wildcard '*.contoso.com' self signed cert for testing purposes only
.PARAMETER postConnect
will run "https://aka.ms/azure-rm-rdp-post-deployment.ps1" following deployment
.PARAMETER primaryDBConnectionString
ODBC connection string for HA Broker and uber deployments. should be similar to following syntax
DRIVER=SQL Server Native Client 11.0;Server={enter_sql_server_here},1433;Database={enter_sql_database_here};Uid={enter_sql_admin_here}@{enter_sql_server_here};Pwd={enter_sql_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;
.PARAMETER publicIPAddressName
is the public ip address name.
default is 'gwpip'
.PARAMETER rdshCollectionName
name of rds collection for use with rds-update-rdsh-collection
.PARAMETER rdshVmSize
size is of the azure vm's to use.
default is 'Standard_A2'
.PARAMETER rdshTemplateImageUri
uri to blob storage containing a vhd of image to use for rds-update-rdsh-collection
vhd OS image should have been sysprepped with c:\windows\system32\sysprep.exe -oobe -generalize
also, in azure, set-azurermvm -ResourceGroupName $resourceGroup -Name $vm.Name -Generalized
.PARAMETER rdshUpdateIteration
used to designate new deployment vm / OS name
example:
rdsh-01 (default)
rdsh-101 (name of vm with rdshUpdateIteration set to 1)
default is null
.PARAMETER resourceGroup
resourceGroup is a mandatory parameter and is the azure arm resourcegroup to use / create for this deployment.
default is 'resourceGroup(get-random)'
.PARAMETER sqlServer
OS name of existing sql server to use if not using Azure SQL
.PARAMETER subnetName
name of subnet to create / use.
default is 'subnet'
.PARAMETER templateBaseRepoUri
base template path for artifacts / scripts / dsc / templates
default "https://raw.githubusercontent.com/Azure/RDS-Templates/master/"
.PARAMETER tenantId
tenantId to be used in subscription for deployment
default will be enumerated
.PARAMETER useExistingJson
will use existing json file for arguments when deploying instead of overwriting
.PARAMETER vaultName
name of vault to use / create for certificate use
default is "$(resourceGroup)Cert"
.PARAMETER vnetName
name of vnet to create / use
default is 'vnet'
.PARAMETER whatIf
to test script actions with configuration but will not deploy
#>
[CMDLETBINDING()]
param(
[string]$random = ((get-random).ToString().Substring(0, 9)), # positional requirement
[string]$adminUserName = "cloudadmin",
[string]$adminPassword = "Password$($random)!",
[string]$applicationId,
[string]$brokerName = "rdcb-01",
[string]$resourceGroup = "resgrp$($random)",
[string]$domainName = "$($resourceGroup).lab",
[string]$certificateName = "$($resourceGroup)Certificate",
[string]$applicationPassword = $adminPassword,
[string]$clientAccessName = "HARDCB",
[pscredential]$credentials,
[string]$dnsLabelPrefix = "$($resourceGroup.ToLower())", # has to be unique
[string]$dnsServer = "addc-01",
[string]$gatewayLoadBalancer = "loadbalancer",
[string]$gwAvailabilitySet = "gw-availabilityset",
[string[]][ValidateSet("ad-domain-only-test", "rds-deployment", "rds-update-certificate", "rds-deployment-ha-broker", "rds-deployment-ha-gateway", "rds-deployment-uber", "rds-deployment-existing-ad", "rds-update-rdsh-collection","rds-deployment-vm-scale-sets")]
$installOptions = @("rds-deployment", "rds-update-certificate", "rds-deployment-ha-broker", "rds-deployment-ha-gateway","rds-deployment-vm-scale-sets"),
[string][ValidateSet('2012-R2-Datacenter', '2016-Datacenter')]$imageSku = "2016-Datacenter",
[string]$location = "",
[int]$logoffTimeInminutes = 60,
[switch]$monitor,
[int]$numberOfRdshInstances = 2,
[int]$numberOfWebGwInstances = 1,
[string]$deployFileAdDeployment = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/bb24e0c10dd73b818dc492133522ceaf72887cd5/active-directory-new-domain/azuredeploy.json",
[string]$parameterFileAdDeployment = "$($env:TEMP)\ad-deployment-only-test.azuredeploy.parameters.json",
[string]$parameterFileRdsDeployment = "$($env:TEMP)\rds-deployment.azuredeploy.parameters.json",
[string]$parameterFileRdsDeploymentExistingAd = "$($env:TEMP)\rds-deployment-existing-ad.azuredeploy.parameters.json",
[string]$parameterFileRdsUpdateCertificate = "$($env:TEMP)\rds-update-certificate.azuredeploy.parameters.json",
[string]$parameterFileRdsHaBroker = "$($env:TEMP)\rds-deployment-ha-broker.azuredeploy.parameters.json",
[string]$parameterFileRdsHaGateway = "$($env:TEMP)\rds-deployment-ha-gateway.azuredeploy.parameters.json",
[string]$parameterFileRdsUber = "$($env:TEMP)\rds-deployment-uber.azuredeploy.parameters.json",
[string]$parameterFileRdsUpdateRdshCollection = "$($env:TEMP)\rds-update-rdsh-collection.azuredeploy.parameters.json",
[string]$parameterFileRdsDeploymentVmScaleSets = "$($env:TEMP)\rds-deployment-vm-scale-sets.azuredeploy.parameters.json",
[string]$vmssTemplateBaseRepoUri = "https://raw.githubusercontent.com/Azure/vm-scale-sets/master/hack2017/",
[string]$deployFileRdsDeploymentVmScaleSets = "$($vmssTemplateBaseRepoUri)azuredeploy-rds-autoscale.json",
[switch]$pause,
[string]$pfxFilePath,
[switch]$postConnect,
[string]$gatewayPublicIp = "gwpip",
[string]$primaryDbConnectionString = "",
[string]$rdshAvailabilitySet = "rdsh-availabilityset",
[string]$rdshCollectionName = "Desktop Collection",
[string]$rdshVmSize = "Standard_A2",
[string]$rdshTemplateImageUri = "",
[string]$rdshUpdateIteration = "1",
[string]$scaleSetName = "$($resourcegroup)scaleset",
[string]$sqlServer = "",
[string]$subnetName = "subnet",
[string]$templateBaseRepoUri = "https://raw.githubusercontent.com/Azure/RDS-Templates/master/",
[string]$templateVmNamePrefix = "templateVm",
[string]$tenantId,
[switch]$useExistingJson,
[string]$vaultName = "$($resourceGroup)Cert",
[string]$vnetName = "vnet",
[switch]$whatIf
)
$maxRdshTestInstances = 10
$templateFileName = "azuredeploy.parameters.json"
# ----------------------------------------------------------------------------------------------------------------
function main()
{
$error.Clear()
write-host "$(get-date) starting" -foregroundcolor cyan
$timer = (get-date)
get-variable | out-string
if(!(runas-admin))
{
exit 1
}
write-host "using random: $($random)" -foregroundcolor yellow
write-host "using password: $($adminPassword)" -foregroundcolor yellow
write-host "using resource group: $($resourceGroup)" -foregroundcolor yellow
write-host "authenticating to azure"
authenticate-azureRm
write-host "checking tenant id"
if (!$tenantId)
{
$tenantId = (Get-AzureRmSubscription -warningAction SilentlyContinue).TenantId
}
write-host "checking parameters"
check-parameters
check-resourceGroup
foreach ($installOption in $installOptions)
{
switch ($installOption.ToString().ToLower())
{
"ad-domain-only-test" { start-ad-domain-only-test }
"rds-deployment" { start-rds-deployment }
"rds-update-certificate" { start-rds-update-certificate }
"rds-deployment-ha-broker" { start-rds-deployment-ha-broker }
"rds-deployment-ha-gateway" { start-rds-deployment-ha-gateway }
"rds-deployment-uber" { start-rds-deployment-uber }
"rds-deployment-existing-ad" { start-rds-deployment-existing-ad }
"rds-update-rdsh-collection" { start-rds-update-rdsh-collection }
"rds-deployment-vm-scale-sets" { start-rds-deployment-vm-scale-sets }
default { Write-Error "unknown option $($installOption)" }
} # end switch
if ($pause)
{
pause
}
} # end foreach
$rdWebSite = "https://$($dnsLabelPrefix).$($location).cloudapp.azure.com/RDWeb"
if ($postConnect)
{
run-postConnect
}
write-host "errors: $($error | out-string)"
write-host "-----------------------------------"
write-host "resource group: $($resourceGroup)" -foregroundcolor yellow
write-host "domain name: $($domainName)" -foregroundcolor yellow
write-host "admin user name: $($adminUsername)" -foregroundcolor yellow
write-host "admin password: $($adminPassword)" -foregroundcolor yellow
write-host "rdweb site: $($rdWebSite)"
write-host "$(get-date) finished. total time: $((get-date) -$timer)" -foregroundcolor cyan
}
# ----------------------------------------------------------------------------------------------------------------
function authenticate-azureRm()
{
# make sure at least wmf 5.0 installed
if ($PSVersionTable.PSVersion -lt [version]"5.0.0.0")
{
write-host "update version of powershell to at least wmf 5.0. exiting..." -ForegroundColor Yellow
start-process "https://www.bing.com/search?q=download+windows+management+framework+5.0"
exit
}
# verify NuGet package
$nuget = get-packageprovider nuget -Force
if (-not $nuget -or ($nuget.Version -lt [version]::New("2.8.5.22")))
{
write-host "installing nuget package..."
install-packageprovider -name NuGet -minimumversion ([version]::New("2.8.5.201")) -force
}
$allModules = (get-module azure* -ListAvailable).Name
# install AzureRM module
if ($allModules -inotcontains "AzureRM")
{
# each has different azurerm module requirements
# installing azurerm slowest but complete method
# if wanting to do minimum install, run the following script against script being deployed
# https://raw.githubusercontent.com/jagilber/powershellScripts/master/script-azurerm-module-enumerator.ps1
# this will parse scripts in given directory and output which azure modules are needed to populate the below
# at least need profile, resources, insights, logicapp for this script
if ($allModules -inotcontains "AzureRM.profile")
{
write-host "installing AzureRm.profile powershell module..."
install-module AzureRM.profile -force
}
if ($allModules -inotcontains "AzureRM.resources")
{
write-host "installing AzureRm.resources powershell module..."
install-module AzureRM.resources -force
}
if ($allModules -inotcontains "AzureRM.compute")
{
write-host "installing AzureRm.compute powershell module..."
install-module AzureRM.compute -force
}
if ($allModules -inotcontains "AzureRM.network")
{
write-host "installing AzureRm.network powershell module..."
install-module AzureRM.network -force
}
if ($allModules -inotcontains "AzureRM.storage")
{
write-host "installing AzureRm.storage powershell module..."
install-module AzureRM.storage -force
}
if ($allModules -inotcontains "AzureRM.sql")
{
write-host "installing AzureRm.sql powershell module..."
install-module AzureRM.sql -force
}
if ($allModules -inotcontains "AzureRM.logicapp")
{
write-host "installing AzureRm.logicapp powershell module..."
install-module AzureRM.logicapp -force
}
if ($allModules -inotcontains "AzureRM.insights")
{
write-host "installing AzureRm.insights powershell module..."
install-module AzureRM.insights -force
}
Import-Module azurerm.profile
Import-Module azurerm.resources
Import-Module azurerm.compute
Import-Module azurerm.network
Import-Module azurerm.storage
Import-Module azurerm.sql
Import-Module AzureRM.LogicApp
Import-Module AzureRM.Insights
}
else
{
Import-Module azurerm
}
# authenticate
try
{
$rg = @(Get-AzureRmTenant)
if ($rg)
{
write-host "auth passed $($rg.Count)"
}
else
{
write-host "auth error $($error)" -ForegroundColor Yellow
throw [Exception]
}
}
catch
{
if(!(connect-azurermaccount))
{
log-info "exception authenticating. exiting $($error | out-string)" -ForegroundColor Yellow
exit 1
}
}
}
# ----------------------------------------------------------------------------------------------------------------
function check-deployment($deployment)
{
write-host "checking for existing deployment $($deployment)"
if ((Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup -Name $deployment -ErrorAction SilentlyContinue))
{
if ((read-host "resource group deployment exists! Do you want to delete?[y|n]") -ilike 'y')
{
write-host "Remove-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup -Name $deployment -Confirm"
if (!$whatIf)
{
Remove-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup -Name $deployment -Confirm
}
}
}
}
# ----------------------------------------------------------------------------------------------------------------
function check-forExistingAdDeployment()
{
if (!($installOptions -imatch "ad-domain-only-test"))
{
if ((read-host "this deployment requires an 'existing AD'. do you want to deploy 'ad-domain-only-test' first?[y|n]") -imatch "y")
{
start-ad-domain-only-test
}
}
}
# ----------------------------------------------------------------------------------------------------------------
function check-parameterFile($parameterFile, $deployment, $updateUrl = "")
{
write-host "checking parameter file $($parameterFile) for $($deployment)" -foregroundcolor Green
$ret = $false
if ([IO.File]::Exists($parameterFile))
{
if (!$useExistingJson)
{
write-host "removing previous parameter file $($parameterFile)"
write-host (get-content -Raw -Path $parameterFile) | out-string
[IO.File]::Delete($parameterFile)
}
else
{
$ret = $true
}
}
if (!$ret)
{
# check repo
if(!$updateUrl)
{
$updateUrl = "$($templateBaseRepoUri)/$($deployment)/$($templateFileName)"
}
write-host "downloading template from repo $($updateUrl)"
if (get-urlJsonFile -updateUrl $updateUrl -destinationFile $parameterFile)
{
$ret = $true
}
}
if ($ret)
{
write-host "parameter file:"
write-host (get-content -Raw -Path $parameterFile) | out-string
return $true
}
else
{
write-error "missing parameter file $($parameterFile)"
write-host "exiting"
exit 1
}
return $ret
}
# ----------------------------------------------------------------------------------------------------------------
function check-parameters()
{
try
{
write-host "checking number of instances"
if ($numberofRdshInstances -lt 1 -or $numberofRdshInstances -gt $maxRdshTestInstances)
{
write-host "numberOfRdshInstances should be greater than 1 and less than 100. exiting"
exit 1
}
if (!$deploymentName)
{
$deploymentName = $resourceGroup
}
write-host "checking resource group"
if (!$resourceGroup)
{
write-warning "resourcegroup is a mandatory argument. supply -resourceGroup argument and restart script."
exit 1
}
if ($resourceGroup -ne $resourceGroup.ToLower())
{
write-warning "resourcegroup currently needs to be lower case due to issue 43. exiting script."
exit 1
}
if ($resourceGroup.Contains("-"))
{
write-warning "resourcegroup currently needs not have '-' due to issue 43. exiting script."
exit 1
}
write-host "checking ad domain name"
if (!$domainName)
{
$domainName = "$($resourceGroup.ToLower()).lab"
write-host "domain name '$($domainName)' should populated. example: contoso.com"
write-host "exiting"
exit 1
}
if (!$domainName.Contains("."))
{
write-host "domain name '$($domainName)' should be fqdn. example: contoso.com"
write-host "exiting"
exit 1
}
if ($domainName.IndexOf(".") -gt 15)
{
write-host "error: base domain name greater than 15 characters $($domainName). use -domainName with a new shortend name and restart script." -ForegroundColor Yellow
write-host "exiting"
exit 1
}
write-host "checking dns label"
if (!$dnsLabelPrefix)
{
write-host "dns label prefix '$($dnsLabelPrefix)' should be populated. example: 'rdsgateway' or '$($resourceGroup)'"
write-host "exiting"
exit 1
}
if ($dnsLabelPrefix -ne $dnsLabelPrefix.ToLower())
{
write-host "dns label prefix '$($dnsLabelPrefix)' should be lower case. example: 'rdsgateway' or '$($resourceGroup.ToLower())'"
write-host "exiting"
exit 1
}
if ($dnsLabelPrefix.Contains("."))
{
write-host "dns label prefix '$($dnsLabelPrefix)' should not contain '.'"
write-host "exiting"
exit 1
}
$cloudAppDns = "$($dnsLabelPrefix).$($location).cloudapp.azure.com"
if (Resolve-DnsName -Name $cloudAppDns -ErrorAction SilentlyContinue)
{
Write-Warning "dns name already exists! '$($cloudAppDns)'. if not recreating deployment, you may need to select new unique name."
}
write-host "checking location"
if (!(Get-AzureRmLocation | Where-Object Location -Like $location) -or !$location)
{
(Get-AzureRmLocation).Location
write-warning "location: $($location) not found. supply -location using one of the above locations and restart script."
exit 1
}
write-host "checking vm size"
if (!(Get-AzureRmVMSize -Location $location | Where-Object Name -Like $rdshVmSize))
{
Get-AzureRmVMSize -Location $location
write-warning "rdshVmSize: $($rdshVmSize) not found in $($location). correct -rdshVmSize using one of the above options and restart script."
exit 1
}
write-host "checking sku"
if (!(Get-AzureRmVMImageSku -Location $location -PublisherName MicrosoftWindowsServer -Offer WindowsServer | Where-Object Skus -Like $imageSKU))
{
Get-AzureRmVMImageSku -Location $location -PublisherName MicrosoftWindowsServer -Offer WindowsServer
write-warning "image sku: $($imageSku) not found in $($location). correct -imageSKU using one of the above options and restart script."
exit 1
}
write-host "checking password"
if (!$credentials)
{
if (!$adminPassword)
{
$global:credential = Get-Credential
}
else
{
$SecurePassword = $adminPassword | ConvertTo-SecureString -AsPlainText -Force
$global:credential = new-object Management.Automation.PSCredential -ArgumentList $adminUsername, $SecurePassword
}
}
else
{
$global:credential = $credentials
}
$adminUsername = $global:credential.UserName
$adminPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($global:credential.Password))
$count = 0
# uppercase check
if ($adminPassword -match "[A-Z]") { $count++ }
# lowercase check
if ($adminPassword -match "[a-z]") { $count++ }
# numeric check
if ($adminPassword -match "\d") { $count++ }
# specialKey check
if ($adminPassword -match "\W") { $count++ }
if ($adminPassword.Length -lt 8 -or $adminPassword.Length -gt 123 -or $count -lt 3)
{
Write-warning @"
azure password requirements at time of writing (3/2017):
The supplied password must be between 8-123 characters long and must satisfy at least 3 of password complexity requirements from the following:
1) Contains an uppercase character
2) Contains a lowercase character
3) Contains a numeric digit
4) Contains a special character.
correct password and restart script.
"@
exit 1
}
if ($monitor)
{
run-monitor
}
}
catch
{
Write-Warning "exception checking parameters. continuing WITHOUT parameter validation!"
write-error "$($error | out-string)"
$error.clear()
}
}
# ----------------------------------------------------------------------------------------------------------------
function check-resourceGroup()
{
write-host "checking for existing resource group $($resourceGroup)"
if ((Get-AzureRmResourceGroup -Name $resourceGroup -ErrorAction SilentlyContinue))
{
write-host "resource group exists! this is normally ok unless intent is to delete resource group which WILL DELETE all items in resource group. " -ForegroundColor Yellow
if ((read-host "Do you want to delete resource group?[y|n]") -ilike 'y')
{
write-host "Remove-AzureRmResourceGroup -Name $resourceGroup"
if (!$whatIf)
{
Remove-AzureRmResourceGroup -Name $resourceGroup
}
}
}
# create resource group if it does not exist
if (!(Get-AzureRmResourceGroup -Name $resourceGroup -ErrorAction SilentlyContinue))
{
Write-Host "creating resource group $($resourceGroup) in location $($location)"
write-host "New-AzureRmResourceGroup -Name $resourceGroup -Location $location"
if (!$whatIf)
{
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
}
}
}
# ----------------------------------------------------------------------------------------------------------------
function create-cert
{
write-host "$(get-date) create-cert..." -foregroundcolor cyan
if (!$pfxFilePath)
{
write-warning "this is a standalone cert that should only be used for test and NOT production"
write-host "Get-ChildItem -Path cert:\LocalMachine\My -Recurse | where-object Subject -Match $domainName | Remove-Item -Force"
write-host ".\ps-certreq.ps1 -subject `"*.$($domainName)`" -outputDir $($env:TEMP)"
$pfxFilePath = "$($env:TEMP)\$($domainName).pfx"
write-host "Get-ChildItem -Path cert:\LocalMachine\My -Recurse | where-object Subject -Match $domainName | Export-PfxCertificate -Password $mypwd -FilePath $pfxFilePath -Force"
write-host "Get-ChildItem -Path cert:\LocalMachine\My -Recurse | where-object Subject -Match $domainName | Remove-Item -Force"
$ret = $null
if (!$whatIf)
{
Get-ChildItem -Path cert:\LocalMachine\My -Recurse | where-object Subject -Match $domainName | Remove-Item -Force
$ret = .\ps-certreq.ps1 -subject "*.$($domainName)" -outputDir $env:TEMP
write-host $ret
$mypwd = ConvertTo-SecureString -String $adminPassword -Force -AsPlainText
Get-ChildItem -Path cert:\LocalMachine\My -Recurse | where-object Subject -Match $domainName | Export-PfxCertificate -Password $mypwd -FilePath $pfxFilePath -Force
# use post import to trusted root
Get-ChildItem -Path cert:\LocalMachine\My -Recurse | where-object Subject -Match $domainName | Remove-Item -Force
}
}
if (!$applicationId)
{
write-host "$(get-date) create-vault..." -foregroundcolor cyan
write-host ".\azure-rm-aad-add-key-vault.ps1 -pfxFilePath $pfxFilePath `
-certPassword $applicationPassword `
-certNameInVault $certificateName `
-vaultName $vaultName `
-uri 'https://$($resourceGroup)/$($domainName)' `
-resourceGroup $resourceGroup `
-adApplicationName 'rdscert$($resourceGroup)'"
if (!$whatIf)
{
$ret = .\azure-rm-aad-add-key-vault.ps1 -pfxFilePath $pfxFilePath `
-certPassword $applicationPassword `
-certNameInVault $certificateName `
-vaultName $vaultName `
-uri "https://$($resourceGroup)/$($domainName)" `
-resourceGroup $resourceGroup `
-adApplicationName "rdscert$($resourceGroup)"
return $ret
}
}
else
{
return "application id: $($applicationId) "
}
}
# ----------------------------------------------------------------------------------------------------------------
function create-sql
{
write-host "$(get-date) sql..." -foregroundcolor cyan
write-host ".\azure-rm-sql-create.ps1 -resourceGroupName $resourceGroup `
-location $location `
-databaseName RdsCb `
-adminPassword $adminPassword `
-generateUniqueName `
-nolog "
#-nsgStartIpAllow '10.0.0.4' `
#-nsgEndIpAllow '10.0.0.254' "
if (!$whatIf)
{
$ret = .\azure-rm-sql-create.ps1 -resourceGroupName $resourceGroup `
-location $location `
-databaseName RdsCb `
-adminPassword $adminPassword `
-servername "sql-server$($random)" `
-nolog #`
#-nsgStartIpAllow "10.0.0.4" `
#-nsgEndIpAllow "10.0.0.254"
$match = [regex]::Match($ret, "connection string ODBC Native client:`r`n(DRIVER.+;)", [Text.RegularExpressions.RegexOptions]::Singleline -bor [Text.RegularExpressions.RegexOptions]::IgnoreCase)
$odbcstring = ($match.Captures[0].Groups[1].Value).Replace("`r`n", "")
return $odbcstring
}
}
# ----------------------------------------------------------------------------------------------------------------
function create-templateVm($parameterFile)
{
$ujson = ConvertFrom-Json (get-content -Raw -Path $parameterFile)
if ((read-host "Do you want to create a new template vm from gallery into $($resourceGroup)?[y|n]") -imatch 'y')
{
$count = 0
while($vm = Get-AzureRmVM -ResourceGroupName $resourceGroup -Name "$($templateVmNamePrefix)-$($count.ToString("D2"))" -ErrorAction SilentlyContinue)
{
$count++
}
write-host "adding template vm. this will take a while..." -ForegroundColor Green
write-host ".\azure-rm-vm-create.ps1 -publicIp `
-resourceGroupName $resourceGroup `
-location $location `
-adminUsername $adminUsername `
-adminPassword $adminpassword `
-vmBaseName $templateVmNamePrefix `
-vmStartCount $count `
-vmCount 1 "
if (!$whatIf)
{
$ret = .\azure-rm-vm-create.ps1 -publicIp `
-resourceGroupName $resourceGroup `
-location $location `
-adminUsername $adminUsername `
-adminPassword $adminpassword `
-vmBaseName $templateVmNamePrefix `
-vmStartCount $count `
-vmCount 1
}
write-host "return: $($ret)"
write-host "getting vhd location"
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroup -Name "$($templateVmNamePrefix)-$($count.ToString("D2"))"
write-host "$($vm | out-string)"
$vhdUri = $vm.StorageProfile.OsDisk.Vhd.Uri
$tpIp = (Get-AzureRmPublicIpAddress -Name ([IO.Path]::GetFileName($vm.NetworkProfile.NetworkInterfaces[0].Id)) -ResourceGroupName $resourceGroup).IpAddress
if ([string]::IsNullOrEmpty($vhdUri) -or [string]::IsNullOrEmpty($tpIp))
{
write-host "unable to find template public ip. exiting"
exit 1
}
write-host "mstsc /v $tpIp /admin" -ForegroundColor Magenta
write-host "use mstsc connection to run sysprep on template c:\windows\system32\sysprep\sysprep.exe -oobe -generalize" -foregroundcolor Green
mstsc /v $tpIp /admin
write-host "waiting for machine to shutdown"
while ($true)
{
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroup -Name "$($templateVmNamePrefix)-01" -Status
if ($vm.Statuses.Code.Contains("PowerState/stopped"))
{
write-host "deallocating vm"
stop-azurermvm -name $vm.Name -Force -ResourceGroupName $resourceGroup
write-host "setting vm to OSState/generalized"
set-azurermvm -ResourceGroupName $resourceGroup -Name $vm.Name -Generalized
break
}
elseif ($vm.Statuses.Code.Contains("PowerState/deallocated"))
{
break
}
start-sleep -Seconds 1
}
}
else
{
$vhdUri = $rdshTemplateImageUri
if (!$vhdUri)
{
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroup -Name "$($templateVmNamePrefix)-01"
$vhdUri = $vm.StorageProfile.OsDisk.Vhd.Uri
}
if ($vhdUri)
{
write-host $vhdUri -foregroundcolor Magenta
if ((read-host "Is this the correct path to vhd of template image to be used?[y|n]") -imatch 'n')
{
$ujson.parameters.rdshTemplateImageUri.value = read-host "Enter new vhd path:"
}
}
}
if ($vhdUri)
{
write-host "returning rdshTemplateImageUri: $($vhdUri)"
return $vhdUri
}
else
{
write-host "error:invalid vhd path. exiting"
exit 1
}
}
# ----------------------------------------------------------------------------------------------------------------
function deploy-template($templateFile, $parameterFile, $deployment)
{
write-host "validating template"
write-host "Test-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup `
-TemplateFile $templateFile `
-Mode Complete `
-TemplateParameterFile $parameterFile "
$ret = $null
$ret = Test-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup `
-TemplateFile $templateFile `
-Mode Complete `
-TemplateParameterFile $parameterFile
if ($ret)
{
Write-Error "template validation failed. error: `n`n$($ret.Code)`n`n$($ret.Message)`n`n$($ret.Details)"
Write-Error "error: $($error | out-string)"
write-host "exiting"
exit 1
}
$error.Clear()