|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Azure Automatic Cleanup Script for Unused Images. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + This script creates a new Azure Resource Group with the Azure PowerShell. |
| 7 | + The script uses the following Azure PowerShell command: |
| 8 | + New-AzResourceGroup -Name $AzResourceGroup -Location $AzLocation |
| 9 | +
|
| 10 | +.PARAMETER DryRun |
| 11 | + It allows you to simulate the execution of a script or command without actually making any changes to the system. |
| 12 | +
|
| 13 | +.PARAMETER ResourceGroup |
| 14 | + Defines the Azure Resource Group that a particular command or operation should target. |
| 15 | +
|
| 16 | +.PARAMETER ImageId |
| 17 | + Defines the unique identifier of an image resource in Azure. |
| 18 | +
|
| 19 | +.EXAMPLE |
| 20 | + param( |
| 21 | + [string]$ResourceGroup, # Name of the target resource group |
| 22 | + [string]$ImageId, # Unique ID of the image to check or remove |
| 23 | + [switch]$DryRun # Simulate the process without making changes |
| 24 | +) |
| 25 | +
|
| 26 | +.LINK |
| 27 | + https://github.com/xoap-io/scripted-actions |
| 28 | +
|
| 29 | +.COMPONENT |
| 30 | + Azure PowerShell |
| 31 | +
|
| 32 | +#> |
| 33 | + |
| 34 | +# Variables |
| 35 | +# Specify the subscription ID and resource group(s) |
| 36 | +param ( |
| 37 | + [switch]$DryRun, |
| 38 | + [string]$ResourceGroup |
| 39 | +) |
| 40 | + # Set to $false to actually delete the images |
| 41 | + |
| 42 | +# Function to check if an image is in use |
| 43 | +function Is-ImageInUse { |
| 44 | + param ( |
| 45 | + [string]$ImageId |
| 46 | + ) |
| 47 | + |
| 48 | + # Get all VMs and check if any reference the image |
| 49 | + $vms = Get-AzVM -Status |
| 50 | + foreach ($vm in $vms) { |
| 51 | + if ($vm.StorageProfile.ImageReference.Id -eq $ImageId) { |
| 52 | + return $true |
| 53 | + } |
| 54 | + } |
| 55 | + return $false |
| 56 | +} |
| 57 | + if($ResourceGroup) { |
| 58 | + $images = Get-AzImage -ResourceGroupName $ResourceGroup |
| 59 | + |
| 60 | + foreach ($image in $images) { |
| 61 | + Write-Host "Processing image: $($image.Name)" |
| 62 | + |
| 63 | + if (-not (Is-ImageInUse -ImageId $image.Id)) { |
| 64 | + Write-Host "Image $($image.Name) is not in use." |
| 65 | + |
| 66 | + if (-not $DryRun) { |
| 67 | + # Delete the unused image |
| 68 | + Write-Host "Deleting image: $($image.Name)" |
| 69 | + Remove-AzImage -ResourceGroupName $image.ResourceGroupName -Name $image.Name -Force |
| 70 | + } else { |
| 71 | + Write-Host "Dry Run: Image $($image.Name) would be deleted." |
| 72 | + } |
| 73 | + } else { |
| 74 | + Write-Host "Image $($image.Name) is in use by one or more VMs." |
| 75 | + } |
| 76 | +} |
| 77 | + } |
| 78 | + else { |
| 79 | + |
| 80 | +$images = Get-AzImage |
| 81 | + |
| 82 | + foreach ($image in $images) { |
| 83 | + Write-Host "Processing image: $($image.Name)" |
| 84 | + |
| 85 | + if (-not (Is-ImageInUse -ImageId $image.Id)) { |
| 86 | + Write-Host "Image $($image.Name) is not in use." |
| 87 | + |
| 88 | + if (-not $DryRun) { |
| 89 | + # Delete the unused image |
| 90 | + Write-Host "Deleting image: $($image.Name)" |
| 91 | + Remove-AzImage -ResourceGroupName $image.ResourceGroupName -Name $image.Name -Force |
| 92 | + } else { |
| 93 | + Write-Host "Dry Run: Image $($image.Name) would be deleted." |
| 94 | + } |
| 95 | + } else { |
| 96 | + Write-Host "Image $($image.Name) is in use by one or more VMs." |
| 97 | + } |
| 98 | +} |
| 99 | + } |
| 100 | + # Get all images in the resource group |
| 101 | + |
| 102 | + |
| 103 | +Write-Host "Image cleanup completed." |
0 commit comments