刚刚的一篇Blog采用Json Template的方式从已有的VHD创建了一台新的VM。由于Json Template封装的比较好,可以改的内容不多。

下面将介绍通过用Powershell来从已有的VHD创建一台新的VM。

由于Powershell中的各种变量、参数都是可以定义的,所以可以自己去修改、创建。

下面是具体的脚本:

function vm-fromvhd{
param(
#The VM resource group
[Parameter(Mandatory=$true)]
[String]$rgname,
 
#The VM name
[Parameter(Mandatory=$true)]
[String]$vmname,
 
#The High Avalibility Set name
[Parameter(Mandatory=$true)]
[String]$hasetname,
 
#The new VM IP name
[Parameter(Mandatory=$true)]
[String]$vmpipname,
 
#The Vnet Name
[Parameter(Mandatory=$true)]
[String]$vnetname,
 
#The Subnet Name
[Parameter(Mandatory=$true)]
[String]$subnet1,
 
#The new VM size
[Parameter(Mandatory=$true)]
[String]$vmsize,
 
#The existing VHD URL
[Parameter(Mandatory=$true)]
[String]$osDiskURL
)
 
#Get a random text as the random text
$hash = $null
for ($i = 0; $i -le 4; $i++){
$j = (97..122) | Get-Random -Count 1 | % {[char]$_}
$hash = $hash + $j
}
for ($i = 0; $i -le 4; $i++){
$j = (48..57) | Get-Random -Count 1 | % {[char]$_}
$hash = $hash + $j
}
 
#check the Resource Group, if not exist, create
$rgs = Get-AzureRmResourceGroup -Location "China East"
$rgrslt = $false
foreach ($rg in $rgs){if($rg.ResourceGroupName -eq $rgname){$rgrslt = $true;break}}
if(-not $rgrslt) {$rg = New-AzureRmResourceGroup -Name $rgname -Location "China East"}
 
#check the High Avalibility Set, if not exist, create
foreach ($rgh in $rgs){
$haset = Get-AzureRmAvailabilitySet -ResourceGroupName $rgh.ResourceGroupName -Name $hasetname -ErrorAction Ignore;
if($haset.name -match $hasetname){
if($haset.ResourceGroupName -match $rgname){break;}
else{write-host "Please change another haset name";exit;}
}
}
if(-not $haset.Name) {$haset = new-AzureRmAvailabilitySet -ResourceGroupName $rgname -Name $hasetname -Location $rg.Location}
 
#check the Vnet, if not exist, create
$vnets = Get-AzureRmVirtualNetwork
$vnetrslt = $false
foreach ($vnet in $vnets){if($vnet.Name -eq $vnetname){$vnetrslt = $true;break}}
if(-not $vnetrslt) {$subnet1 = New-AzureRmVirtualNetworkSubnetConfig -Name $subnet1 -AddressPrefix 172.16.1.0/24;$vnet = New-AzureRmVirtualNetwork -Name $vnetname -AddressPrefix 172.16.0.0/16 -Subnet $subnet1 -ResourceGroupName $rgname -Location $rg.Location}
 
#check the PIP address, if not exist, create
$piprslt = Test-AzureRmDnsAvailability -DomainNameLabel $vmpipname -Location $rg.location
if(-not $piprslt){$vmpipname = $hash + $vmpipname}
$pip = New-AzureRmPublicIpAddress -Name $vmpipname -AllocationMethod Dynamic -DomainNameLabel $vmpipname -ResourceGroupName $rgname -Location $rg.Location
 
#check the NIC, if not exist, create
$nics = Get-AzureRmNetworkInterface
$nicrslt = $false
foreach($nic in $nics){if($nic.name -eq $vmname){$nicrslt = $true;break}}
if($nicrslt){$nicname = $hash+$vmname}else{$nicname = $vmname}
$nic = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $rgname -Location $rg.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
 
#create the VM
$vmosname = $hash + $vmname + "osDisk"
$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize -AvailabilitySetId $haset.Id
$vm = Add-AzureRmVMNetworkInterface -VM $vm -NetworkInterface $nic
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $vmosname -VhdUri $osDiskURL -CreateOption Attach -Linux
 
New-AzureRmVM -ResourceGroupName $rgname -Location "China East" -VM $vm
 
}
 
$rgname = "vnet-bgp"
$vmname = "hwfromvhd02"
$hasetname = "hwvhdtest" #Please check the haset isn't avalible
$vmpipname = "hwvhdtestpip"
$vnetname = "vnet-bgp-3"
$subnet1 = "vlan1"
$vmsize = "Standard_D1"
$osDiskURL = "https://gwzdiskdisks420.blob.core.chinacloudapi.cn/vhds/hwvntp0120170401203214.vhd"
 
