【问题标题】:What is the equivalent script in powershell for image-copy-extension extension from Azure CLIAzure CLI 中用于 image-copy-extension 扩展的 powershell 中的等效脚本是什么
【发布时间】:2018-10-05 13:41:42
【问题描述】:

我需要使用 powershell 从位于 locationB 的图像 ImageBlocationA 中创建一个 VM。由于我无法做到这一点,我正在尝试将ImageB 的副本创建到locationA 中,这样我就可以轻松创建VM。

我搜索了复制图像的脚本并得到了很多结果。我发现了一个特别的link,它有简单的脚本来复制图像。但它是 AzureCLI cmdlet。我需要 powershell 脚本来完成这项工作,因为我使用的工具只理解 powershell 脚本。

我已经搜索了一个等效的 powershell 脚本,但没有找到。任何人都可以在这里帮助我。

【问题讨论】:

    标签: azure powershell azure-cli


    【解决方案1】:

    这是一个复制图像的脚本。您可以创建快照并将其复制到另一个区域,然后创建映像。

    创建快照:

    <# -- 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
    

    更多详情,请参考此link

    【讨论】:

    • 谢谢您的回复。在这一步 --> # 在其他区域设置目标存储帐户,在 $storageAccountName 中,我应该提供源区域或目标区域的存储帐户名称吗?
    • @CrazyCoder 目标区域。
    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 2023-03-30
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多