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

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

#
-----------------------------------------------------------------Blob中指定vhd创建虚拟机---------------------------------------- #vhd所在blob存储的信息 $destStorageAccountName ="ygwhxstg" #存储账户名称 $destContainerName ="test0115" #VHD所在容器名称 $destStorageAccountKey ="{key}" #key $fileName="redhat-resourceImage.vhd" #容器中vhd名称 -xxxxxx.vhd #资源组名称 $resourceGroupName="whxrdtest1" #虚拟机信息 $vmname = "retest1" #虚拟机名称 小写 $vmUser="azureuser"#虚拟机用户名 $vmSize="Standard_A4_v2"#虚拟机大小 $osType=1 #0-windows 1-Linux #订阅登录信息 $subscLogin="{登录名}" $subscPwd="{密码}" $securePassword = ConvertTo-SecureString $subscPwd -AsPlainText -Force #此密码也将作为虚拟机登录密码 $credlogin = New-Object System.Management.Automation.PSCredential ($subscLogin, $securePassword) $endpoint="core.chinacloudapi.cn" $fileAbsoluteUri="https://"+$sourceStorageAccountName+".blob."+$endpoint+"/"+$sourceContainerName+"/"+$fileName $locationStr="China North" $osdiskurl = "https://"+$destStorageAccountName+".blob."+$endpoint+"/"+$destContainerName+"/" #登录并创建指定资源组 Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $credlogin
 #选择要使用的订阅
 Get-AzureRmSubscription | select Name, SubscriptionId
 $subscriptionId=Read-Host "请选择上面列出的订阅ID"
 Write-Host "Selecting subscription '$subscriptionId'";
 Select-AzureRmSubscription -SubscriptionID $subscriptionId;
New-AzureRmResourceGroup -Name $resourceGroupName -Location $locationStr -Force

# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Location $locationStr -Name MyVNet -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig -Force

#创建一个公网IP地址:
$publicIP=New-AzureRmPublicIpAddress -Name createstfromPIP -ResourceGroupName $resourceGroupName -Location $locationStr -AllocationMethod Dynamic -IpAddressVersion IPv4 -Force  #新建
Get-AzureRmPublicIpAddress -Name createstfromPIP -ResourceGroupName $resourceGroupName #查看刚刚创建的公网IP信息

#创建可用性集:
#$Availabilityset = New-AzureRmAvailabilitySet -Name cranetestAV -ResourceGroupName crane -Location $locationStr

#创建网卡:
$nic=New-AzureRmNetworkInterface -Name VHDNIC -ResourceGroupName $resourceGroupName -Location $locationStr -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIP.Id -Force

#配置要使用的存储账号以及系统盘名称:
$osdiskname = $vmname + "_OSDisk"
$vmOsDisk =$osdiskurl+$osdiskname+".vhd"
$cred = New-Object System.Management.Automation.PSCredential ($vmUser, $securePassword)
$osdiskAbsoluteUri=$osdiskurl+$fileName

#生成虚拟机的配置,将新建的虚拟机放在虚拟网络:

if($osType.Equals(1))#linux创建
{
$vmconfig = New-AzureRmVMConfig -VMName $vmname -VMSize $vmSize 
$vmconfig =Set-AzureRmVMOperatingSystem -VM $vmconfig -Linux -ComputerName $vmname -Credential $cred
$vmconfig =Set-AzureRmVMOSDisk -VM $vmconfig -Name $osdiskname -VhdUri $vmOsDisk  -SourceImageUri $osdiskAbsoluteUri -CreateOption FromImage -Linux 
$vmconfig =Add-AzureRmVMNetworkInterface -VM $vmconfig -Id $NIC.Id
}
if($osType.Equals(0)) #windows创建
{
$vmconfig = New-AzureRmVMConfig -VMName $vmname -VMSize $vmSize 
$vmconfig =Set-AzureRmVMOperatingSystem -VM $vmconfig  -Windows -ComputerName $vmname -Credential $cred
$vmconfig =#-ProvisionVMAgent -EnableAutoUpdate 
$vmconfig =Set-AzureRmVMOSDisk -VM $vmconfig  -Name $osdiskname -VhdUri $vmOsDisk -SourceImageUri $osdiskAbsoluteUri -Caching "ReadWrite" -CreateOption FromImage -Windows
$vmconfig =Add-AzureRmVMNetworkInterface -VM $vmconfig  -Id $nic.Id -Primary
}

#创建虚拟机:
New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $locationStr -VM $vmconfig 

#获得创建信息
$vmList = Get-AzureRmVM -ResourceGroupName $resourceGroupName
$vmList.Name

 

posted on 2018-02-02 15:25  OceanWang  阅读(168)  评论(0编辑  收藏  举报

导航