forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure-rm-download-deployment-templates.ps1
272 lines (234 loc) · 9.07 KB
/
azure-rm-download-deployment-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
<#
script to export all deployment templates, parameters, and operations from azure subscription or list of resourcegroups
https://docs.microsoft.com/en-us/rest/api/resources/resourcegroups/exporttemplate
#>
param(
[string]$outputDir = (get-location).path,
[string[]]$resourceGroups,
[string]$exportTemplateApiVersion = "2018-02-01",
[switch]$useGit,
[switch]$currentOnly,
[string]$clientId = $global:clientId,
[string]$clientSecret = $global:clientSecret,
[switch]$vscode
)
$error.Clear()
$outputDir = $outputDir + "\armDeploymentTemplates"
$ErrorActionPreference = "silentlycontinue"
$getCurrentConfig = $clientId -and $clientSecret
$currentdir = (get-location).path
$error.Clear()
$repo = "https://raw.githubusercontent.com/jagilber/powershellScripts/master/"
$restLogonScript = "$($currentDir)\azure-rm-rest-logon.ps1"
$restQueryScript = "$($currentDir)\azure-rm-rest-query.ps1"
$global:token = $Null
function main()
{
if($currentOnly -and (!$clientId -or !$clientSecret))
{
write-warning "if specifying -currentOnly, -clientId and -clientSecret must be used as well. exiting..."
return
}
if(!$getCurrentConfig -or !$resourceGroups -or !$currentOnly)
{
check-authentication
}
New-Item -ItemType Directory $outputDir -ErrorAction SilentlyContinue
Get-AzureRmDeployment | Save-AzureRmDeploymentTemplate -Path $outputDir -Force
set-location $outputDir
write-host "using output location: $($outputDir)" -ForegroundColor Yellow
if ($useGit)
{
$error.clear()
(git)
if($error)
{
$error.Clear()
write-host "git not installed"
$useGit = $false
$error.Clear()
}
}
if ($useGit -and !(git status))
{
git init
}
if ($resourceGroups.Count -lt 1)
{
$resourceGroups = @((Get-AzureRmResourceGroup).ResourceGroupName)
}
if (!$currentOnly)
{
$deployments = (Get-AzureRmResourceGroup) | Where-Object ResourceGroupName -imatch ($resourceGroups -join "|") | Get-AzureRmResourceGroupDeployment
foreach ($dep in ($deployments | sort-object -Property Timestamp))
{
$rg = $dep.ResourceGroupName
$rgDir = "$($outputDir)\$($rg)"
New-Item -ItemType Directory $rgDir -ErrorAction SilentlyContinue
$baseFile = "$($rgDir)\$($dep.deploymentname)"
Save-AzureRmResourceGroupDeploymentTemplate -Path $baseFile -ResourceGroupName $rg -DeploymentName ($dep.DeploymentName) -Force
out-file -Encoding ascii -InputObject ((convertto-json ($dep.Parameters) -Depth 99).Replace(" ", " ")) -FilePath "$($baseFile).parameters.json" -Force
$operations = Get-AzureRmResourceGroupDeploymentOperation -DeploymentName $($dep.DeploymentName) -ResourceGroupName $rg
out-file -Encoding ascii -InputObject ((convertto-json $operations -Depth 99).Replace(" ", " ")) -FilePath "$($baseFile).operations.json" -Force
if ($useGit)
{
git add -A
git commit -a -m "$($rg) $($dep.deploymentname) $($dep.TimeStamp) $($dep.ProvisioningState)`n$($dep.outputs | fl * | out-string)" --date (($dep.TimeStamp).ToString("o"))
}
}
}
if ($getCurrentConfig)
{
if (!(test-path $restLogonScript))
{
get-update -destinationFile $restLogonScript -updateUrl "$($repo)$([io.path]::GetFileName($restLogonScript))"
}
if (!(test-path $restQueryScript))
{
get-update -destinationFile $restQueryScript -updateUrl "$($repo)$([io.path]::GetFileName($restQueryScript))"
}
$global:token = Invoke-Expression "$($restLogonScript) -clientSecret $($clientSecret) -clientId $($clientId)"
$global:token
}
if ($useGit -and !(git status))
{
git init
}
if ($resourceGroups.Count -lt 1)
{
$resourceGroups = @((Get-AzureRmResourceGroup).ResourceGroupName)
}
if ($getCurrentConfig)
{
foreach ($rg in $resourceGroups)
{
$rgDir = "$($outputDir)\$($rg)"
New-Item -ItemType Directory $rgDir -ErrorAction SilentlyContinue
$currentConfig = get-currentConfig -rg $rg
#out-file -InputObject (convertto-json (get-currentConfig -rg $rg)) -FilePath "$($rgDir)\current.json" -Force
out-file -Encoding ascii -InputObject ([Text.RegularExpressions.Regex]::Unescape((convertto-json ($currentConfig.template) -Depth 99))) -FilePath "$($rgDir)\current.json" -Force
if ($currentConfig.error)
{
out-file -Encoding ascii -InputObject ([Text.RegularExpressions.Regex]::Unescape((convertto-json ($currentConfig.error) -Depth 99))) -FilePath "$($rgDir)\current.errors.json" -Force
}
if ($useGit)
{
git add -A
git commit -a -m "$($rg) $((get-date).ToString("o"))"
}
}
}
else
{
write-host
write-warning ("`nthis information does *not* include the currently running configuration, only previous uniquely named deployments.`n" `
+ "some examples are any changes made in portal after deployment or any deployment using same name (only last will be available)")
write-host
write-host "to get the current running configuration ('automation script' in portal), use portal, or rerun script with clientid and clientsecret"
write-host "these are values used when connecting to azure using a script either with powershel azure modules or rest methods"
write-host "output will contain clientid and clientsecret (thumbprint)."
write-host "see link for additional information https://blogs.msdn.microsoft.com/igorpag/2017/06/28/using-powershell-as-an-azure-arm-rest-api-client/" -ForegroundColor Cyan
write-host
write-host "use this script to generate azure ad spn app with a self signed cert for use with scripts (not just this one)."
write-host "(new-object net.webclient).downloadfile(`"https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-rm-create-aad-application-spn.ps1`",`"$($currentDir)\azure-rm-create-aad-application-spn.ps1`");" -ForegroundColor Cyan
write-host "$($currentDir)\azure-rm-create-aad-application-spn.ps1 -aadDisplayName powerShellRestSpn -logontype certthumb" -ForegroundColor Cyan
}
}
function get-update($updateUrl, $destinationFile)
{
write-host "downloading helper script: $($updateUrl)" -ForegroundColor Green
$file = ""
$git = $null
try
{
$git = Invoke-RestMethod -UseBasicParsing -Method Get -Uri $updateUrl
# git may not have carriage return
# reset by setting all to just lf
$git = [regex]::Replace($git, "`r`n", "`n")
# add cr back
$git = [regex]::Replace($git, "`n", "`r`n")
if ([IO.File]::Exists($destinationFile))
{
$file = [IO.File]::ReadAllText($destinationFile)
}
if (([string]::Compare($git, $file) -ne 0))
{
write-host "copying script $($destinationFile)"
[IO.File]::WriteAllText($destinationFile, $git)
return $true
}
else
{
write-host "script is up to date"
}
return $false
}
catch [System.Exception]
{
write-host "get-update:exception: $($error)"
$error.Clear()
return $false
}
}
function get-currentConfig($rg)
{
$url = "https://management.azure.com/subscriptions/$((get-azurermcontext).subscription.id)/resourcegroups/$($rg)/exportTemplate?api-version=$($exportTemplateApiVersion)"
write-host $url
$body = "@{'options'='IncludeParameterDefaultValue, IncludeComments';'resources' = @('*')}"
$command = "$($restQueryScript) -clientId $($clientid) -contentType `"application/json`" -query `"resourcegroups/$($rg)/exportTemplate`" -apiVersion `"$($exportTemplateApiVersion)`" -method post -body " + $body
write-host $command
$results = invoke-expression $command
if ($results.error)
{
write-warning (convertto-json ($results.error) -depth 99)
}
return $results
}
function check-authentication()
{
# authenticate
try
{
$tenants = @(Get-AzureRmTenant)
if ($tenants)
{
write-host "auth passed $($tenants.Count)"
}
else
{
write-host "auth error $($error)" -ForegroundColor Yellow
exit 1
}
}
catch
{
try
{
connect-azurermaccount
}
catch
{
write-host "exception authenticating. exiting $($error)" -ForegroundColor Yellow
exit 1
}
}
}
try
{
main
}
catch
{
write-host "main exception $($error | out-string)"
}
finally
{
if($vscode)
{
code $outputDir
}
set-location $currentdir | Out-Null
write-host
write-host "output location: $($outputDir)" -ForegroundColor Yellow
write-host "finished"
}