Using PowerShell with Azure

There are two key things you need to do before you can start managing Azure features using PowerShell. The first is to obtain an Azure subscription. The second is to get access to the cmdlets you need to be able to access Azure (and Office 365's features).

Azure is a commercial service—each feature you use has a cost attached. Azure charges are based on resource usage. With an Azure VM, for example, you would pay to have the VM running, with additional charges for the storage the VM uses and for any network traffic.

The charges for Office 365, on the other hand, are user-based—a given user can use lots of email, for example, without incurring any additional charges. For details on costs for Azure, see https://azure.microsoft.com/pricing/, and for details of Office 365 charges, see https://products.office.com/business/compare-office-365-for-business-plans.

To use Azure's IaaS and PaaS features, you need to have an Azure subscription. There are many ways you can get an Azure subscription, including via an MSDN subscription, an Action Pack subscription, or by outright purchase. Naturally, you need to ensure that any systems are properly licensed.

Microsoft also provides a one-month free trial subscription. This subscription provides you with full access to Azure features up to a financial limit, which, at the time of writing, is $200 US dollars or similar in other currencies. These limits may have changed by the time you read this book. Having said that, the trial subscription should be sufficient to enable you to learn how to use PowerShell with Azure.

If you do not have an existing subscription to Azure, navigate to https://azure.microsoft.com/free/, where you can create a trial subscription.

Note that a free trial requires you to submit a credit card number. There is no charge for the subscription; the credit card number is used only to identify verification—plus it keeps the lawyers happier.

If you take out an Azure trial and you want to keep your Azure resources running after the trial expires, you have to move it to a pay as you go subscription. You will receive an email shortly before the trial expires to transition it, which prevents downtime if you are using the trial for production.

To use PowerShell with Azure's various features, you need to obtain cmdlets that Microsoft does not provide in Windows Server 2019, Windows PowerShell 5.0/5.1, or PowerShell Core. You get the relevant modules from the PowerShell Gallery using the cmdlets in the PowerShellGet module to find and download them.

Azure has had PowerShell support almost since the very start of the service. These cmdlet sets have changed as Azure has matured (and expanded in scope). In 2019, the Azure PowerShell team released a new module, the Az module, to serve as the basis for automating Azure operations with PowerShell. The Az module is actually a set of modules containing over 2,500 cmdlets, a few of which you explore in this chapter.

Getting ready

To run this recipe, and all the recipes in this chapter, on CL1, which you initially configured in the Installing RSAT Tools on Windows 10 and Windows Server 2019 recipe in Chapter 1, Establishing a PowerShell Administrative Environment.

How to do it...

  1. Find the core AZ module:
    Find-Module -Name Az
  2. Install the AZ modules:
    Install-Module -Name Az -Force
  3. Discover Azure modules and how many cmdlets each contains:
    $HT = @{ Label ='Cmdlets'
             Expression = {(Get-Command -module $_.name).count}}
    Get-Module Az* -ListAvailable | 
      Sort {(Get-command -Module $_.Name).Count} -Descending |
        Format-Table -Property Name,Version,Author,$HT -AutoSize
  4. Find the Azure AD cmdlets:
    Find-Module AzureAD |
      Format-Table -Property Name,Version,Author -AutoSize -Wrap
  5. Download and install the AzureAD module:
    Install-Module -Name AzureAD -Force
  6. Discover the AzureAD module:
    $FTHT = @{
      Property = 'Name', 'Version', 'Author', 'Description'
      AutoSize = $true
      Wrap     = $true
    }
    Get-Module -Name AzureAD -ListAvailable |
      Format-Table @FTHT
  7. Log in to Azure:
    $CredAZ = Get-Credential
    $Subscription = Login-AzAccount -Credential $CredAZ
  8. Obtain the Azure subscription details:
    $SubID = $Subscription.Context.Subscription.SubscriptionId
    Get-AzSubscription -SubscriptionId $SubId |
      Format-List -Property *
  9. Get Azure locations:
    Get-AzLocation | Sort-Object Location |
      Format-Table Location, Displayname
  10. Get Azure environments:
    Get-AzEnvironment |
      Format-Table -Property name, ManagementPortalURL

How it works...

In step 1, you used the Find-Module cmdlet to search the PowerShell Gallery for the AZ module, which produces the following output:

How it works...

In step 2, you downloaded and installed the AZ modules, which generates no output. In step 3, you looked at the downloaded modules, which look like this:

How it works...

In step 4, you found the Azure AD module, which generates output like this:

How it works...

In step 5, you installed the Azure AD module, which generates no output. After you installed the module, in step 6, you examined the module, which generated the following output:

How it works...

In step 7, you used your subscription logon information and logged on to Azure. This step produced output. In step 8, you got the details of your subscription, which looks like this:

How it works...

In step 9, you used the Get-AzLocation cmdlet to discover the current Azure locations, which produces output like this:

How it works...

In the final step, step 10, you viewed the different public Azure clouds that are run by Microsoft. The output of this step looks like this:

How it works...

There's more...

In step 3, you examined the modules you downloaded. Possibly confusingly, you downloaded the Az module, which actually downloads the 46 separate modules you can see in the preceding screenshots.

In step 9, you viewed the current Azure public cloud locations. Each of these 30 locations provide a range of services, although not all services are necessarily in every location. In step 10, you viewed the current Azure environments. Each of these four environments are totally separate and unrelated cloud offerings. The recipes in this chapter should run unchanged on each of the four Azure cloud environments, but no testing has been undertaken in this regard. You should also be aware that Azure is a very fast changing set of products. By the time you read this book, Azure may well have expanded.

See also

To keep up to date about new Azure product updates as well as roadmap announcements, see the Azure updates page at https://azure.microsoft.com/en-gb/updates/. The recipes in this chapter make use of Azure data centers in Europe. Depending on where you live, you may find that these data centers are further away from locations nearer to you. To discover network latency between you and the Azure locations, see https://www.AzureSpeed.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.138.202