Summary: Use the Azure Resource Manager cmdlets to assign credentials and create the virtual machine.
This blog post is part of a series about how to create Azure Resource Manager virtual machines by using PowerShell. To get the most out of this series, read the posts in order.
- Create Azure Resource Manager virtual machines by using PowerShell – Part 1
- Create Azure Resource Manager virtual machines by using PowerShell – Part 2
- Create Azure Resource Manager virtual machines by using PowerShell – Part 3
- Create Azure Resource Manager virtual machines by using PowerShell – Part 4
- Create Azure Resource Manager virtual machines by using PowerShell – Part 5
If you’re new to PowerShell, you might want to start with the two series that precede this series:
- Work with the Azure Resource Manager cmdlets
- Retrieve Azure Resource Manager virtual machine properties by using PowerShell
Now that I’ve built all the objects for the virtual machine (VM), what’s left before we spin up the machine in Azure Resource Manager?
Honorary Scripting Guy, Sean Kearney, is here to finish up our three-week session about VMs in Azure Resource Manager using PowerShell.
Let’s consider everything that we’ve seen over the past few weeks on a high level.
- Create the necessary resources and infrastructure for Azure VMs
- Spin up a VM and access its properties by using PowerShell
- Identify the necessary properties to create a new VM
Then, finally, over the last few days, we’ve been putting together that VM.
Our last step is actually pretty simple, but it’s one of the more important steps. We need to assign the primary UserId and password for the VM. There are, of course, some important and critical details.
You obviously cannot use a UserId of ‘root’ in the Linux environment or ‘Administrator’ in the Windows world as they are both built in. In the case of Windows, this account would be disabled by default.
Now an important note on the password. It can’t be anything obvious. In fact, if you try, Azure will reject it. So ‘Password’,’P@ssw0rd’ are actually right off the list. Won’t even let you, so don’t try.
Well, you can try but, it’s only useful if you like red error messages. 😉
The next cmdlet is Set-AzureVMOperatingSystem, and its task is to identify the key pieces for our operating system.
- The host name to assign to the operating system
- The primary user name
- The password
- The operating system type
The Hostname (or NetBIOS name in the Windows world) is typically the same as the VM name. It can be unique, but you’re better off to avoid digging in the cloud for the actual name. Let’s just say, this is a good practice to, at least, have them match.
So, remember earlier that we defined our VM name with the following line:
We’ll just use this value for the computer name. To assign the credentials (the UserID and password), we need to create a PSCredential object. This is the same process that we use when we create credentials for PowerShell normally.
You can choose the interactive method (which is a lower risk on security) in the following manner:
You could prepopulate the UserID as well and just pass this to the Get-Credential cmdlet:
$Credential=Get-Credential –credential $UserID
The third option is to fully automate it. In a normal fully exposed PowerShell script, there is high level of risk because the password is in clear text. However, if you are using Systems Center Orchestrator or Azure Automation, you can mitigate this risk by storing the credentials as a secure asset.
$SecurePassword=Convertto-SecureString $Password –asplaintext -force
$Credential=New-Object System.Management.Automation.PSCredential ($UserID,$SecurePassword)
We now define the settings for our operating system by using Set-AzureRMVMOperatingSystem:
$AzureVM = Set-AzureRmVMOperatingSystem -VM $AzureVM -Windows -ComputerName $VMname -Credential $cred -Windows
If this were a Linux operating system, we would simply change the –Windows switch to a –Linux switch.
After we have defined all the particular objects, we need to call up only one cmdlet to do all the magic. We need to supply only three parameters: our ResourceGroupName, Location, and VMimage object that we have been building.
New-AzureRMVM –ResourceGroupName $RGName –location $Location –VM $AzureVM
We’ll let this run through and, given some appropriate time, we’ll have a new VM spun up in Azure!
I hope the information that we’ve provided will make it easier to create VMs in Azure. Azure really offers some amazing features outside of just infrastructure as a service. The amazing piece as I dig into it is that almost everything can be accessed and modified by using Windows PowerShell!
Please check it all out! Hope to share more with you soon!
If you’d like a sample of the PowerShell scripts that were used in this series of blog posts, you can download them from the TechNet Script Repository.
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow.
Until then, always remember that with Great PowerShell comes Great Responsibility.
Honorary Scripting Guy
Cloud and Datacenter Management MVP