Azure Powershell使用已有Image创建ARM非托管磁盘虚拟机

时间:2023-03-08 19:12:04

生成Image映像文件,记录好Image的URL(下面URL为测试URL,具体请参考实际):
ImageURL:
https://hlmrgstoragen.blob.core.chinacloudapi.cn/vhdscopy/hlmn4sysclocap-os-2017-09-21-2CE40CEE.vhd

安装和配置Azure Powershell的步骤请参考对应的安装文档。安装配置完成后,使用下面的命令进行创建(#为注释,忽略对应的说明):

#指定虚拟机名称

$vmname = "hlmsysr2n5"

#指定资源组

$resourceGroup = "hlmrgn"

#指定区域位置

$location = "China North"

#指定虚拟网络

$vnetname = "hlmrgvnetn"

#指定子网

$subnetname = "default"

#指定公网IP名称

$publicipname = $vmname+"pip"

#指定nsg名称

$nsgname = $vmname+"-nsg"

#指定网卡名称

$nicname = $vmname+"nic"

#指定虚拟机磁盘名称

$osdiskname = $vmname + "-osdisk"

#指定OS磁盘缓存类型

$osdiskcaching = "ReadWrite"

#指定公网IP分配方式

$publicipallocationmethod = "Dynamic"

#指定创建机器的image文件位置

$sourceimageurl = "https://hlmrgstoragen.blob.core.chinacloudapi.cn/vhdscopy/hlmn4sysclocap-os-2017-09-21-2CE40CEE.vhd"

#指定新建机器系统盘存放位置

$osdiskurl = "https://hlmrgstoragen.blob.core.chinacloudapi.cn/vhds/"+$osdiskname+".vhd"

#查看Subnet

$vnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $resourceGroup

$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetname -VirtualNetwork $vnet

#创建一个公网IP地址

$publicIP = New-AzureRmPublicIpAddress -Name $publicipname -ResourceGroupName $resourceGroup -Location $location -AllocationMethod $publicipallocationmethod -IpAddressVersion IPv4 –Force

#创建nsg安全规则

$nsgrule = New-AzureRmNetworkSecurityRuleConfig -Name "default-allow-rdp" -Description "Allow RDP" -Access Allow -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389

$nsg = New-AzureRmNetworkSecurityGroup -Name $nsgname -ResourceGroupName $resourceGroup -Location $location -SecurityRules $nsgrule

#创建网卡

$nic = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $resourceGroup -Location $location -SubnetId $subnet.Id -PublicIpAddressId $publicIP.Id -NetworkSecurityGroupId $nsg.Id

#设置用户名及密码

$credential = Get-Credential -Message "Type the name and password of the local administrator account."

#生成虚拟机的配置

$vmconfig = New-AzureRmVMConfig -VMName $vmname -VMSize Standard_A2 | Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmname -Credential $credential -ProvisionVMAgent -EnableAutoUpdate | Set-AzureRmVMOSDisk -Name $osdiskname -VhdUri $osdiskurl -SourceImageUri $sourceimageurl -Caching $osdiskcaching -CreateOption FromImage -Windows | Add-AzureRmVMNetworkInterface -Id $nic.Id -Primary

#创建虚拟机

New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmconfig

所有的脚本命令如下:

$vmname = "hlmsysr2n5"
$resourceGroup = "hlmrgn"
$location = "China North"
$vnetname = "hlmrgvnetn"
$subnetname = "default"
$publicipname = $vmname+"pip"
$nsgname = $vmname+"-nsg"
$nicname = $vmname+"nic"
$osdiskname = $vmname + "-osdisk"
$osdiskcaching = "ReadWrite"
$publicipallocationmethod = "Dynamic"
$sourceimageurl = "https://hlmrgstoragen.blob.core.chinacloudapi.cn/vhdscopy/hlmn4sysclocap-os-2017-09-21-2CE40CEE.vhd"
$osdiskurl = "https://hlmrgstoragen.blob.core.chinacloudapi.cn/vhds/"+$osdiskname+".vhd"
$vnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $resourceGroup
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetname -VirtualNetwork $vnet
$publicIP = New-AzureRmPublicIpAddress -Name $publicipname -ResourceGroupName $resourceGroup -Location $location -AllocationMethod $publicipallocationmethod -IpAddressVersion IPv4 –Force
$nsgrule = New-AzureRmNetworkSecurityRuleConfig -Name "default-allow-rdp" -Description "Allow RDP" -Access Allow -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$nsg = New-AzureRmNetworkSecurityGroup -Name $nsgname -ResourceGroupName $resourceGroup -Location $location -SecurityRules $nsgrule
$nic = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $resourceGroup -Location $location -SubnetId $subnet.Id -PublicIpAddressId $publicIP.Id -NetworkSecurityGroupId $nsg.Id
$credential = Get-Credential -Message "Type the name and password of the local administrator account."
$vmconfig = New-AzureRmVMConfig -VMName $vmname -VMSize Standard_A2 | Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmname -Credential $credential -ProvisionVMAgent -EnableAutoUpdate | Set-AzureRmVMOSDisk -Name $osdiskname -VhdUri $osdiskurl -SourceImageUri $sourceimageurl -Caching $osdiskcaching -CreateOption FromImage -Windows | Add-AzureRmVMNetworkInterface -Id $nic.Id -Primary
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmconfig

命令执行成功截图:

Azure Powershell使用已有Image创建ARM非托管磁盘虚拟机

创建完的虚拟机可以成功连接使用:

Azure Powershell使用已有Image创建ARM非托管磁盘虚拟机

备注:

Linux虚拟机需要修改如下三点:

1.nsg规则对应的名称及端口

2.命令:Set-AzureRmVMOperatingSystem,的参数

3.命令:Set-AzureRmVMOSDisk,的参数

提供一个创建Linux机器的脚本命令:

$vmname = "hlmsys69n4"
$resourceGroup = "hlmrgn"
$location = "China North"
$vnetname = "hlmrgvnetn"
$subnetname = "default"
$publicipname = $vmname+"pip"
$nsgname = $vmname+"-nsg"
$nicname = $vmname+"nic"
$osdiskname = $vmname + "-osdisk"
$osdiskcaching = "ReadWrite"
$publicipallocationmethod = "Dynamic"
$sourceimageurl = "https://hlmrgstoragen.blob.core.chinacloudapi.cn/vhdscopy/hlm69n4wadclocap-os-2017-09-25-723993A1.vhd"
$osdiskurl = "https://hlmrgstoragen.blob.core.chinacloudapi.cn/vhds/"+$osdiskname+".vhd"
$vnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $resourceGroup
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetname -VirtualNetwork $vnet
$publicIP = New-AzureRmPublicIpAddress -Name $publicipname -ResourceGroupName $resourceGroup -Location $location -AllocationMethod $publicipallocationmethod -IpAddressVersion IPv4 –Force
$nsgrule = New-AzureRmNetworkSecurityRuleConfig -Name "default-allow-ssh" -Description "Allow SSH" -Access Allow -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22
$nsg = New-AzureRmNetworkSecurityGroup -Name $nsgname -ResourceGroupName $resourceGroup -Location $location -SecurityRules $nsgrule
$nic = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $resourceGroup -Location $location -SubnetId $subnet.Id -PublicIpAddressId $publicIP.Id -NetworkSecurityGroupId $nsg.Id
$credential = Get-Credential -Message "Type the name and password of the local administrator account."
$vmconfig = New-AzureRmVMConfig -VMName $vmname -VMSize Standard_A2 | Set-AzureRmVMOperatingSystem -Linux -ComputerName $vmname -Credential $credential | Set-AzureRmVMOSDisk -Name $osdiskname -VhdUri $osdiskurl -SourceImageUri $sourceimageurl -Caching $osdiskcaching -CreateOption FromImage -Linux | Add-AzureRmVMNetworkInterface -Id $nic.Id -Primary
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmconfig

成功执行截图:

Azure Powershell使用已有Image创建ARM非托管磁盘虚拟机

创建完的虚拟机可以成功连接使用:

Azure Powershell使用已有Image创建ARM非托管磁盘虚拟机

备注:

使用Image创建虚拟机时,Image可以重复使用。