zoukankan      html  css  js  c++  java
  • Azure PowerShell 在ARM环境下使用指定 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 |`
    Set-AzureRmVMOperatingSystem -Linux -ComputerName $vmname -Credential $cred| `
    Set-AzureRmVMOSDisk -Name $osdiskname -VhdUri $vmOsDisk  -SourceImageUri $osdiskAbsoluteUri -CreateOption FromImage -Linux | `
    Add-AzureRmVMNetworkInterface -Id $NIC.Id
    }
    if($osType.Equals(0)) #windows创建
    {
    $vmconfig = New-AzureRmVMConfig -VMName $vmname -VMSize $vmSize |`
    Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmname -Credential $cred| `
    #-ProvisionVMAgent -EnableAutoUpdate | `
    Set-AzureRmVMOSDisk -Name $osdiskname -VhdUri $vmOsDisk -SourceImageUri $osdiskAbsoluteUri -Caching "ReadWrite" -CreateOption FromImage -Windows | `
    Add-AzureRmVMNetworkInterface -Id $nic.Id -Primary
    }
    
    #创建虚拟机:
    New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $locationStr -VM $vmconfig 
    
    #获得创建信息
    $vmList = Get-AzureRmVM -ResourceGroupName $resourceGroupName
    $vmList.Name
  • 相关阅读:
    Java基础——clone()方法浅析
    Unity shader error: “Too many texture interpolators would be used for ForwardBase pass”
    ar 解压一个.a文件报错: xxx.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
    How to set up "lldb_codesign" certificate!
    Unity-iPhone has Conflicting Provisioning Settings
    ETC1/DXT1 compressed textures are not supported when publishing to iPhone
    Xcode 提交APP时遇到 “has one iOS Distribution certificate but its private key is not installed”
    XCode iOS之应用程序标题本地化
    苹果电脑(Mac mini或Macbook或iMac)恢复出厂设置
    Unity 4.7 导出工程在XCode10.1上编译报错
  • 原文地址:https://www.cnblogs.com/oceanwang/p/7762844.html
Copyright © 2011-2022 走看看