使用存储在Blob上的已创建(通用/ sysprep' ed)快照在Azure上部署VM

时间:2018-03-16 12:22:38

标签: azure azure-storage azure-disk

我需要更多有经验的人在Azure上使用VM。我已经使用Win2016 Server准备了图像,并且几乎没有其他配置。之后我完成了sysprep并创建了图像和快照。当我使用此图像创建新计算机时,计算机将失败并且不会启动事件。所以我找到了另一个解决方案:从这台机器上获取快照并转移到blob。在下面的代码执行开始之后的新机器,但我无法连接RDP ......

错误:VM部署时出现错误:New-AzureRmVM:长时间运行操作失败,状态为'失败'

$resourceGroupName="<resource-group>"
$subscriptionId = "<subs>"
$tenantId = "<tenant>"
$location = "West Europe"

# sign in
Write-Host "Logging in...";
Login-AzureRmAccount -TenantID $tenantID;

# select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzureRmSubscription -SubscriptionID $subscriptionId;


# Create a Resource Group 
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location


#Prepare the VM parameters 
$rgName = $resourceGroupName
# Get the Azure location and storage account names
$locName=(Get-AzureRmResourceGroup -Name $rgName).Location

$exSubnet=New-AzureRMVirtualNetworkSubnetConfig -Name EX2016Subnet -AddressPrefix 10.0.0.0/24
New-AzureRMVirtualNetwork -Name EX2016Vnet -ResourceGroupName $rgName -Location $locName -AddressPrefix 10.0.0.0/16 -Subnet $exSubnet -DNSServer 10.0.0.5
$rule1 = New-AzureRMNetworkSecurityRuleConfig -Name "RDPTraffic" -Description "Allow RDP to all VMs on the subnet" -Access Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$rule2 = New-AzureRMNetworkSecurityRuleConfig -Name "ExchangeSecureWebTraffic" -Description "Allow HTTPS to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 101 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 443
$rule3 = New-AzureRMNetworkSecurityRuleConfig -Name "ExchangeSecurePortExternal" -Description "Allow access port 25 to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 102 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 25
New-AzureRMNetworkSecurityGroup -Name EX2016Subnet -ResourceGroupName $rgName -Location $locName -SecurityRules $rule1, $rule2,$rule3
$vnet=Get-AzureRMVirtualNetwork -ResourceGroupName $rgName -Name EX2016Vnet
$nsg=Get-AzureRMNetworkSecurityGroup -Name EX2016Subnet -ResourceGroupName $rgName
Set-AzureRMVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name EX2016Subnet -AddressPrefix "10.0.0.0/24" -NetworkSecurityGroup $nsg

# Specify the virtual machine name and size
$vmName="exVM"
$vmSize="Standard_D3_v2"
$vnet=Get-AzureRMVirtualNetwork -Name "EX2016Vnet" -ResourceGroupName $rgName
$vm=New-AzureRMVMConfig -VMName $vmName -VMSize $vmSize

# Create the NIC for the virtual machine
$nicName=$vmName + "-NIC"
$pipName=$vmName + "-PublicIP"
$pip=New-AzureRMPublicIpAddress -Name $pipName -ResourceGroupName $rgName -DomainNameLabel $vmDNSName -Location $locName -AllocationMethod Dynamic
$nic=New-AzureRMNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -PrivateIpAddress "10.0.0.5"

# Specify the image and local administrator account, and then add the NIC
$cred=Get-Credential -Message "Type the name and password of the local administrator account for exVM."


$osDiskName = "VM01-OSDisk"
$osDiskUri = "https://xxxxxx.blob.core.windows.net/images/Image.vhd"

$storageAccountType = "StandardLRS"
$vm=Add-AzureRMVMNetworkInterface -VM $vm -Id $nic.Id

$osDisk = New-AzureRmDisk -DiskName $osDiskName -Disk (New-AzureRmDiskConfig -AccountType $storageAccountType -Location $location -CreateOption Import -SourceUri $osDiskUri) -ResourceGroupName $rgName
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType $storageAccountType -DiskSizeInGB 128 -CreateOption Attach -Windows

$vm = Set-AzureRmVMBootDiagnostics -VM $vm -disable
#Create the new VM
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm

1 个答案:

答案 0 :(得分:1)

您可以使用blob创建快照并使用快照创建新VM,您可以查看example

isomorphic-fetch

您还可以检查此link以从blob创建快照。