Automating Disk Cleanup in Azure Using PowerShell, Azure Resource Graph and LastOwnershipUpdateTime by info.odysseyx@gmail.com August 26, 2024 written by info.odysseyx@gmail.com August 26, 2024 0 comment 14 views 14 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 Installation module: Make sure you have the latest Azure PowerShell modules installed. Connect-Az Account: Authenticates your session with Azure. Search-AzGraph: Query Azure Resource Graph to find disks older than 60 days. 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 Share 0 FacebookTwitterPinterestEmail info.odysseyx@gmail.com previous post Now Available: Microsoft Viva Glint 360 Feedback next post Architecting secure Gen AI applications: Preventing Indirect Prompt Injection Attacks You may also like 7 Disturbing Tech Trends of 2024 December 19, 2024 AI on phones fails to impress Apple, Samsung users: Survey December 18, 2024 Standout technology products of 2024 December 16, 2024 Is Intel Equivalent to Tech Industry 2024 NY Giant? December 12, 2024 Google’s Willow chip marks breakthrough in quantum computing December 11, 2024 Job seekers are targeted in mobile phishing campaigns December 10, 2024 Leave a Comment Cancel Reply Save my name, email, and website in this browser for the next time I comment.