首页 > 解决方案 > Azure PowerShell Runbook Get Set Az-Disk dynamically

问题描述

The team is trying to automate a snapshot restore, which was achieved successfully. However I am not able to figure out how to dynamically get the previous disk within the resource group; As well as set the next disk with a new name.

In the code below "AZR-001_OsDisk_7" has to set dynamically to "AZR-001_OsDisk_8" the next time it runs:

$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName "ETD-RFT" -DiskName "AZR-001_OsDisk_7"
$disk1 = Get-AzDisk -ResourceGroupName "ETD-RFT" -Name "AZR-001_OsDisk_7" 

标签: azurepowershellazure-runbook

解决方案


Not a final solution, but I have a quick idea. You may use Get-AzDisk -ResourceGroupName 'ResourceGroupName ' to get all the disks. And then you can get the disk name.

As you named the disk with appropriate rule, you may split the name string by _, then you will get a string array which contains all the parts. In this way, you will be able to get the version.

A sample:

$disks = Get-AzDisk -ResourceGroupName JackVS2019

foreach($disk in $disks){
    $arr = $disk.Name.Split('_')
    Write-Host $arr[2]
}

The output:

1

Then you can generate the new disk name.


推荐阅读