@@ -73,6 +73,7 @@ param wsusServer string
73
73
74
74
var parameters = {
75
75
arcGisProInstaller : arcGisProInstaller
76
+ computeGalleryImageResourceId : computeGalleryImageResourceId
76
77
computeGalleryResourceId : computeGalleryResourceId
77
78
containerName : containerName
78
79
customizations : string (customizations )
@@ -111,7 +112,7 @@ var parameters = {
111
112
officeInstaller : officeInstaller
112
113
replicaCount : string (replicaCount )
113
114
resourceGroupName : resourceGroupName
114
- computeGalleryImageResourceId : computeGalleryImageResourceId
115
+ resourceManagerUri : environment (). resourceManager
115
116
sourceImageType : sourceImageType
116
117
storageAccountResourceId : storageAccountResourceId
117
118
subnetResourceId : subnetResourceId
@@ -131,7 +132,6 @@ var parameters = {
131
132
}
132
133
var privateEndpointName = 'pe-${automationAccountName }'
133
134
var runbookName = 'New-AzureZeroTrustImageBuild'
134
- var storageEndpoint = environment ().suffixes .storage
135
135
var subscriptionId = subscription ().subscriptionId
136
136
var tenantId = subscription ().tenantId
137
137
@@ -205,7 +205,21 @@ resource privateDnsZoneGroup 'Microsoft.Network/privateEndpoints/privateDnsZoneG
205
205
}
206
206
}
207
207
208
- resource runCommand 'Microsoft.Compute/virtualMachines/runCommands@2023-07-01' = {
208
+ resource runBook 'Microsoft.Automation/automationAccounts/runbooks@2023-11-01' = {
209
+ parent : automationAccount
210
+ name : runbookName
211
+ properties : {
212
+ runbookType : 'PowerShell'
213
+ logProgress : true
214
+ logVerbose : true
215
+ }
216
+ tags : union (
217
+ contains (tags , 'Microsoft.Automation/automationAccounts/runbooks' ) ? tags ['Microsoft.Automation/automationAccounts/runbooks' ] : {},
218
+ mlzTags
219
+ )
220
+ }
221
+
222
+ resource updateRunBook 'Microsoft.Compute/virtualMachines/runCommands@2023-07-01' = {
209
223
name : 'runbook'
210
224
location : location
211
225
tags : union (
@@ -218,96 +232,57 @@ resource runCommand 'Microsoft.Compute/virtualMachines/runCommands@2023-07-01' =
218
232
asyncExecution : false
219
233
parameters : [
220
234
{
221
- name : 'AutomationAccountName'
222
- value : automationAccountName
223
- }
224
- {
225
- name : 'ContainerName'
226
- value : containerName
227
- }
228
- {
229
- name : 'Environment'
230
- value : environment ().name
231
- }
232
- {
233
- name : 'ResourceGroupName'
234
- value : resourceGroup ().name
235
+ name : 'RunBookResourceId'
236
+ value : runBook .id
235
237
}
236
238
{
237
- name : 'RunbookName '
238
- value : runbookName
239
+ name : 'ResourceManagerUri '
240
+ value : environment (). resourceManager
239
241
}
240
242
{
241
- name : 'StorageAccountName'
242
- value : split (storageAccountResourceId , '/' )[8 ]
243
- }
244
- {
245
- name : 'StorageEndpoint'
246
- value : storageEndpoint
247
- }
248
- {
249
- name : 'SubscriptionId'
250
- value : subscription ().subscriptionId
251
- }
252
- {
253
- name : 'TenantId'
254
- value : tenant ().tenantId
243
+ name : 'RunbBookScriptContent'
244
+ value : loadTextContent ('../scripts/New-AzureZeroTrustImageBuild.ps1' )
255
245
}
256
246
{
257
247
name : 'UserAssignedIdentityClientId'
258
248
value : userAssignedIdentityClientId
259
249
}
260
- {
261
- name : 'UserAssignedIdentityObjectId'
262
- value : userAssignedIdentityPrincipalId
263
- }
264
250
]
265
251
source : {
266
252
script : '''
267
- param (
268
- [string]$AutomationAccountName,
269
- [string]$ContainerName,
270
- [string]$Environment,
271
- [string]$ResourceGroupName,
272
- [string]$RunbookName,
273
- [string]$StorageAccountName,
274
- [string]$StorageEndpoint,
275
- [string]$SubscriptionId,
276
- [string]$TenantId,
277
- [string]$UserAssignedIdentityClientId,
278
- [string]$UserAssignedIdentityObjectId
253
+ param(
254
+ [string]$ResourceManagerUri,
255
+ [string]$RunBookResourceId,
256
+ [string]$RunBookScriptContent,
257
+ [string]$UserAssignedIdentityClientId
279
258
)
280
259
$ErrorActionPreference = 'Stop'
281
260
$WarningPreference = 'SilentlyContinue'
282
- $BlobName = 'New-AzureZeroTrustImageBuild.ps1'
283
- $StorageAccountUrl = "https://" + $StorageAccountName + ".blob." + $StorageEndpoint + "/"
284
- $TokenUri = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=$StorageAccountUrl&object_id=$UserAssignedIdentityObjectId"
285
- $AccessToken = ((Invoke-WebRequest -Headers @{Metadata=$true} -Uri $TokenUri -UseBasicParsing).Content | ConvertFrom-Json).access_token
286
- $File = "$env:windir\temp\$BlobName"
287
- do
288
- {
289
- try
290
- {
291
- Write-Output "Download Attempt $i"
292
- Invoke-WebRequest -Headers @{"x-ms-version"="2017-11-09"; Authorization ="Bearer $AccessToken"} -Uri "$StorageAccountUrl$ContainerName/$BlobName" -OutFile $File
293
- }
294
- catch [System.Net.WebException]
295
- {
296
- Start-Sleep -Seconds 60
297
- $i++
298
- if($i -gt 10){throw}
299
- continue
300
- }
301
- catch
302
- {
303
- $Output = $_ | select *
304
- Write-Output $Output
305
- throw
261
+
262
+ Try {
263
+ # Fix the resource manager URI since only AzureCloud contains a trailing slash
264
+ $ResourceManagerUriFixed = if($ResourceManagerUri[-1] -eq '/'){$ResourceManagerUri.Substring(0,$ResourceManagerUri.Length - 1)} else {$ResourceManagerUri}
265
+
266
+ # Get an access token for Azure resources
267
+ $AzureManagementAccessToken = (Invoke-RestMethod `
268
+ -Headers @{Metadata="true"} `
269
+ -Uri $('http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=' + $ResourceManagerUriFixed + '&client_id=' + $UserAssignedIdentityClientId)).access_token
270
+
271
+ # Set header for Azure Management API
272
+ $AzureManagementHeader = @{
273
+ 'Content-Type'='application/json'
274
+ 'Authorization'='Bearer ' + $AzureManagementAccessToken
306
275
}
276
+
277
+ # Upload Content to Draft
278
+ Invoke-RestMethod -Headers $AzureManagementHeader -Method 'PUT' -Uri $($ResourceManagerUriFixed + $RunBookResourceId + '/draft/content?api-version=2023-11-01') -Body $RunBookScriptContent
279
+
280
+ # Publish the RunBook
281
+ Invoke-RestMethod -Headers $AzureManagementHeader -Method 'POST' -Uri $($ResourceManagerUriFixed + $RunBookResourceId + '/publish?api-version=2023-11-01')
282
+ }
283
+ catch {
284
+ throw
307
285
}
308
- until(Test-Path -Path $File)
309
- Connect-AzAccount -Environment $Environment -Tenant $TenantId -Subscription $SubscriptionId -Identity -AccountId $UserAssignedIdentityClientId | Out-Null
310
- Import-AzAutomationRunbook -Name $RunbookName -Path $File -Type PowerShell -AutomationAccountName $AutomationAccountName -ResourceGroupName $ResourceGroupName -Published -Force | Out-Null
311
286
'''
312
287
}
313
288
}
@@ -341,7 +316,7 @@ resource jobSchedule 'Microsoft.Automation/automationAccounts/jobSchedules@2022-
341
316
}
342
317
}
343
318
dependsOn : [
344
- runCommand
319
+ updateRunBook
345
320
]
346
321
}
347
322
@@ -371,7 +346,7 @@ resource hybridRunbookWorker 'Microsoft.Automation/automationAccounts/hybridRunb
371
346
vmResourceId : virtualMachine .id
372
347
}
373
348
dependsOn : [
374
- runCommand
349
+ updateRunBook
375
350
]
376
351
}
377
352
@@ -391,7 +366,7 @@ resource extension_HybridWorker 'Microsoft.Compute/virtualMachines/extensions@20
391
366
}
392
367
}
393
368
dependsOn : [
394
- runCommand
369
+ updateRunBook
395
370
]
396
371
}
397
372
@@ -420,6 +395,6 @@ resource extension_JsonADDomainExtension 'Microsoft.Compute/virtualMachines/exte
420
395
}
421
396
dependsOn : [
422
397
extension_HybridWorker
423
- runCommand
398
+ updateRunBook
424
399
]
425
400
}
0 commit comments