【问题标题】:How to Create a Azure managed Disk using snapshot from Powershell如何使用 Powershell 中的快照创建 Azure 托管磁盘
【发布时间】:2021-11-14 06:17:20
【问题描述】:

如何使用 PowerShell 中的快照创建 Azure 托管磁盘?

我可以使用 Azure 门户中的快照创建托管磁盘,但是如何使用 PowerShell 来创建托管磁盘?

【问题讨论】:

    标签: azure powershell azure-storage


    【解决方案1】:

    您可以使用以下脚本在您需要的区域创建托管磁盘的快照。在下面的脚本中,您可以根据需要输入订阅 ID、资源组名称和其他组件的详细信息,您应该能够为它们创建快照

    #Provide the subscription Id
    $subscriptionId = 'yourSubscriptionId'
    
    #Provide the name of your resource group
    $resourceGroupName ='yourResourceGroupName'
    
    #Provide the name of the snapshot that will be used to create Managed Disks
    $snapshotName = 'yourSnapshotName'
    
    #Provide the name of the Managed Disk
    $diskName = 'yourManagedDiskName'
    
    #Provide the size of the disks in GB. It should be greater than the VHD file size.
    $diskSize = '128'
    
    #Provide the storage type for Managed Disk. PremiumLRS or StandardLRS.
    $storageType = 'Premium_LRS'
    
    #Provide the Azure region (e.g. westus) where Managed Disks will be located.
    #This location should be same as the snapshot location
    #Get all the Azure location using command below:
    #Get-AzLocation
    $location = 'westus'
    
    #Set the context to the subscription Id where Managed Disk will be created
    Select-AzSubscription -SubscriptionId $SubscriptionId
    
    $snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName 
    
    $diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id -DiskSizeGB $diskSize
     
    New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName
    

    来源here。

    您也可以使用here 中提到的 powershell 命令:

    1)设置所需参数。更新值以反映您的环境。

    $resourceGroupName = 'myResourceGroup' 
    $location = 'eastus' 
    $vmName = 'myVM'
    $snapshotName = 'mySnapshot'
    

    2)使用 Get-AzVM cmdlet 获取包含要复制的 VHD 的 VM。

    $vm = Get-AzVM `
        -ResourceGroupName $resourceGroupName `
        -Name $vmName
    

    3)创建快照配置

    $snapshot =  New-AzSnapshotConfig `
        -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id `
        -Location $location `
        -CreateOption copy
    

    4)拍摄快照:

    New-AzSnapshot `
        -Snapshot $snapshot `
        -SnapshotName $snapshotName `
        -ResourceGroupName $resourceGroupName
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
    猜你喜欢
    • 2017-07-08
    • 2018-05-25
    • 1970-01-01
    • 2019-02-02
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多