|
1 | 1 | # Add 'AzsdkResourceType' member to outputs since actual output types have changed over the years. |
2 | 2 |
|
3 | 3 | function Get-PurgeableGroupResources { |
4 | | - param ( |
5 | | - [Parameter(Mandatory=$true, Position=0)] |
6 | | - [string] $ResourceGroupName |
7 | | - ) |
8 | | - |
9 | | - # Get any Key Vaults that will be deleted so they can be purged later if soft delete is enabled. |
10 | | - Get-AzKeyVault @PSBoundParameters | ForEach-Object { |
11 | | - # Enumerating vaults from a resource group does not return all properties we required. |
12 | | - Get-AzKeyVault -VaultName $_.VaultName | Where-Object { $_.EnableSoftDelete } ` |
13 | | - | Add-Member -MemberType NoteProperty -Name AzsdkResourceType -Value 'Key Vault' -PassThru |
14 | | - } |
| 4 | + param ( |
| 5 | + [Parameter(Mandatory=$true, Position=0)] |
| 6 | + [string] $ResourceGroupName |
| 7 | + ) |
| 8 | + $purgeableResources = @() |
15 | 9 |
|
16 | | - # Get any Managed HSMs in the resource group, for which soft delete cannot be disabled. |
17 | | - Get-AzKeyVaultManagedHsm @PSBoundParameters ` |
18 | | - | Add-Member -MemberType NoteProperty -Name AzsdkResourceType -Value 'Managed HSM' -PassThru |
19 | | -} |
| 10 | + Write-Verbose "Retrieving deleted Key Vaults from resource group $ResourceGroupName" |
| 11 | + |
| 12 | + # Get any Key Vaults that will be deleted so they can be purged later if soft delete is enabled. |
| 13 | + $deletedKeyVaults = Get-AzKeyVault -ResourceGroupName $ResourceGroupName -ErrorAction Ignore | ForEach-Object { |
| 14 | + # Enumerating vaults from a resource group does not return all properties we required. |
| 15 | + Get-AzKeyVault -VaultName $_.VaultName -ErrorAction Ignore | Where-Object { $_.EnableSoftDelete } ` |
| 16 | + | Add-Member -MemberType NoteProperty -Name AzsdkResourceType -Value 'Key Vault' -PassThru |
| 17 | + } |
| 18 | + |
| 19 | + if ($deletedKeyVaults) { |
| 20 | + Write-Verbose "Found $($deletedKeyVaults.Count) deleted Key Vaults to potentially purge." |
| 21 | + $purgeableResources += $deletedKeyVaults |
| 22 | + } |
| 23 | + |
| 24 | + Write-Verbose "Retrieving deleted Managed HSMs from resource group $ResourceGroupName" |
| 25 | + |
| 26 | + # Get any Managed HSMs in the resource group, for which soft delete cannot be disabled. |
| 27 | + $deletedHsms = Get-AzKeyVaultManagedHsm -ResourceGroupName $ResourceGroupName -ErrorAction Ignore ` |
| 28 | + | Add-Member -MemberType NoteProperty -Name AzsdkResourceType -Value 'Managed HSM' -PassThru |
| 29 | + |
| 30 | + if ($deletedHsms) { |
| 31 | + Write-Verbose "Found $($deletedHsms.Count) deleted Managed HSMs to potentially purge." |
| 32 | + $purgeableResources += $deletedHsms |
| 33 | + } |
20 | 34 |
|
| 35 | + return $purgeableResources |
| 36 | +} |
21 | 37 | function Get-PurgeableResources { |
22 | | - $subscriptionId = (Get-AzContext).Subscription.Id |
23 | | - |
24 | | - # Get deleted Key Vaults for the current subscription. |
25 | | - Get-AzKeyVault -InRemovedState ` |
26 | | - | Add-Member -MemberType NoteProperty -Name AzsdkResourceType -Value 'Key Vault' -PassThru |
27 | | - |
28 | | - # Get deleted Managed HSMs for the current subscription. |
29 | | - $response = Invoke-AzRestMethod -Method GET -Path "/subscriptions/$subscriptionId/providers/Microsoft.KeyVault/deletedManagedHSMs?api-version=2021-04-01-preview" -ErrorAction Ignore |
30 | | - if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300 -and $response.Content) { |
31 | | - $content = $response.Content | ConvertFrom-Json |
32 | | - foreach ($r in $content.value) { |
33 | | - [pscustomobject] @{ |
34 | | - AzsdkResourceType = 'Managed HSM' |
35 | | - Id = $r.id |
36 | | - Name = $r.name |
37 | | - Location = $r.properties.location |
38 | | - DeletionDate = $r.properties.deletionDate -as [DateTime] |
39 | | - ScheduledPurgeDate = $r.properties.scheduledPurgeDate -as [DateTime] |
40 | | - EnablePurgeProtection = $r.properties.purgeProtectionEnabled |
41 | | - } |
42 | | - } |
| 38 | + $purgeableResources = @() |
| 39 | + $subscriptionId = (Get-AzContext).Subscription.Id |
| 40 | + |
| 41 | + Write-Verbose "Retrieving deleted Key Vaults from subscription $subscriptionId" |
| 42 | + |
| 43 | + # Get deleted Key Vaults for the current subscription. |
| 44 | + $deletedKeyVaults = Get-AzKeyVault -InRemovedState ` |
| 45 | + | Add-Member -MemberType NoteProperty -Name AzsdkResourceType -Value 'Key Vault' -PassThru |
| 46 | + |
| 47 | + if ($deletedKeyVaults) { |
| 48 | + Write-Verbose "Found $($deletedKeyVaults.Count) deleted Key Vaults to potentially purge." |
| 49 | + $purgeableResources += $deletedKeyVaults |
| 50 | + } |
| 51 | + |
| 52 | + Write-Verbose "Retrieving deleted Managed HSMs from subscription $subscriptionId" |
| 53 | + |
| 54 | + # Get deleted Managed HSMs for the current subscription. |
| 55 | + $response = Invoke-AzRestMethod -Method GET -Path "/subscriptions/$subscriptionId/providers/Microsoft.KeyVault/deletedManagedHSMs?api-version=2021-04-01-preview" -ErrorAction Ignore |
| 56 | + if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300 -and $response.Content) { |
| 57 | + $content = $response.Content | ConvertFrom-Json |
| 58 | + |
| 59 | + $deletedHsms = @() |
| 60 | + foreach ($r in $content.value) { |
| 61 | + $deletedHsms += [pscustomobject] @{ |
| 62 | + AzsdkResourceType = 'Managed HSM' |
| 63 | + Id = $r.id |
| 64 | + Name = $r.name |
| 65 | + Location = $r.properties.location |
| 66 | + DeletionDate = $r.properties.deletionDate -as [DateTime] |
| 67 | + ScheduledPurgeDate = $r.properties.scheduledPurgeDate -as [DateTime] |
| 68 | + EnablePurgeProtection = $r.properties.purgeProtectionEnabled |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if ($deletedHsms) { |
| 73 | + Write-Verbose "Found $($deletedHsms.Count) deleted Managed HSMs to potentially purge." |
| 74 | + $purgeableResources += $deletedHsms |
43 | 75 | } |
| 76 | + } |
| 77 | + |
| 78 | + return $purgeableResources |
44 | 79 | } |
45 | 80 |
|
46 | 81 | # A filter differs from a function by teating body as -process {} instead of -end {}. |
47 | 82 | # This allows you to pipe a collection and process each item in the collection. |
48 | 83 | filter Remove-PurgeableResources { |
49 | | - param ( |
50 | | - [Parameter(Position=0, ValueFromPipeline=$true)] |
51 | | - [object[]] $Resource |
52 | | - ) |
| 84 | + param ( |
| 85 | + [Parameter(Position=0, ValueFromPipeline=$true)] |
| 86 | + [object[]] $Resource |
| 87 | + ) |
53 | 88 |
|
54 | | - if (!$Resource) { |
55 | | - return |
56 | | - } |
| 89 | + if (!$Resource) { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + $subscriptionId = (Get-AzContext).Subscription.Id |
57 | 94 |
|
58 | | - $subscriptionId = (Get-AzContext).Subscription.Id |
59 | | - |
60 | | - foreach ($r in $Resource) { |
61 | | - switch ($r.AzsdkResourceType) { |
62 | | - 'Key Vault' { |
63 | | - Log "Attempting to purge $($r.AzsdkResourceType) '$($r.VaultName)'" |
64 | | - if ($r.EnablePurgeProtection) { |
65 | | - # We will try anyway but will ignore errors |
66 | | - Write-Warning "Key Vault '$($r.VaultName)' has purge protection enabled and may not be purged for $($r.SoftDeleteRetentionInDays) days" |
67 | | - } |
68 | | - |
69 | | - Remove-AzKeyVault -VaultName $r.VaultName -Location $r.Location -InRemovedState -Force -ErrorAction Continue |
70 | | - } |
71 | | - |
72 | | - 'Managed HSM' { |
73 | | - Log "Attempting to purge $($r.AzsdkResourceType) '$($r.Name)'" |
74 | | - if ($r.EnablePurgeProtection) { |
75 | | - # We will try anyway but will ignore errors |
76 | | - Write-Warning "Managed HSM '$($r.Name)' has purge protection enabled and may not be purged for $($r.SoftDeleteRetentionInDays) days" |
77 | | - } |
78 | | - |
79 | | - $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 |
80 | | - if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300) { |
81 | | - Write-Warning "Successfully requested that Managed HSM '$($r.Name)' be purged, but may take a few minutes before it is actually purged." |
82 | | - } elseif ($response.Content) { |
83 | | - $content = $response.Content | ConvertFrom-Json |
84 | | - if ($content.error) { |
85 | | - $err = $content.error |
86 | | - Write-Warning "Failed to deleted Managed HSM '$($r.Name)': ($($err.code)) $($err.message)" |
87 | | - } |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - default { |
92 | | - Write-Warning "Cannot purge resource type $($r.AzsdkResourceType). Add support to https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/scripts/Helpers/Resource-Helpers.ps1." |
93 | | - } |
| 95 | + foreach ($r in $Resource) { |
| 96 | + switch ($r.AzsdkResourceType) { |
| 97 | + 'Key Vault' { |
| 98 | + Log "Attempting to purge $($r.AzsdkResourceType) '$($r.VaultName)'" |
| 99 | + if ($r.EnablePurgeProtection) { |
| 100 | + # We will try anyway but will ignore errors |
| 101 | + Write-Warning "Key Vault '$($r.VaultName)' has purge protection enabled and may not be purged for $($r.SoftDeleteRetentionInDays) days" |
94 | 102 | } |
| 103 | + |
| 104 | + Remove-AzKeyVault -VaultName $r.VaultName -Location $r.Location -InRemovedState -Force -ErrorAction Continue |
| 105 | + } |
| 106 | + |
| 107 | + 'Managed HSM' { |
| 108 | + Log "Attempting to purge $($r.AzsdkResourceType) '$($r.Name)'" |
| 109 | + if ($r.EnablePurgeProtection) { |
| 110 | + # We will try anyway but will ignore errors |
| 111 | + Write-Warning "Managed HSM '$($r.Name)' has purge protection enabled and may not be purged for $($r.SoftDeleteRetentionInDays) days" |
| 112 | + } |
| 113 | + |
| 114 | + $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 |
| 115 | + if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300) { |
| 116 | + Write-Warning "Successfully requested that Managed HSM '$($r.Name)' be purged, but may take a few minutes before it is actually purged." |
| 117 | + } elseif ($response.Content) { |
| 118 | + $content = $response.Content | ConvertFrom-Json |
| 119 | + if ($content.error) { |
| 120 | + $err = $content.error |
| 121 | + Write-Warning "Failed to deleted Managed HSM '$($r.Name)': ($($err.code)) $($err.message)" |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + default { |
| 127 | + Write-Warning "Cannot purge resource type $($r.AzsdkResourceType). Add support to https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/scripts/Helpers/Resource-Helpers.ps1." |
| 128 | + } |
95 | 129 | } |
| 130 | + } |
96 | 131 | } |
97 | 132 |
|
98 | 133 | # The Log function can be overridden by the sourcing script. |
99 | 134 | function Log($Message) { |
100 | | - Write-Host ('{0} - {1}' -f [DateTime]::Now.ToLongTimeString(), $Message) |
| 135 | + Write-Host ('{0} - {1}' -f [DateTime]::Now.ToLongTimeString(), $Message) |
101 | 136 | } |
0 commit comments