@@ -93,6 +93,11 @@ param (
9393 })]
9494 [array ] $AllowIpRanges = @ (),
9595
96+ # Instead of running the post script, create a wrapped file to run it with parameters
97+ # so that CI can run it in a subsequent step with a refreshed azure login
98+ [Parameter ()]
99+ [string ] $SelfContainedPostScript ,
100+
96101 [Parameter ()]
97102 [switch ] $CI = ($null -ne $env: SYSTEM_TEAMPROJECTID ),
98103
@@ -625,9 +630,41 @@ try {
625630 SetResourceNetworkAccessRules - ResourceGroupName $ResourceGroupName - AllowIpRanges $AllowIpRanges - CI:$CI
626631
627632 $postDeploymentScript = $templateFile.originalFilePath | Split-Path | Join-Path - ChildPath " $ResourceType -resources-post.ps1"
633+
634+ if ($SelfContainedPostScript -and ! (Test-Path $postDeploymentScript )) {
635+ throw " -SelfContainedPostScript is not supported if there is no 'test-resources-post.ps1' script in the deployment template directory"
636+ }
637+
628638 if (Test-Path $postDeploymentScript ) {
629- Log " Invoking post-deployment script '$postDeploymentScript '"
630- & $postDeploymentScript - ResourceGroupName $ResourceGroupName - DeploymentOutputs $deploymentOutputs @PSBoundParameters
639+ if ($SelfContainedPostScript ) {
640+ Log " Creating invokable post-deployment script '$SelfContainedPostScript ' from '$postDeploymentScript '"
641+
642+ $deserialized = @ {}
643+ foreach ($parameter in $PSBoundParameters.GetEnumerator ()) {
644+ if ($parameter.Value -is [System.Management.Automation.SwitchParameter ]) {
645+ $deserialized [$parameter.Key ] = $parameter.Value.ToBool ()
646+ } else {
647+ $deserialized [$parameter.Key ] = $parameter.Value
648+ }
649+ }
650+ $deserialized [' ResourceGroupName' ] = $ResourceGroupName
651+ $deserialized [' DeploymentOutputs' ] = $deploymentOutputs
652+ $serialized = $deserialized | ConvertTo-Json
653+
654+ $outScript = @"
655+ `$ parameters = `@'
656+ $serialized
657+ '`@ | ConvertFrom-Json -AsHashtable
658+ # Set global variables that aren't always passed as parameters
659+ `$ ResourceGroupName = `$ parameters.ResourceGroupName
660+ `$ DeploymentOutputs = `$ parameters.DeploymentOutputs
661+ $postDeploymentScript `@parameters
662+ "@
663+ $outScript | Out-File $SelfContainedPostScript
664+ } else {
665+ Log " Invoking post-deployment script '$postDeploymentScript '"
666+ & $postDeploymentScript - ResourceGroupName $ResourceGroupName - DeploymentOutputs $deploymentOutputs @PSBoundParameters
667+ }
631668 }
632669
633670 if ($templateFile.jsonFilePath.EndsWith (' .compiled.json' )) {
0 commit comments