Freigeben über


Azure Powershell Part 2-Create VM

in previous post “Azure Powershell Part 1” we setup and establish a connection to Azure through Powershell, now we try to create a new VM in Azure

after we established the connection and entered relevant subscription information my your session you will be able to run from here

Step 1: Determine the ImageFamily
First you need to determine the ImageFamily or Label value for the specific image corresponding to the Azure virtual machine you want to create. You can get the list of available ImageFamily values with this command.

there is a bunch of Images out there and total list of ImageFamily you can get with Get-AzureVMImage | select ImageFamily –Unique

image

Once you identified the image you want to deploy, copy the ImageFamily name for next step

$family="<ImageFamily value>"
$image=Get-AzureVMImage | where { $_.ImageFamily -eq $family } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1

In my scenario I will use “Windows Server 2012 R2 Datacenter” please pay attention

image

Please note in some cases, the image name is in the Label property instead of the ImageFamily value. If you didn't find the image that you are looking for using the ImageFamily property, list the images by their Label property with this command –> Get-AzureVMImage | select Label –Unique

Step 2: Build your command set for VMDeploy
Build the rest of your command set by copying the appropriate set of blocks below into your new text file or the ISE and then filling in the variable values and removing the < and > characters

$vmname="<machine name>"
$vmsize="<Specify one: Small, Medium, Large, ExtraLarge, A5, A6, A7, A8, A9>"
$vm1=New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image

the value vmsize basically defines the instance class with which you classify the hardware properties of your VM. more details on “Sizes for Cloud services” 

now I want to connect this VM to a existing VMSubnet and assign also a static IP. Get-AzureVNetConfig returns back a XML structure, to get more data you can use fl to see what contains inside the XML

image

here I see I have a VMSubnet called “MyLabNetwork” and CIDR is 10.0.0.0/8 (Class A). checking which IPs are available you can use Test-AzureStaticVNetIP run following query

image

good state, so we now know VMSubnet name, subnet range and we confirmed the IP is available

so all together total script to deploy new VM would look like this

###Step1 Add your Account $userName = "<your organizational account user name>"
$securePassword = ConvertTo-SecureString -String "<your organizational account password>" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($userName, $securePassword)
Add-AzureAccount -Credential $cred ###Step1 END

###Step2 Set your subscription and storage account $subscr="<subscription name>"
$staccount="<storage account name>"
Select-AzureSubscription -SubscriptionName $subscr –Current
Set-AzureSubscription -SubscriptionName $subscr -CurrentStorageAccountName $staccount ###Step2 END

###Step3 - Determine ImageFamily and Build VMDeploy CommandSet $vmname="MyLAB2012R2"
$family="Windows Server 2012 R2 Datacenter"
$vmsize="Small"
$vm1=New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image
$cred=Get-Credential -Message "Type the name and password of the local administrator account."

$vm1 | Add-AzureProvisioningConfig -Windows -AdminUsername $cred.Username -Password $cred.GetNetworkCredential().Password
Test-AzureStaticVNetIP –VNetName "MyLabNetwork" –IPAddress 10.0.0.50
$vm1 | Set-AzureStaticVNetIP -IPAddress 10.0.0.50
$vm1 | Set-AzureSubnet -SubnetNames "MyLabNetwork" ###Step3 END

image 
image

till here, we “only” passed the values but didn’t really create the VM, the final command New-AzureVM is required to kick on the real deployment in Azure

New-AzureVM –ServiceName "<short name of the cloud service>" -VMs $vm1

image

Once deployment started you will see it in your Azure dashboard

image

image

more parameters are available for New-AzureVM commandlet here

Parameter Set: ExistingService

New-AzureVM -ServiceName <String> -VMs <PersistentVM[]> [-DeploymentLabel <String> ] [-DeploymentName <String> ] [-DnsSettings <DnsServer[]> ] [-InternalLoadBalancerConfig <InternalLoadBalancerConfig> ] [-ReservedIPName <String> ] [-VNetName <String> ] [-WaitForBoot] [ <CommonParameters>]

Parameter Set: CreateService

New-AzureVM -ServiceName <String> -VMs <PersistentVM[]> [-AffinityGroup <String> ] [-DeploymentLabel <String> ] [-DeploymentName <String> ] [-DnsSettings <DnsServer[]> ] [-InternalLoadBalancerConfig <InternalLoadBalancerConfig> ] [-Location <String> ] [-ReservedIPName <String> ] [-ReverseDnsFqdn <String> ] [-ServiceDescription <String> ] [-ServiceLabel <String> ] [-VNetName <String> ] [-WaitForBoot] [ <CommonParameters>]

Password need to comply with following security standards else deployment will fail because of password policy, also only following usernames “Admin1, Administrator, Admin”"…” can be used. to use custom admin names you need to use Add-AzureProvisioningConfig -Windows -AdminUsername "<Custom Admin Username>" -Password <YOURPASSWORD>

if you modify $creds you have to pass that again to VMs config

$vm1 | Add-AzureProvisioningConfig -Windows -AdminUsername "<Custom Admin Username>" -Password <YOURPASSWORD>

image

Quick Tipp, in case you run into any issues during deployment of VM you can use –debug which helps to determine why deployment is failing to proceed

Windows Azure Management Cmdlets
https://msdn.microsoft.com/en-us/library/windowsazure/jj152841

Sizes for Cloud Services
https://azure.microsoft.com/en-us/documentation/articles/cloud-services-sizes-specs/

Azure Limits and Quotas
https://azure.microsoft.com/en-us/documentation/articles/azure-subscription-service-limits/

Should I choose cloud services or something else?
https://azure.microsoft.com/en-us/documentation/articles/cloud-services-choose-me/

there are tons of options available when you are creating Virtual Machine in Azure like a.e Domain Join, additional disk, StaticIP (DIP) for more details around configuration possibilities check out the commandlet “Add-AzureProvisioningConfig”