azure - Azure CLI 中用于 image-copy-extension 扩展的 powershell 中的等效脚本是什么
问题描述
我需要从使用 powershelllocationA
的 ImageImageB
中创建一个 VM。locationB
由于我无法做到这一点,我正在尝试创建ImageB
into的副本locationA
,以便我可以轻松创建 VM。
我搜索了复制图像的脚本并得到了很多结果。我发现了一个特定的链接,它有简单的脚本来复制图像。但它是 AzureCLI cmdlet。我需要 powershell 脚本来完成这项工作,因为我使用的工具只理解 powershell 脚本。
我已经搜索了一个等效的 powershell 脚本,但找不到任何东西。任何人都可以在这里帮助我。
解决方案
这是复制图像的脚本。您可以创建快照并将其复制到另一个区域,然后创建映像。
创建快照:
<# -- Create a snapshot of the OS (and optionally data disks) from the generalized VM -- #>
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName
$disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName $vm.StorageProfile.OsDisk.Name
$snapshot = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $region
$snapshotName = $imageName + "-" + $region + "-snap"
New-AzureRmSnapshot -ResourceGroupName $resourceGroupName -Snapshot $snapshot -SnapshotName $snapshotName
复制快照:
# Create the name of the snapshot, using the current region in the name.
$snapshotName = $imageName + "-" + $region + "-snap"
# Get the source snapshot
$snap = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
# Create a Shared Access Signature (SAS) for the source snapshot
$snapSasUrl = Grant-AzureRmSnapshotAccess -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName -DurationInSecond 3600 -Access Read
# Set up the target storage account in the other region
$targetStorageContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
New-AzureStorageContainer -Name $imageContainerName -Context $targetStorageContext -Permission Container
# Use the SAS URL to copy the blob to the target storage account (and thus region)
Start-AzureStorageBlobCopy -AbsoluteUri $snapSasUrl.AccessSAS -DestContainer $imageContainerName -DestContext $targetStorageContext -DestBlob $imageBlobName
Get-AzureStorageBlobCopyState -Container $imageContainerName -Blob $imageBlobName -Context $targetStorageContext -WaitForComplete
# Get the full URI to the blob
$osDiskVhdUri = ($targetStorageContext.BlobEndPoint + $imageContainerName + "/" + $imageBlobName)
# Build up the snapshot configuration, using the target storage account's resource ID
$snapshotConfig = New-AzureRmSnapshotConfig -AccountType StandardLRS `
-OsType Windows `
-Location $targetRegionName `
-CreateOption Import `
-SourceUri $osDiskVhdUri `
-StorageAccountId "/subscriptions/${sourceSubscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Storage/storageAccounts/${storageAccountName}"
# Create the new snapshot in the target region
$snapshotName = $imageName + "-" + $targetRegionName + "-snap"
$snap2 = New-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName -Snapshot $snapshotConfig
创建图像:
<# -- In the second subscription, create a new Image from the copied snapshot --#>
Select-AzureRmSubscription -SubscriptionId $targetSubscriptionId
$snap = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
$imageConfig = New-AzureRmImageConfig -Location $destinationRegion
Set-AzureRmImageOsDisk -Image $imageConfig `
-OsType Windows `
-OsState Generalized `
-SnapshotId $snap.Id
New-AzureRmImage -ResourceGroupName $resourceGroupName `
-ImageName $imageName `
-Image $imageConfig
有关更多详细信息,请参阅此链接。
推荐阅读
- mysql - 将结果加入新表?
- python - Python:如何获取每个项目在多个列表中的位置的统计信息?
- javascript - Promise.all 返回双精度数组
- java - 从树构建 JPA 规范
- azure-cosmosdb - 如何在折叠 gremlin 之后使用以前存储的项目
- javascript - 在 vue 的不同位置渲染两个单选按钮
- python - 基本 HTTPServer 挂在“socket.readinto : return self._sock.recv_into(b)”
- reactjs - 在反应组件中上传图像后无法显示图像
- python - Pandas Dataframe:将混合浮点字符串列拆分为单独的浮点和字符串列
- node.js - 在 npm 脚本中使用子依赖项