Home NewsX Automating Disk Cleanup in Azure Using PowerShell, Azure Resource Graph and LastOwnershipUpdateTime

Automating Disk Cleanup in Azure Using PowerShell, Azure Resource Graph and LastOwnershipUpdateTime

by info.odysseyx@gmail.com
0 comment 14 views


Automating Disk Cleanup in Azure Using PowerShell, Azure Resource Graph, and LastOwnershipUpdateTime

Managing resources efficiently in Azure is critical to cost optimization and operational efficiency. Recently, new properties, LastOwnershipUpdateTimeIntroduced in Azure Disks, it helps in tracking the last state change of the disk. In this blog, we will see how to automate the cleanup of unattached disks older than 60 days using PowerShell and Azure Resource Graph queries.

introduction

This post covers:

  • How to query Azure resources to find unattached disks LastOwnershipUpdateTime.
  • How to automate the deletion of these disks using PowerShell.

Set up

Before looking at the script, check the following:

  • The latest version of the Azure PowerShell module (Az (module version 11.0.0 or later).
  • Appropriate permissions to manage Azure resources.

Step by step guide

1. Make sure you have the latest Azure PowerShell modules

First, you need to update your Azure PowerShell module to the latest version to access the new properties.

# Check for existing Az modules 
get-module -ListAvailable -Name Az* | Select-Object Name, Version

# Uninstall all old versions of Az modules
Get-Module -ListAvailable Az* | foreach { Uninstall-Module -Name $_.Name -RequiredVersion $_.Version }

# Install the latest Az module
Install-Module -Name Az -AllowClobber -Scope CurrentUser

# Verify the installation
Get-Module -ListAvailable -Name Az* | Select-Object Name, Version

2. Writing Azure Resource Graph Queries

The following query finds disks whose ownership has not been updated in the last 60 days.

$disksToBeRemoved = Search-AzGraph -Query '
resources
| where type == "microsoft.compute/disks"
| where todatetime(properties.LastOwnershipUpdateTime) < ago(60d)
| project name, diskState = properties.diskState, lastUpdateTime = format_datetime(todatetime(properties.LastOwnershipUpdateTime), "dd-MM-yyyy")
'

3. Automate disk deletion

Once the disks are identified, you can automate their deletion using PowerShell.

foreach ($disk in $disksToBeRemoved) {
    # Simulate the deletion action
    Write-Output "Disk: $($disk.name), Last Update: $($disk.lastUpdateTime)"
    
    # Actual deletion command
    Remove-AzDisk -Name $disk.name
}

4. Putting it all together

Let’s look at all the parts together.

# Ensure you have the latest Azure PowerShell module
Install-Module -Name Az -AllowClobber -Scope CurrentUser -Force

# Authenticate to Azure
Connect-AzAccount

# Define the query to find disks that haven't had ownership updates in the last 60 days
$disksToBeRemoved = Search-AzGraph -Query '
resources
| where type == "microsoft.compute/disks"
| where todatetime(properties.LastOwnershipUpdateTime) < ago(60d)
| project name, diskState = properties.diskState, lastUpdateTime = format_datetime(todatetime(properties.LastOwnershipUpdateTime), "dd-MM-yyyy")
'

# Loop through each disk and delete it
foreach ($disk in $disksToBeRemoved) {
    # Output the disk information for verification
    Write-Output "Disk: $($disk.name), Last Update: $($disk.lastUpdateTime)"
    
    # Actual deletion command
    Remove-AzDisk -Name $disk.name -Force
}

explanation

  1. Installation module: Make sure you have the latest Azure PowerShell modules installed.
  2. Connect-Az Account: Authenticates your session with Azure.
  3. Search-AzGraph: Query Azure Resource Graph to find disks older than 60 days.
  4. Remove-AzDisk: Delete each disk found by the query.

conclusion

Automating the cleanup of unattached disks older than 60 days can help you optimize resource usage and reduce costs. Following this guide will help you implement a similar solution in your Azure environment. If you have any questions or feedback, please leave a comment below.

disclaimer

The sample script is not supported by any Microsoft standard support program or service. The sample script or Power BI dashboard is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties, including but not limited to the implied warranties of merchantability or fitness for a particular purpose. The entire risk arising out of the use or performance of the sample script and documentation remains with you. In no event shall Microsoft, the authors, or anyone else involved in the creation, production, or delivery of the script or Power BI dashboard be liable for any damages whatsoever (including but not limited to damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample script or documentation, even if Microsoft has been advised of the possibility of such damages. This blog post was created with the assistance of Generation AI.





Source link

You may also like

Leave a Comment

Our Company

Welcome to OdysseyX, your one-stop destination for the latest news and opportunities across various domains.

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

Laest News

@2024 – All Right Reserved. Designed and Developed by OdysseyX