vm-fromvhd -rgname $rgname -vmname $vmname -hasetname $hasetname -vmpipname $vmpipname -vnetname $vnetname -subnet1 $subnet1 -vmsize $vmsize -osDiskURL $osDiskURL
 

Azure上采用Powershell从已有的VHD创建VM的更多相关文章

  1. Azure上采用Json Template从已有的VHD创建VM

    从已有的VHD创建VM是使用Azure中经常要操作的内容. 本文将介绍如何采用Json Template从已经有的VHD创建VM. 一.准备VHD 在我的Azure账户中选择一台VM,如下图: 查看其 ...

  2. 在Azure上通过Powershell创建多Interface的Cisco CSR路由器

    前面通过Json的Template在Azure上创建了Cisco的CSR路由器.但那个Json的template只支持1块网卡.如果需要多网卡的Cisco CSR路由器,可以改上篇文章中提到的Json ...

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

    生成Image映像文件,记录好Image的URL(下面URL为测试URL,具体请参考实际):ImageURL:https://hlmrgstoragen.blob.core.chinacloudapi ...

  4. 用Json Template在Azure上创建Cisco CSR路由器

    Azure的ARM模式可以通过Json的模板创建VM.本文以Cisco的CSR的image为例,介绍如何用Json的创建VM. 一.Cisco CSR的Image 首先把Cisco CSR的image ...

  5. Azure上批量创建OS Disk大于30G的Linux VM

    Azure上VM的OS盘的大小在创建时是固定的.Windows是127G,Linux是30G.如果需要批量创建的VM的OS Disk有更大的容量.可以考虑用下面的方法实现. 1 创建一台有Data-d ...

  6. Azure Powershell blob中指定的vhd创建虚拟机

    #此脚本用于 Azure 存储账户中已有 vhd 镜像文件创建虚拟机,一般用于做好镜像测试 #----------------------------------------------------- ...

  7. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(二)

    前言 (二)建立虚拟网络环境,以及域控和DNS服务器   1搭建虚拟网络环境 在Azure上创建虚拟网络.本例选择的是东南亚数据中心.后面在创建虚机的时候,也选择这个数据中心. VNet Name: ...

  8. 如何在云端部署SAP HANA实战, Azure 上的 SAP HANA(大型实例)概述和体系结构

    什么是 Azure 上的 SAP HANA(大型实例)? Azure 上的 SAP HANA(大型实例)是一种针对 Azure 的独特解决方案. 除了提供 Azure 虚拟机以用于部署和运行 SAP ...

  9. 在 Windows Azure 上设计多租户应用程序

    作者:Suren Machiraju 和 Ralph Squillace 审校:Christian Martinez.James Podgorski.Valery Mizonov 和 Michael ...

随机推荐

  1. linux创建指定大小的文件

    一.生成文件大小和实际占空间大小一样的文件 dd if=/dev/zero of=50M.file bs=1M count=50 dd if=/dev/zero of=20G.file bs=1G c ...

  2. AngularJS 视图和路由

    在AngularJS之后引用angular-route  路由   ngRoute模块加载声明   AngularJS提供的when和otherwise两个方法来定义应用的路由   otherwise ...

  3. uiwebview 加载本地js、css、img,html从网站加载

    资源文件都是放在根目录下 1.index.html <html> <head> <title>My test Page</title> <link ...

  4. POJ 3159 最短路 SPFA

    #include<iostream> using namespace std; const int nMax = 30005; const int mMax = 150005; const ...

  5. myeclipse 10 j安装了JDK1.7,java编译器无法选择到1.7的问题

    java程序编写,在eclipse中会自动编译,编译的版本在preferrence-->java-->compiler选择具体版本,这时你写程序时自动编译用的jdk就是这个版本的jdk,这 ...

  6. java hasmap对象的深复制实现:字节码复制和对象序列化成字符串复制比较。

    /** * Created by Administrator on 2016/11/23. */ public class test { public static void main(String[ ...

  7. Nginad Server安装

    前言 Nginad是一个基于php的开源项目,它既可以作为静态配置的Ad Server,也可以作为动态的RTB Exchange使用.代码结构比较直接明了,挺适合用作学习的.本文如果有理解错误的地方, ...

  8. C#加密解密DES字符串<转>

    using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptograph ...

  9. Myeclipse快捷键(二)

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+↓ ...

  10. php:PHP解析xml的4种方法

    XML处理是开发过程中经常遇到的,PHP对其也有很丰富的支持,本文只是对其中某几种解析技术做简要说明,包括:Xml parser, SimpleXML, XMLReader, DOMDocument. ...