PowerShell でマネージド ディスクを同じサブスクリプションまたは別のサブスクリプションにコピーする

このスクリプトは、既存のマネージド ディスクのコピーを同じサブスクリプションまたは別のサブスクリプションに作成します。 新しいディスクは、親のマネージド ディスクと同じリージョンに作成されます。

必要に応じて、Azure PowerShell ガイドの手順に従って Azure PowerShell モジュールをインストールし、Connect-AzAccount を実行して、Azure との接続を作成します。 また、id_rsa.pub という名前の SSH 公開キーがユーザー プロファイルの .ssh ディレクトリに必要です。

Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

サンプル スクリプト

#Provide the subscription Id of the subscription where managed disk exists
$sourceSubscriptionId='yourSourceSubscriptionId'

#Provide the name of your resource group where managed disk exists
$sourceResourceGroupName='mySourceResourceGroupName'

#Provide the name of the managed disk
$managedDiskName='myDiskName'

#Set the context to the subscription Id where Managed Disk exists
Select-AzSubscription -SubscriptionId $sourceSubscriptionId

#Get the source managed disk
$managedDisk= Get-AzDisk -ResourceGroupName $sourceResourceGroupName -DiskName $managedDiskName

#Provide the subscription Id of the subscription where managed disk will be copied to
#If managed disk is copied to the same subscription then you can skip this step
$targetSubscriptionId='yourTargetSubscriptionId'

#Name of the resource group where snapshot will be copied to
$targetResourceGroupName='myTargetResourceGroupName'

#Set the context to the subscription Id where managed disk will be copied to
#If snapshot is copied to the same subscription then you can skip this step
Select-AzSubscription -SubscriptionId $targetSubscriptionId

$diskConfig = New-AzDiskConfig -SourceResourceId $managedDisk.Id -Location $managedDisk.Location -CreateOption Copy 

#Create a new managed disk in the target subscription and resource group
New-AzDisk -Disk $diskConfig -DiskName $managedDiskName -ResourceGroupName $targetResourceGroupName

スクリプトの説明

このスクリプトでは、コピー元のマネージド ディスクの ID を使ってコピー先のサブスクリプションに新しいマネージド ディスクを作成します。そのために、以下のコマンドが使われています。 表内の各コマンドは、それぞれのドキュメントにリンクされています。

コマンド メモ
New-AzDiskConfig ディスクの作成に使用するディスク構成を作成します。 この構成には、親ディスクのリソース ID および親ディスクと同じ場所が含まれます。
New-AzDisk パラメーターとして渡されたディスク構成、ディスク名、リソース グループ名を使ってディスクを作成します。

次のステップ

マネージド ディスクから仮想マシンを作成する

Azure PowerShell モジュールの詳細については、Azure PowerShell のドキュメントを参照してください。

その他の仮想マシン用の PowerShell サンプル スクリプトは、Azure Windows VM のドキュメントにあります。