Streamline Azure Cost Management: Automate Disk Cleanup with LastOwnershipUpdateTime and PowerShell

Posted by

Automating disk cleanup in Azure is essential for cost efficiency. This guide introduces the new LastOwnershipUpdateTime property for tracking disk state changes. It explains how to identify unattached disks older than 60 days using PowerShell and Azure Resource Graph, and provides a step-by-step process to automate their deletion.2.

“`html

Automating Disk Cleanup in Azure: A Game Changer

Managing resources in Azure is essential for optimizing costs and enhancing operational efficiency. Recently, Microsoft introduced a new property called LastOwnershipUpdateTime for Azure disks. This feature tracks the last state change of a disk, making it easier to manage resources effectively.

What’s New?

The introduction of the LastOwnershipUpdateTime property allows users to identify unattached disks that haven’t been updated in over 60 days. This new feature simplifies the process of automating disk cleanup using PowerShell and Azure Resource Graph queries.

“Automating the cleanup of unattached disks older than 60 days helps optimize resource usage and reduce costs.”

Major Updates: Step-by-Step Guide

To automate disk cleanup, follow these steps:

1. Update Azure PowerShell Modules

Ensure you have the latest version of the Azure PowerShell module. Use the following commands:

# Check for existing Az modules
get-module -ListAvailable -Name Az* | Select-Object Name, Version
# Uninstall old versions
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. Write the Azure Resource Graph Query

This query identifies 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")
'

3. Automate Disk Deletion

Once identified, automate the deletion of these disks:

foreach ($disk in $disksToBeRemoved) {
    Write-Output "Disk: $($disk.name), Last Update: $($disk.lastUpdateTime)"
    Remove-AzDisk -Name $disk.name
}

What's Important to Know?

Implementing this automation can significantly streamline Azure resource management. It helps ensure that you only retain necessary resources, thus minimizing costs. However, always verify the disks before deletion to avoid accidental data loss.

"By following this guide, you can implement a similar solution in your Azure environment."

Conclusion

Automating disk cleanup is a straightforward yet powerful way to enhance your Azure management strategy. For any questions or feedback, feel free to leave a comment below!

```

  • Introduces the LastOwnershipUpdateTime property for Azure disks.
  • Focuses on optimizing Azure resource management for cost reduction.
  • Provides a detailed guide for using PowerShell to automate disk cleanup.
  • Emphasizes the importance of having the latest Azure PowerShell module installed.
  • Encourages community engagement through comments and feedback on the process.
  • ```

    From the Core Infrastructure and Security Blog



    Related Posts
    Unlock the Mystery of Why BitLocker is Not Resuming After Reboot Count Has Been Reached

    Security hardening changes needed on domain controllers in IT environments to address CVE-2022-37967 will enter the Third deployment phase, as Read more

    Unlock the Power of Automation with Dynamic Thresholds in Azure Monitor Log Alerts

    What is Azure Monitor? Azure Monitor is a service that provides a single source for monitoring Azure resources. It provides Read more

    Connect to Azure Government with Azure Data Studio – Unlocking the Power of Government Cloud Computing!

    Azure Data Studio Connections to Azure Government Azure Data Studio is an open source, cross-platform database tool for data professionals Read more

    Unlock the Power of Kubernetes with External DNS for Azure DNS & AKS!

    What is Kubernetes External DNS? Kubernetes External DNS is a service that allows users to manage and configure public DNS Read more