Azure上采用Powershell从已有的VHD创建VM

刚刚的一篇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 
 
原文地址:https://www.cnblogs.com/hengwei/p/6700689.html