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).  Loo...
  • kyazaferr's avatar
    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.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