Manage Azure Virtual Machines with PowerShell

    PowerShell is a very powerful tool, you only need to know how to use it correctly. It is also important to note that for some time now PowerShell provides more functionality than using graphical controls: everything that can be done from the graphical interface can be implemented using PowerShell cmdlets. On the contrary, this rule does not work. In the case of Microsoft Azure, using PowerShell, you can do the same actions as through the management portal, except for creating the subscription itself. Using Azure PowerShell, you can control and automate the deployment and management of workloads. In this article, we’ll talk about how you can use Azure PowerShell to manage virtual machines.

    Install Azure PowerShell and connect to a subscription

    Begin by installing the PowerShell module itself for Azure management. The installation process is extremely simple. You need to download the Microsoft Web Platform Installer , run it and install Microsoft Azure PowerShell with the Microsoft Azure SDK. In addition to the modules, a customized Azure PowerShell console will also be installed.

    The next step is to connect to your Azure subscription. There are two ways to connect:
    • Azure the Active Directory . This authentication method is recommended for the reason that it facilitates the access control process for your Azure subscription. In this method, you use your Microsoft account (or your work account) to log in. Azure Active Directory validates the data and returns an access token that allows you to manage your Azure subscription using PowerShell.
    • Certificate of Management. If you use a management certificate, it is important to remember that access to your subscription using PowerShell will be valid as long as your certificate is valid. However, this method is more complicated when managing subscription sharing. In addition, the Azure Resource Manager API does not accept certificate authentication.
    Next, we’ll look at the whole process of connecting to a subscription using both methods.

    I completely forgot to say. You can use Azure PowerShell to manage your Azure subscription. But using several different PowerShells is not always convenient. You can easily import the Azure PowerShell module into Windows PowerShell. If you want to know how, look under the spoiler.

    How to import an Azure PowerShell module into Windows PowerShell
    We start Windows PowerShell with Administrator rights and use only two cmdlets:
    Set-ExecutionPolicy RemoteSigned
    Import-Module “C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1”

    Using the first cmdlet, we determine that all downloaded PowerShell scripts must be signed by a trusted publisher before they are run. Well, with the second cmdlet, we import the Azure PowerShell module into Windows PowerShell.

    Azure ad

    1. Launch the Azure PowerShell Console
    2. Run the cmdlet

    After its execution, a window will appear in which you will need to specify the email address associated with the account under which you want to log in

    Next, you will need to enter the password for your account. Azure authenticates you to the system, saves the credentials, and closes the window. In PowerShell, you can see the result of the cmdlet:

    Azure PowerShell is now successfully connected to your subscription and you can get started.

    Management Certificate

    1. Go to the Azure Management Portal and launch the Azure PowerShell Console
    2. In the console, enter the cmdlet:

    This cmdlet will open a web page on the Azure Management Portal from which you can download information about your subscription.
    1. Download and save the “*. publishsettings , which contains the management certificate.
    2. Now import the file using the cmdlet
    Import-AzurePublishSettingsFile C:\Users\\Downloads\-credentials.publishsettings

    This completes the process of connecting Azure PowerShell to your subscription, and you can begin further work.

    Creating virtual machines

    Before you start working with a virtual machine in Azure, you need to create it. To do this, follow a few simple steps.
    First you need to install your Azure subscription and storage account. To find out the name of a subscription, use the cmdlet:

    Vault Account Name:

    Now install:
    Select-AzureSubscription –SubscriptionName “Visual Studio Ultimate with MSDN”
    Set-AzureSubscription –SubscriptionName “Visual Studio Ultimate with MSDN” –CurrentStorageAccountName “portalvhdsbs5v2wvyhthcq”

    Now you need to select ImageFamily or the label value for the specific image that matches the Azure virtual machine that you want to create. Here are some sample images from the Azure Management Portal gallery.

    You can see the list of available images using the cmdlet:
    Get-AzureVMImage | select ImageFamily –Unique

    Sometimes, the name of the image is hidden in the Label property , and not in ImageFamily . Therefore, if you did not find the desired image after searching ImageFamily, try running the following cmdlet:
    Get-AzureVMImage | select Label -Unique

    After we have decided on the image, we can begin to create a virtual machine. For this, I recommend using a script, although you can enter commands line by line in regular Windows PowerShell.
    $family="Windows Server 2012 R2 Datacenter"
    $image=Get-AzureVMImage | where { $_.ImageFamily -eq $family } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1
    $vm1=New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image -AvailabilitySetName $availset
    $cred=Get-Credential -Message "Type the name and password of the local administrator account."
    $vm1 | Add-AzureProvisioningConfig -Windows -AdminUsername $cred.GetNetworkCredential().Username -Password $cred.GetNetworkCredential().Password
    New-AzureVM –Location "North Europe" -VMs $vm1 

    Now I will explain what is happening in this script.
    First, we specify the name of the virtual machine image that we received when the Get-AzureVMImage | select ImageFamily –Unique . With the same success, it can be replaced with the name Label . Next, we select the latest image that is available in the Microsoft Azure Gallery from this image family.
    $family="Windows Server 2012 R2 Datacenter"
    $image=Get-AzureVMImage | where { $_.ImageFamily -eq $family } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1

    Next, specify the name of the future virtual machine, select its size (you can choose from Small, Medium, Large, ExtraLarge, A5, A6, A7, A8, A9 ), specify the name of the availability group. In conclusion, on the basis of all these properties, we form the virtual machine itself.
    $vm1=New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image -AvailabilitySetName $availset

    Next, we specify the username and password for the local administrator:
    $cred=Get-Credential -Message "Type the name and password of the local administrator account."
    $vm1 | Add-AzureProvisioningConfig -Windows -AdminUsername $cred.GetNetworkCredential().Username -Password $cred.GetNetworkCredential().Password

    When the script is executed, a window will appear in which we will have to enter the login and password:

    Finally, using the last cmdlet, we create a new virtual machine in a new cloud service:
    New-AzureVM –Location "North Europe" -VMs $vm1 

    We indicate the desired location and can choose from Central US, East US, East US 2, South Central US, West US, North Europe, West Europe, East Asia, Southeast Asia, Japan West, Japan East .
    Also, when you run this cmdlet, you will need to enter a name for the cloud service you are creating. We wait a few minutes and the Azure virtual machine is ready!

    We can see the same virtual machine in the portal:

    Turn on, turn off, delete

    If creating a virtual machine requires a certain sequence of steps, then just one cmdlet is enough to remove it:
    Remove-AzureVM -ServiceName "mvademovm" -Name "mvademovm" -DeleteVHD

    One of the situations where it is more convenient to use Azure PowerShell to manage a virtual machine than to go to the Azure Management Portal is to turn it on and off. This task is solved by two scripts:
    starting a virtual machine
    Start-AzureVM -ServiceName "mvademovm" -Name "mvademovm"

    shutdown of the virtual machine
    Stop-AzureVM -ServiceName "mvademovm" -Name "mvademovm" -Force

    The main examples of managing virtual machines using Microsoft Azure, we reviewed. You can find a detailed list of Azure PowerShell cmdlets here .
    I hope the information in this article will be useful to you!
    Good luck with PowerShell to tune your systems!

    useful links

    Also popular now: