Azure使用Powershell创建虚拟机

Login-AzureRmAccount  -Environment AzureChinaCloud

New-AzureRmResourceGroup -Name   liao-rg  -Location chinanorth

$subnetconfig = New-AzureRmVirtualNetworkSubnetConfig  -Name  subnet01 -AddressPrefix 192.168.0.0/16

$vnet = New-AzureRmVitualNetwork  -ResourceGroupName  liao-rg  -Location chinanorth -Name CRMnet -AddressPrefix 192.168.1.0/24  -Subnet  $subnetconfig

$pip = New-AzureRmPublicIpAddress  -IdleTimeoutInMinutes 4  -Name “mypublicdns$(Get-Random)”

$nsgRuleSSH = New-AzureRmNetworkSecurityRuleConfig  -Name mynetworksecurityGroupRuleSSH   -Protocol tcp  -Direction  Inbound -Priority  1000 -SourceAddressPrefix *    -SourcePortRange *  -DestinationAddressPrefix  *  -DestinationPortRange 22  -Access  Allow

$nsg = New-AzureRmNetworkSecurityGroup  liao-rg  -Location  chinanorth  -Name  mynetworksecurityGroup  -SecurityRules $nsgRuleSSH

$nic = New-AzureRmNetworkInterface  -Name mynic  -ResourceGroupName  liao-rg  -Location chinanorth  -SubnetId  $vnet.Subnets[0].id  -PublicIpAddressId $pip.Id  -NetworkSecurityGroupId  $nsg.Id

$securePassword = ConvertTo-SecureString    ”  -AsPlainText   -Force

$cerd  =  New-Object  System.Management.Automation.PSCredential(“azureuser”, $securePassword)

$vmconfig = New-AzureRmVMConfig  myvm  VMSize  Standard_D1  |  Set-AzureRmVMOperatingSystem -Linux  -ComputerName  myvm  -Credential  $cerd  -DisablePasswordAuthentication  |  Set-AzureRmSourceImage  -PublisherName  Canonical  -Offer UbuntuServer -Skus  14.04.2-LTS  | Add-AzureRmVMNetworkInterface -id  $nic.Id

$sshpublickey = Get-Content  “$env:USERPOFILE\.ssh\id_rsa.pub”

Add-AzureRmVMSshPublicKey  -vm $vmconfig  -KeyData  $sshpublickey  -Path  “/home/azureuser/.ssh/authorized_keys”

New-AzureRMVM   -ResourceGroupName  liao-rg  -Location  chinanorth  -vm $vmconfig