如何将 Azure 中的自定义图像从一个订阅复制到另一个订阅

How to copy custom images in Azure from one subscription to another

我是 运行 我在 Azure 上的虚拟机,它由多个目录组成,每个目录都有两到三个订阅。我在我的默认订阅下为我的基础设施环境制作了一个自定义图像(比如 "Visual Studio Enterprise: BizSpark")。

我想将此自定义图像复制到不同目录下的其他订阅,以便我的整个 Azure 帐户都可以轻松访问此图像。

我正在关注这个 link : Copy Managed Images by Michael S. Collier 但我卡在了这个片段:

diskName=$(az vm show --resource-group linux -n vm --query "storageProfile.osDisk.name" -o tsv)

错误:

The Resource 'Microsoft.Compute/virtualMachines/vm' under resource group 'linux' was not found.

如有任何线索,我们将不胜感激。

第一个命令只检索磁盘名称,因此如果您知道磁盘名称,则不必 运行。只是做:

diskName="diskname_goes_here"

对于托管磁盘或托管映像,您不能直接将其从一个订阅移动到另一个存在于不同租户中的订阅。该错误可能表明您已连接到其他租户。

如果原来的托管盘存在,可以参考这篇博文move Azure Managed Disk between Tenants

You can create a snapshot of the managed disk and move it to a storage account in the source subscription, then copy the snapshot to a storage account in the destination subscription and create a managed disk out of the snapshot and a VM with the managed disk afterward. Here are the main steps in PowerShell.

A:将磁盘快照下载到存储帐户

$sourceSubscriptionId = ''
$sourceStorageAccountName = "SourceStorageAccount"
$sourceStorageAccountKey = "9O1...Kg=="
$sourceStorageAccountContainer = "containername"
# path of the download URL of the snapshot
$VHDDownloadUri = "https://....blob.core.windows.net/..."
$targetSnapshotName = "snapshot.vhd"
#download snapshot to StorageAccount-Source (the storage account is located in the source subscription)
Select-AzureRmSubscription -SubscriptionId $sourceSubscriptionId
$sourceStorageAccountContext = New-AzureStorageContext –StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceStorageAccountKey
Start-AzureStorageBlobCopy -AbsoluteUri $VHDDownloadUri -DestContainer $sourceStorageAccountContainer -DestContext $sourceStorageAccountContext -DestBlob $targetSnapshotName

B:将快照复制到其他租户的目标订阅中的存储帐户:

$destSubscriptionId = ''
$destStorageAccount = "DestStorageAccount"
$destStorageAccountKey = "Pqn.../Q=="
$destStorageAccountContainer = "container"
Select-AzureRmSubscription -SubscriptionId $destSubscriptionId
$destStorageAccountContext = New-AzureStorageContext –StorageAccountName $destStorageAccount -StorageAccountKey $destStorageAccountKey
Get-AzureStorageBlobCopyState -Context $destStorageAccountContext -Blob $targetSnapshotName
$blobCopy = Start-AzureStorageBlobCopy -DestContainer $destStorageAccountContainer -DestContext $destStorageAccountContext -SrcBlob $targetSnapshotName -Context $sourceStorageAccountContext -SrcContainer $sourceStorageAccountContainer
Write-Host ($blobCopy | Get-AzureStorageBlobCopyState).CopyId
Write-Host ($blobCopy | Get-AzureStorageBlobCopyState).TotalBytes
Write-Host ($blobCopy | Get-AzureStorageBlobCopyState).BytesCopied
while(($blobCopy | Get-AzureStorageBlobCopyState).Status -eq "Pending")
{
    Start-Sleep -s 5
    #$blobCopy | Get-AzureStorageBlobCopyState
    $output = "`r" + ($blobCopy | Get-AzureStorageBlobCopyState).BytesCopied
    Write-Host $output -NoNewline
}
The copy process runs asynchronous. If you need to stop the copy process, get the CopyId and use the Stop-AzureStorageBlogCopy command: Stop-AzureStorageBlobCopy -Container $destStorageAccountContainer -Blob $targetSnapshotName -CopyId "<GUID>" -Context $destStorageAccountContext

C:创建新 VM 并使用 DestStorageAccount 中的 snapshot.vhd 作为托管磁盘的基础映像:

$rgName = "DestResourceGroup"
$location = "northeurope"
$storageName = "MyVMstorage"
$storageType = "Standard_LRS"
$nicname = "MyVM-nic"
$subnet1Name = "MyVM-subnet"
$vnetName = "MyVM-vnet"
$vnetAddressPrefix = "10.0.0.0/16"
$vnetSubnetAddressPrefix = "10.0.0.0/24"
$vmName = "MyVM"
$vmSize = "Standard_D2s_v3"
$osDiskName = $vmName + "osDisk"
$osDiskUri = "https://deststorageaccount.blob.core.windows.net/container/snapshot.vhd"
$storageacc = New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $storageName -Type $storageType -Location $location
$pip = New-AzureRmPublicIpAddress -Name $nicname -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
$subnetconfig = New-AzureRmVirtualNetworkSubnetConfig -Name $subnet1Name -AddressPrefix $vnetSubnetAddressPrefix
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location -AddressPrefix $vnetAddressPrefix -Subnet $subnetconfig
$nic = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$discStorageAcc = Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroup -Name $destStorageAccount
$diskConfig = New-AzureRmDiskConfig -AccountType 'PremiumLRS' -Location $location -CreateOption Import -StorageAccountId ($discStorageAcc.Id) -SourceUri $osDiskUri
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $rgName -DiskName "managedsnapshot"
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $disk.Id -CreateOption Attach -Windows
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm -Verbose