-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDelete-ComputeGalleryImages.ps1
77 lines (68 loc) · 2.95 KB
/
Delete-ComputeGalleryImages.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
param (
[string]$WorkingDirectory = (Get-Location).Path,
[string]$ApplicationId,
[string]$TenantId,
[string]$Secret,
[string]$SubscriptionId
)
function Connect-AzAccountWithServicePrincipal
{
param (
[string]$ApplicationId,
[string]$TenantId,
[string]$Secret,
[string]$SubscriptionId
)
try
{
$SecureSecret = $Secret | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($ApplicationId, $SecureSecret)
Connect-AzAccount -ServicePrincipal -Credential $Credential -Tenant $TenantId -ErrorAction Stop | Out-Null
if (-not [string]::IsNullOrEmpty($SubscriptionId))
{
Set-AzContext -SubscriptionId $SubscriptionId | Out-Null
}
Write-Host "[$( $MyInvocation.MyCommand.Name )] Success: Successfully logged in to Azure." -ForegroundColor Cyan
}
catch
{
Write-Error "[$( $MyInvocation.MyCommand.Name )] Error: Failed to log in to Azure with the provided service principal details: $_"
throw $_
}
}
try
{
Connect-AzAccountWithServicePrincipal -ApplicationId $ApplicationId -SubscriptionId $SubscriptionId -TenantId $TenantId -Secret $Secret
# List and delete all managed images in the subscription
$managedImages = Get-AzImage
foreach ($image in $managedImages)
{
Write-Host "Deleting managed image: $( $image.Name ) in resource group: $( $image.ResourceGroupName )"
# Uncomment the next line to actually perform the deletion
# Remove-AzImage -ImageName $image.Name -ResourceGroupName $image.ResourceGroupName -Force
}
# List all galleries
$galleries = Get-AzGallery
foreach ($gallery in $galleries)
{
Write-Host "Processing gallery: $( $gallery.Name )"
# List all image definitions within the gallery
$imageDefinitions = Get-AzGalleryImageDefinition -ResourceGroupName $gallery.ResourceGroupName -GalleryName $gallery.Name
foreach ($imageDef in $imageDefinitions)
{
Write-Host "Processing image definition: $( $imageDef.Name )"
# List all image versions for the current image definition
$imageVersions = Get-AzGalleryImageVersion -ResourceGroupName $gallery.ResourceGroupName -GalleryName $gallery.Name -GalleryImageDefinitionName $imageDef.Name
foreach ($imageVersion in $imageVersions)
{
Write-Host "Deleting image version: $( $imageVersion.Name )"
# Uncomment the next line to actually perform the deletion
Remove-AzGalleryImageVersion -ResourceGroupName $gallery.ResourceGroupName -GalleryName $gallery.Name -GalleryImageDefinitionName $imageDef.Name -GalleryImageVersionName $imageVersion.Name -Force
}
}
}
}
catch
{
Write-Error "Error: An exception has occurred $_"
}