#-----------------------------------------------------------------Create a virtual machine with a VHD in the specified BLOB-------------------------------------- --#目标存储信息 $destStorageAccountName = "{Storage account name}" #目标存储账户名称 $destContainerName = "{container name}" #VHD所在容器名称 $ Deststorageaccountkey = "{Storage account Key}" #key $fileName = "{Mirror name and extension}" #容器中文件的名称及扩张名-xxxxxx.vhd# virtual machine Information $resourcegroupname= " Demoresourcegroup "#要创建资源组的名称 $vmname =" Createvmtest "#虚拟机名称 $vmUser =" Azureuser "$vmSize =" standard_a4 "$osType =1 #0- Windows 1-linux
#公共配置信息-Default, no configuration $credlogin = New-object System.Management.Automation.PSCredential ("{Subscription account name}", $securePassword) $ Securepassword = convertto-securestring ' {Subscription login password} '-asplaintext-force #此密码也将作为虚拟机登录密码 $endpoint = " core.chinacloudapi.cn "$fileAbsoluteUri =" https://"+ $sourceStorageAccountName +". Blob. " + $endpoint + "/" + $sourceContainerName + "/" + $fileName $locationstr= "China North" $osdiskurl = "https://" +$ Deststorageaccountname+ ". Blob." + $endpoint + "/" + $destContainerName + "/" #拷贝后的vhd所在存储账户url地址 (not including file name) #登录并创建指定资源组Login-azurermaccount- Environmentname azurechinacloud-credential $credloginNew-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# Create a public network IP address: $publicIP =New-azurermpublicipaddress-name createstfrompip-resourcegroupname $resourceGroupName-location $locationStr- Allocationmethod dynamic-ipaddressversion ipv4-force #新建Get-azurermpublicipaddress-name CreatestfromPIP- Resourcegroupname $resourceGroupName #查看刚刚创建的公网IP信息 # Create an availability set: # $Availabilityset = New-azurermavailabilityset-name Cranetestav-resourcegroupname crane-location $LOCATIONSTR # Create network card: $nic =new-azurermnetworkinterface-name VHDNIC- Resourcegroupname $resourceGroupName-location $locationStr-subnetid $vnet. Subnets[0]. Id-publicipaddressid $publicIP. id-force# Configure the storage account to use and the system disk name: # $storage = Get-azurermstorageaccount-name Cranenorthtest-resourcegroupname $resourceGroupName # $vmname = "Whxcreatevmtest" $osdiskname = $vmname + "_osdisk" #$ Osdiskurl = "https://ygwhxteststg.blob.core.chinacloudapi.cn/whx-vhd/ Community-15186-37f6baac-720f-4e49-b2f7-9c39f44a82f9-1.vhd "$vmOsDisk = $osdiskurl + $osdiskname +". vhd "$cred = New-object System.Management.Automation.PSCredential ($vmUser, $securePassWord) $osdiskAbsoluteUri = $osdiskurl + $fileName # Generate the configuration of the virtual machine and place the new virtual machine on the virtual network:
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
Create a virtual machine with the specified VHD mirror under arm using Azure PowerShell