📜 ⬆️ ⬇️

Windows Azure PowerShell to work with IaaS

Introduction

Back in June 2012, Windows Azure PowerShell cmdlets were updated and some interesting functionality was added to manage Windows Azure virtual machines. In this article I will discuss some of the new features in Windows Azure IaaS automation.

Configuring Windows Azure PowerShell
The first thing to do is install Windows Azure PowerShell. The easiest way to download and import file settings is to download a template from the official site. After the publish settings file is loaded, you need to import it:
Import-AzurePublishSettingsFile 'c:\temp\mysub.publishsettings' 

It is also possible to customize the publication profile manually, without downloading the settings file:
 $subid = '[YOUR-SUBSCRIPTION-ID]' $cert = Get-Item Cert:\CurrentUser\My\YOURCERTTHUMBPRINT Set-AzureSubscription -SubscriptionName 'testsub1' -SubscriptionId $subid -Certificate $cert 

Please note that after the subscription has been completed, the posting profile is saved by default in the following location: C: \ Users \ user \ AppData \ Roaming \ Windows Azure Powershell
This means that you do not need to run a Set-AzureSubscription for each of the scenarios, since it already exists. Windows Azure PowerShell supports multiple subscriptions, so here you can choose which subscription to work with, with the Select-AzureSubscription command.

Storage Configuration

image
Also, the new addition is –CurrentStorageAccount. This parameter allows you to specify which repository to use for your VM's in work from PowerShell. To install the repository, run the following command:
 Get-AzureStorageAccount 

If you need to create a StorageAccount, use the following command:
 New-AzureStorageAccount -StorageAccountName 'myuniquelynamedstorage' -Location 'East US' 


Preparing to create a virtual machine from PowerShell

image
Before you start creating VMs in Windows Azure, you need to set up some of the settings that will be required to work from PowerShell, namely, the location:
  $dclocation = '[YOUR-LOCATION]' 

Set the name of the cloud service that will act as a container for VM'S:
 Test-AzureName -Service '[YOUR-CLOUD-SERVICE-NAME]' $cloudSvcName = '[YOUR-CLOUD-SERVICE-NAME]' 

Determine which platform will be used as the basis for VM's:
 Get-AzureVMImage | select ImageName $image = '[YOUR-SELECTED-IMAGE-NAME]' 

Now you can start creating VMs from PowerShell.
')
Quickly create Windows VMs from PowerShell

After all initial settings are complete, to quickly create a virtual machine, you must use the following commands:
 $adminPassword = '[PASSWORD]' $vmname = 'mytestvm' New-AzureQuickVM -Windows -ServiceName $cloudSvcName -Name $vmname -ImageName $image -Password $adminPassword 


Quickly create Linux VMs from PowerShell

 $linuxuser = '[CHOOSE-USERNAME]' $adminPassword = '[YOUR-PASSWORD]' $vmname = 'mytestvm1' New-AzureQuickVM -Linux -ServiceName $cloudSvcName -Name $vmname -ImageName $image -LinuxUser $linuxuser 


Restart, start and stop the Windows Azure virtual machine

 #  Restart-AzureVM -ServiceName $cloudSvcName -Name $vmname #  Stop-AzureVM -ServiceName $cloudSvcName -Name $vmname #  Start-AzureVM -ServiceName $cloudSvcName -Name $vmname 


Advanced Commands for Creating Virtual Machines

New-AzureVMConfig allows you to create a virtual machine by configuring for yourself. If you are not satisfied with the rapid creation of VM's or do not like the number of settings that this command provides, then this section is for you. You can add a data disk, configure endpoints (automatically added for SSH and RDP), and even change the behavior of the OS disk cache or the data disk. All you need to do is call the New-AzureVMConfig command, and then send them to the VM's.

Creating Windows Virtual Machines from PowerShell

 $vmname2 = 'mytestvm2' $vmname3 = 'mytestvm3' $vm2 = New-AzureVMConfig -Name $vmname2 -InstanceSize ExtraSmall -ImageName $image | Add-AzureProvisioningConfig -Windows -Password $adminPassword | Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk1' -LUN 0 | Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'web' ` -LBSetName 'lbweb' -ProbePort 80 -ProbeProtocol http -ProbePath '/' $vm3 = New-AzureVMConfig -Name $vmname3 -InstanceSize ExtraSmall -ImageName $image | Add-AzureProvisioningConfig -Windows -Password $adminPassword | Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk2' -LUN 0 | Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'web' ` -LBSetName 'lbweb' -ProbePort 80 -ProbeProtocol http -ProbePath '/' New-AzureVM -ServiceName $cloudSvcName -VMs $vm2,$vm3 


Creating a Linux virtual machine from PowerShell

 $vmname2 = 'mytestvm2' $vmname3 = 'mytestvm3' $vm2 = New-AzureVMConfig -Name $vmname2 -InstanceSize ExtraSmall -ImageName $image | Add-AzureProvisioningConfig -Linux -LinuxUser $linuxUser -Password $adminPassword | Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk1' -LUN 0 | Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'web' ` -LBSetName 'lbweb' -ProbePort 80 -ProbeProtocol http -ProbePath '/' $vm3 = New-AzureVMConfig -Name $vmname3 -InstanceSize ExtraSmall -ImageName $image | Add-AzureProvisioningConfig -Linux -LinuxUser $linuxUser -Password $adminPassword | Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk2' -LUN 0 | Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'web' ` -LBSetName 'lbweb' -ProbePort 80 -ProbeProtocol http -ProbePath '/' New-AzureVM -ServiceName $cloudSvcName -VMs $vm2,$vm3 


Upgrading Existing Virtual Machines

Modifying existing virtual machines requires getting the current settings using the Get-AzureVM command, after editing, the Update-AzureVM command is used to save the settings. Some of the changes will require a virtual machine reboot, for example, disk cache parameters.
 $vmname = 'mytestvm1' Get-AzureVM -Name $vmname -ServiceName $cloudSvcName | Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk1' -LUN 0 | Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'translogs1' -LUN 1 | Add-AzureEndpoint -Protocol tcp -LocalPort 1433 -PublicPort 2000 -Name 'sql' | Update-AzureVM 


Conclusion

In this topic, I reviewed the basic commands for working with virtual machines from PowerShell, which will help beginners in managing virtual machines in Azure.

Source: https://habr.com/ru/post/156441/


All Articles