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


    生成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

    命令执行成功截图:

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

     

    备注:

    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

    成功执行截图:

     

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

     

    备注:

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

  • 相关阅读:
    [Bullet3]创建世界(场景)及常见函数
    [erlang]supervisor(监控树)的重启策略
    [game]十字链表的AOI算法实现
    [翻译][erlang]cowboy handler模块的使用
    数据挖掘算法系列目录
    Spark原理分析目录
    Spark实战系列目录
    2019年读书书单
    Hadoop源码解读系列目录
    分布式架构系列目录
  • 原文地址:https://www.cnblogs.com/stonehe/p/7594495.html
Copyright © 2020-2023  润新知