With Microsoft Azure you can set up virtual machines with a great range of operating systems quickly and easily. For the administration, Microsoft Azure offers a Web Management interface.
But Azure PowerShell cmdlets are even more interesting for assembling individual script libraries. Use these scripts to install virtual machines comfortably.
I’d like to use this article to explain possibilities of setting up Azure Virtual Machines with PowerShell.
Start by downloading and installing the Azure PowerShell. Installation happens via WebPlatform-Installer:
After that, Azure PowerShell will start automatically.
The next thing is to get it in touch with your Azure account. Use one of these cmdlets:
- Get-AzurePublishSettingsFile and
I use PublishSettingsFile. With Get-AzurePublishSettingsFile, the download page for PublishSettingsFiles is requested. It can also be reached directly via the link Azure Publish Settings.
The downloaded file must be imported into the Azure PowerShell with the cmdlet Import-AzurePublishSettingsFile.
Now, Azure PowerShell is ready to communicate with your Azure environment.
Installing a Virtual Machine with Azure PowerShell
For the installation of a new machine, you usually don’t need an ISO with the operating system. Azure offers an extensive library with readily set up VMs you can use.
Now, let’s have a closer look at how Azure Virtual Machines can be administered with PowerShell.
With Get-AzureVMImage | Select ImageName, you can get an overview over all presently available VMs.
In the following example, I used the recent Windows Server 2012 R2 VHD:
But first, the Storage Account has to be set for the current subscription.
$storeage = Get-AzureStorageAccount | Select StorageAccountName
$subscription = Get-AzureSubscription | Select SubscriptionName
Set-AzureSubscription -SubscriptionName $subscription.SubscriptionName -CurrentStorageAccountName $storage.StorageAccountName
Then, the selected VHD can be saved in a variable:
$image = “a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201409.01-en.us-127GB.vhd”
Next, a new Virtual Machine configuration has to be created. It can then be modified with further settings: e.g. attach hard drives or configure endpoints. However, there also is a faster way which I am going to show later.
Our new machine “FA-TestServer” is to be created as a small instance and with the previously selected image.
$vm1 = New-AzureVMConfig -Name “FA-TestServer” -InstanceSize "Small" -ImageName $image
Then, initial-settings are attached to the configuration-object for the virtual machine. In our example, the administrator-account and password should suffice for now.
$vm1 | Add-AzureProvisioningConfig -Windows -AdminUserName ”adm.test” -Password “P@ssw0rd!”
Now we test whether our machine-name is already used somewhere or not.
Test-AzureName -service "FA-TestServer"
Is it not used yet, we can create our configured virtual machine in the cloud-service “FA-TestCloudService”.
$vm1 | New-AzureVM -ServiceName "FA-TestCloudService" -Location "North Europe"
Now, as promised, the quicker version: execute all the settings with the cmdlet “New-AzureQuickVM”:
New-AzureQuickVM -Windows -ServiceName FA-TestServer2 -Name FA-TestServer2 –ImageName “a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201409.01-en.us-127GB.vhd” -InstanceSize 'Small' -AdminUserName ”adm.test” -Password “P@ssw0rd!” -Location "North Europe"