forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure-rm-deploy-template.ps1
376 lines (319 loc) · 11.6 KB
/
azure-rm-deploy-template.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
<#
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.
.SYNOPSIS
powershell script to test azure deployments
.DESCRIPTION
powershell script to test azure deployments
to enable script execution, you may need to Set-ExecutionPolicy Bypass -Force
.NOTES
file name : azure-rm-deploy-template.ps1
version : 171013 original
.EXAMPLE
.\azure-rm-deploy-template.ps1 -adminPassword changeme3240e2938r92 -resourceGroup rdsdeptest
Example command to deploy rds-deployment with 2 instances using A1 machines. the resource group is rdsdeptest and domain fqdn is rdsdeptest.lab
.PARAMETER adminUsername
if specified, the name of the administrator account.
.PARAMETER adminPassword
if specified, the administrator account password in clear text. password needs to meet azure password requirements.
use -credentials to pass credentials securely
.PARAMETER credentials
can be used for administrator account password. password needs to meet azure password requirements.
.PARAMETER resourceGroup
resourceGroup is a mandatory paramenter and is the azure arm resourcegroup to use / create for this deployment
.PARAMETER test
If specified, will test script and parameters but will not start deployment
.PARAMETER templateFile
required. path to template file on disk
.PARAMETER templateParameterFile
required. path to template parameter file on disk
#>
[cmdletbinding()]
param(
[string]$adminUsername = "cloudadmin",
[string]$adminPassword = "",
[pscredential]$credentials,
[string]$deploymentName = $resourceGroup + (get-date).ToString("yyMMddHHmmss"),
[string]$location,
[switch]$monitor,
[switch]$postConnect,
[Parameter(Mandatory = $true)]
[string]$resourceGroup,
[switch]$test,
[Parameter(Mandatory = $true)]
[string]$templateFile,
[Parameter(Mandatory = $true)]
[string]$templateParameterFile,
[hashtable]$additionalParameters = @{},
[switch]$clean,
[switch]$force
)
# shouldnt need modification
$error.Clear()
$ErrorActionPreference = "Continue"
function main()
{
if (!$deploymentName)
{
$deploymentName = $resourceGroup
}
if (!(test-path $templateFile))
{
write-host "unable to find json file $($templateFile)"
return
}
if (!(test-path $templateParameterFile))
{
write-host "unable to find json file $($templateParameterFile)"
return
}
write-host "running quickstart:$($quickStartTemplate) for group $($resourceGroup)"
write-host "authenticating to azure"
try
{
get-command connect-azurermaccount | Out-Null
}
catch [management.automation.commandNotFoundException]
{
if ((read-host "azurerm not installed but is required for this script. is it ok to install?[y|n]") -imatch "y")
{
write-host "installing minimum required azurerm modules..."
install-module azurerm.profile
install-module azurerm.resources
import-module azurerm.profile
import-module azurerm.resources
}
else
{
return 1
}
}
if (!(Get-AzureRmResourceGroup))
{
connect-azurermaccount
if (!(Get-AzureRmResourceGroup))
{
Write-Warning "unable to authenticate to azurerm. returning..."
return 1
}
}
write-host "checking resource group"
if (!$resourceGroup)
{
write-warning "resourcegroup is a mandatory argument. supply -resourceGroup argument and restart script."
return 1
}
write-host "checking location"
if (!(Get-AzureRmLocation | Where-Object Location -Like $location) -or [string]::IsNullOrEmpty($location))
{
(Get-AzureRmLocation).Location
write-warning "location: $($location) not found. supply -location using one of the above locations and restart script."
return 1
}
$VerbosePreference = "continue"
$DebugPreference = "continue"
# checking json format
$error.clear()
write-host "reading template file $($templateFile)"
$ujson = ConvertFrom-Json (get-content -Raw -Path $templateFile)
$ujson | ConvertTo-Json
if($error)
{
write-error "error json format: $templateFile"
return 1
}
write-host "reading parameter file $($templateParameterFile)"
$ujson = ConvertFrom-Json (get-content -Raw -Path $templateParameterFile)
$ujson | ConvertTo-Json
if($error)
{
write-error "error json format: $templateParameterFile"
return 1
}
if ($ujson.parameters.adminUserName -and $ujson.parameters.adminPassword -and !$test)
{
write-host "checking password"
if (!$credentials)
{
if(!$adminPassword)
{
$adminPassword = $ujson.parameters.adminPassword.value
}
if(!$adminUsername)
{
$adminUsername = $ujson.parameters.adminUserName.value
}
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 complreturny 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. "
return 1
}
}
write-host "checking for existing deployment"
if ((Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup -Name $deploymentName -ErrorAction SilentlyContinue))
{
if($clean -and $force)
{
write-warning "resource group deployment exists! deleting as -clean and -force are specified!"
Remove-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup -Name $deploymentName
}
elseif($clean)
{
if ((read-host "resource group deployment exists! Do you want to delete?[y|n]") -ilike 'y')
{
Remove-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup -Name $deploymentName
}
}
else
{
write-warning "resource group deployment exists!"
}
}
write-host "checking for existing resource group"
if ((Get-AzureRmResourceGroup -Name $resourceGroup -ErrorAction SilentlyContinue))
{
if($clean -and $force)
{
write-warning "resource group exists! deleting as -clean and -force are specified!"
Remove-AzureRmResourceGroup -ResourceGroupName $resourceGroup -Force
}
elseif($clean)
{
if ((read-host "resource group exists! Do you want to delete?[y|n]") -ilike 'y')
{
Remove-AzureRmResourceGroup -ResourceGroupName $resourceGroup -Force
}
}
else
{
write-warning "resource group exists!"
}
}
# create resource group if it does not exist
if (!(Get-AzureRmResourceGroup -Name $resourceGroup -ErrorAction SilentlyContinue))
{
Write-Host "creating resource group $($resourceGroup) in location $($location)"
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
}
write-host "validating template"
$error.Clear()
$ret = $null
if ($global:credential.Password.Length)
{
$ret = Test-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup `
-TemplateFile $templateFile `
-Mode Complete `
-adminUserName $global:credential.UserName `
-adminPassword $global:credential.Password `
-TemplateParameterFile $templateParameterFile `
@additionalParameters
}
else
{
$ret = Test-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup `
-TemplateFile $templateFile `
-Mode Complete `
-TemplateParameterFile $templateParameterFile `
@additionalParameters
}
if ($ret)
{
Write-Error "template validation failed. error: `n`n$($ret.Code)`n`n$($ret.Message)`n`n$($ret.Details)"
return 1
}
if ($monitor)
{
write-host "$([DateTime]::Now) starting monitor"
$monitorScript = "$(get-location)\azure-rm-log-reader.ps1"
if (![IO.File]::Exists($monitorScript))
{
[IO.File]::WriteAllText($monitorScript,
(Invoke-WebRequest -UseBasicParsing -Uri "https://aka.ms/azure-rm-log-reader.ps1").ToString().Replace("???", ""))
}
Start-Process -FilePath "powershell.exe" -ArgumentList "-WindowStyle Minimized -ExecutionPolicy Bypass $($monitorScript)"
}
if (!$test)
{
write-host "$([DateTime]::Now) creating deployment"
$error.Clear()
if ($global:credential.Password.Length)
{
New-AzureRmResourceGroupDeployment -Name $deploymentName `
-ResourceGroupName $resourceGroup `
-DeploymentDebugLogLevel All `
-TemplateFile $templateFile `
-adminUserName $global:credential.UserName `
-adminPassword $global:credential.Password `
-TemplateParameterFile $templateParameterFile `
-Verbose `
@additionalParameters
}
else
{
New-AzureRmResourceGroupDeployment -Name $deploymentName `
-ResourceGroupName $resourceGroup `
-DeploymentDebugLogLevel All `
-TemplateFile $templateFile `
-TemplateParameterFile $templateParameterFile `
-Verbose `
@additionalParameters
}
}
}
main
$VerbosePreference = "silentlycontinue"
$DebugPreference = "silentlycontinue"
$error | out-string
write-host "$([DateTime]::Now) finished"