Unlock the Power of Azure Disk Management with PowerShell! 🚀
Unlock the Power of Azure Disk Management with PowerShell! 🚀
Want to master your Azure disks? This PowerShell script lets you seamlessly connect to Azure, retrieve details for a specific disk, and list all disks in a resource group – including their LastOwnershipUpdateTime! 🛠️
🔑 What does the script do?
- Connects to Azure using secure device authentication.
- Fetches details for a specific disk, including ownership update time.
- Uses the Azure REST API to dig deeper into disk properties.
- Lists all disks in a resource group in a clean table format.
💻 Try it now! Copy the code below and take control of your Azure resources. Just replace $resourceGroupName
and $diskName
with your own values. Got questions? Drop a comment! 👇
PowerShell Script
# Ensure you're connected to Azure
Connect-AzAccount -UseDeviceAuthentication
# Set resource group
$resourceGroupName = "YourResourceGroupName"
$diskName = "YourDiskName"
try {
$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName $diskName -ErrorAction Stop
if ($disk.LastOwnershipUpdateTime) {
Write-Output "Disk: $($disk.Name), LastOwnershipUpdateTime: $($disk.LastOwnershipUpdateTime)"
} else {
Write-Output "LastOwnershipUpdateTime is not directly available for $($disk.Name)."
}
$detailedDisk = Invoke-AzRestMethod -Path "$($disk.Id)?api-version=2023-10-02" -Method GET
$detailedProperties = $detailedDisk.Content | ConvertFrom-Json
$lastOwnershipUpdateTime = $detailedProperties.properties.lastOwnershipUpdateTime
Write-Output "Disk: $($disk.Name), LastOwnershipUpdateTime (via API): $lastOwnershipUpdateTime"
}
catch {
Write-Error "Error fetching disk '$diskName' in resource group '$resourceGroupName': $_"
}
try {
$disks = Get-AzDisk -ResourceGroupName $resourceGroupName -ErrorAction Stop
$results = $disks | ForEach-Object {
$detailedDisk = Invoke-AzRestMethod -Path "$($_.Id)?api-version=2023-10-02" -Method GET
$detailedProperties = $detailedDisk.Content | ConvertFrom-Json
$lastOwnershipUpdateTime = $detailedProperties.properties.lastOwnershipUpdateTime
[PSCustomObject]@{
DiskName = $_.Name
LastOwnershipUpdateTime = $lastOwnershipUpdateTime
}
}
$results | Format-Table -AutoSize
}
catch {
Write-Error "Error listing disks in resource group '$resourceGroupName': $_"
}
Kommentarer
Skicka en kommentar