Skip to content

Commit cc67b8f

Browse files
heathsazure-sdk
authored andcommitted
Use $using:r instead of creating ScriptBlock
More idiomatic for passing ScriptBlocks to jobs.
1 parent 4003282 commit cc67b8f

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

eng/common/scripts/Helpers/Resource-Helpers.ps1

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ filter Remove-PurgeableResources {
115115
Write-Warning "Key Vault '$($r.VaultName)' has purge protection enabled and may not be purged for $($r.SoftDeleteRetentionInDays) days"
116116
}
117117

118-
Wait-PurgeableResource -Resource $r -Timeout $Timeout -PassThru:$PassThru -ScriptBlock { Remove-AzKeyVault -VaultName $r.VaultName -Location $r.Location -InRemovedState -Force -ErrorAction Continue }
118+
# Using `$($using:r)` in the `-ScriptBlock` to make sure `$r` is captured for jobs.
119+
Wait-PurgeableResource -Resource $r -Timeout $Timeout -PassThru:$PassThru -ScriptBlock { Remove-AzKeyVault -VaultName $($using:r).VaultName -Location $($using:r).Location -InRemovedState -Force -ErrorAction Continue }
119120
}
120121

121122
'Managed HSM' {
@@ -124,15 +125,16 @@ filter Remove-PurgeableResources {
124125
Write-Warning "Managed HSM '$($r.Name)' has purge protection enabled and may not be purged for $($r.SoftDeleteRetentionInDays) days"
125126
}
126127

128+
# Using `$($using:r)` in the `-ScriptBlock` to make sure `$r` is captured for jobs.
127129
Wait-PurgeableResource -Resource $r -Timeout $Timeout -PassThru:$PassThru -ScriptBlock {
128-
$response = Invoke-AzRestMethod -Method POST -Path "/subscriptions/$subscriptionId/providers/Microsoft.KeyVault/locations/$($r.Location)/deletedManagedHSMs/$($r.Name)/purge?api-version=2021-04-01-preview" -ErrorAction Ignore
130+
$response = Invoke-AzRestMethod -Method POST -Path "/subscriptions/$subscriptionId/providers/Microsoft.KeyVault/locations/$($($using:r).Location)/deletedManagedHSMs/$($($using:r).Name)/purge?api-version=2021-04-01-preview" -ErrorAction Ignore
129131
if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300) {
130-
Write-Warning "Successfully requested that Managed HSM '$($r.Name)' be purged, but may take a few minutes before it is actually purged."
132+
Write-Warning "Successfully requested that Managed HSM '$($($using:r).Name)' be purged, but may take a few minutes before it is actually purged."
131133
} elseif ($response.Content) {
132134
$content = $response.Content | ConvertFrom-Json
133135
if ($content.error) {
134136
$err = $content.error
135-
Write-Warning "Failed to deleted Managed HSM '$($r.Name)': ($($err.code)) $($err.message)"
137+
Write-Warning "Failed to deleted Managed HSM '$($($using:r).Name)': ($($err.code)) $($err.message)"
136138
}
137139
}
138140
}
@@ -166,15 +168,10 @@ function Wait-PurgeableResource {
166168
[switch] $PassThru
167169
)
168170

169-
# We build a new AST to pass the `$Resource` as `$r`, which is the `foreach` variable declared in `Remove-PurgeableResources` above.
170-
# This is done to make writing a purge script feel natural without having to worry about scope since `[ScriptBlock].GetNewClosure()`
171-
# does not capture `$r` appropriately. If the variable name in the `foreach` above is changed, it must be changed here as well.
172-
$scriptBlockAst = [System.Management.Automation.Language.Parser]::ParseInput(@"
173-
param (`$r)
174-
$($ScriptBlock.Ast.EndBlock.ToString())
175-
"@, [ref] $null, [ref] $null)
176-
177-
$j = Start-ThreadJob -ScriptBlock $scriptBlockAst.GetScriptBlock() -ArgumentList $Resource
171+
# ScriptBlocks need to use `$($using:r)` to capture the `$r` value in the `foreach` loop
172+
# or they will not resolved when invoked as a job. See
173+
# https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_remote_variables
174+
$j = Start-ThreadJob -ScriptBlock $ScriptBlock
178175
$null = Wait-Job -Job $j -Timeout $Timeout
179176

180177
if ($j.State -eq 'Running') {

0 commit comments

Comments
 (0)