Forum Discussion
Azure PowerShell find LastOwnershipUpdateTime on disk
- Dec 17, 2024
# Ensure you're connected to Azure
Connect-AzAccount# Get the disk details with extended properties
$disk = Get-AzDisk -ResourceGroupName "YourResourceGroupName" -DiskName "YourDiskName"# Check the LastOwnershipUpdateTime property
$disk.LastOwnershipUpdateTimeIf the property isn't directly accessible, you can try using the underlying REST API call:
# Get the disk resource
$disk = Get-AzDisk -ResourceGroupName "YourResourceGroupName" -DiskName "YourDiskName"# Use the full resource ID to fetch detailed properties
$detailedDisk = Invoke-AzRestMethod -Path "$($disk.Id)?api-version=2023-10-02" -Method GET# Parse the response to extract LastOwnershipUpdateTime
$detailedProperties = $detailedDisk.Content | ConvertFrom-Json
$lastOwnershipUpdateTime = $detailedProperties.LastOwnershipUpdateTimeIf you want to get this for multiple disks:
# List all disks in a resource group
$disks = Get-AzDisk -ResourceGroupName "YourResourceGroupName"# Filter and display LastOwnershipUpdateTime
$disks | ForEach-Object {
[PSCustomObject]@{
DiskName = $_.Name
LastOwnershipUpdateTime = $_.LastOwnershipUpdateTime
}
}A few important notes:
- Ensure you're using the latest Az.Compute module (version 9.0.0 or higher)
- Make sure you're authenticated with Connect-AzAccount
- Replace "YourResourceGroupName" and "YourDiskName" with your actual resource group and disk names
- The property might not be available on older disks or in all Azure regions
Try this:
# Uninstall 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 -Force
# Verify the installation
Get-Module -ListAvailable -Name Az* | Select-Object Name, Version
Connect-AzAccount
$disks = Search-AzGraph -Query 'resources
| where type == "microsoft.compute/disks"
| project name, lastUpdateTime = properties.LastOwnershipUpdateTime'
$disks | Format-Table -Property name, lastUpdateTime