Forum Discussion

Oleg_A's avatar
Oleg_A
Copper Contributor
Dec 13, 2024
Solved

Azure PowerShell find LastOwnershipUpdateTime on disk

Hello:

I wondering if it's possible to find LastOwnershipUpdateTime on the disk via PowerShell.

I can see this info in the portal, but cannot figure out how to find it via script (PowerShell).  Looks like MSFT recently released it, but even updating my Az.Compute module to the latest (9.0.0) version I still do not see it. 

 

Any help would be really appreciated. 

Thank you! 

  • # 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.LastOwnershipUpdateTime

    If 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.LastOwnershipUpdateTime

    If 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

     

  • Oleg_A's avatar
    Oleg_A
    Copper Contributor

    kyazaferr 

     

    Thank you very much for your help!!! 

    $disk.LastOwnershipUpdateTime did not work for me, but the code below worked great.

     

    # 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

     

    I just had to change $detailedProperties.LastOwnershipUpdateTime to 

    $detailedProperties.properties.LastOwnershipUpdateTime, but it was easy. 

     

    Thank you very much for your help!!!

  • kyazaferr's avatar
    kyazaferr
    Steel Contributor

    # 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.LastOwnershipUpdateTime

    If 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.LastOwnershipUpdateTime

    If 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

Resources