Exploring the REST API

REST is the abbreviated form of Representational State Transfers. Using this, we can connect to the server from a client over HTTP and manipulate resources. In short, REST is a software architectural model commonly used on the World Wide Web.

Take a look at the help document of the Invoke-RestMethod method which sends an HTTP or HTTPS request to a RESTful web service.

Using the Azure REST API in PowerShell

Managing Azure is easy using the REST API and PowerShell. No need for employees clicking Azure machines to retrieve information from the Azure portal. Many IT professionals use the Azure module to automate tasks in Azure, such as building, restarting, and removing the server, and many more. Before we start using the REST API, let's explore a few Azure cmdlets.

As with any other module, we need to import the Azure module as well. A few errors in the Azure module might be misleading, but we can safely ignore these for now.

The first step is to get the Azure publish settings file. Run the following command:

Import-Module Azure -verbose
Get-AzurePublishSettingsFile

The preceding command opens up your default browser and downloads the file. The next step is to import the Azure file. Run the following command:

Import-AzurePublishSettingsFile -PublishSettingsFile C:TempFile

That's it! Now, we can play with the Azure cmdlets.

Approximately 737 cmdlets are available in the Azure module. Explore the help and automate your tasks. As this topic is about the Azure REST API, let's create a website for us to walk through. Run the following command:

New-AzureWebsite -Name "MyAzureSiteDemo" -Verbose

Well! We've now created an Azure website. Very much in a human readable style which is PowerShell Verb-Noun with parameters. You may wonder, what does the preceding command do? It simply creates a new Azure website. Indeed, you can create multiple sites; for this, we need to consider the Name property. Run the following command:

help New-AzureWebsite -Parameter Name

Now, take a look at the following image:

Using the Azure REST API in PowerShell

This accepts pipeline inputs. The following code shows the method to process multiple sites.

"Site1" , "Site2" | %{
  New-AzureWebsite -Name $_ -Verbose
}

Now, let us see the Kudu project from Git—It's huge, so you need to go through the details using the following link:

https://github.com/projectkudu/kudu

With reference to this link, we will try to fetch the version with the site's last modified time stamp, as follows:

$site = Get-AzureWebsite -Name MyAzureSiteDemo
$username = $site.PublishingUsername
$password = $site.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$api = "https://$($site.Name).scm.azurewebsites.net/api"
$kudu = Invoke-RestMethod -Uri "$api/environment" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET
$kudu

Output of the command is shown in the following figure:

Using the Azure REST API in PowerShell

The code performs eight actions to fetch the information we need:

  • 1: First, it obtains the azure website information as Object.
  • 2: Then, it gets the username and password of the site (This is a property of the Get-AzureWebSite cmdlet).
  • 3: By typecasting the code, it will convert the authentication to base64 encoding.
  • 4: Here, we used the API URL—this is REST (Note that the site we created doesn't have the GIT resource mapped—it's just a dummy site).
  • 5: Now, the power of the Invoke-WebRequest cmdlet comes into the picture—we have come across this cmdlet and its features in the previous topic.
  • 6: In the preceding code, we used an environment in the URL. If you are interested in exploring and doing more, refer to the following link and proceed further: https://github.com/projectkudu/kudu/wiki/REST-API
  • 7: In this step, we used the Authorization headers. This parameter is of the <IDictionary> type and can't be used for the user agent or cookie. Just specify the header like shown in the code. Here we are passing the authorization value as Basic and {0} represents the place holder. The value of the $base64AuthInfo variable will be passed like Basic <$base64AuthInfo>.
  • 8: Finally, we retrieved the output, as you saw in the figure.

However, this is not enough for the infrastructure, so we need to do more! We need to jump-start Azure Resource Manager (ARM). We do have a module for this where we can manage the Azure environment—behind the scenes, it invokes the REST API.

If you are a DevOps looking for an ARM and Application Lifecycle Management (ALM) integration, use the following link:

https://www.microsoftvirtualacademy.com/en-us/training-courses/azure-resource-manager-devops-jump-start-8413

Note

As the following modules are separated:

  • AzureResourceManager
  • AzureServiceManagement

To explore resource manager, we should first run the following command:

Switch-AzureMode -Name AzureResourceManager
#or
Switch-AzureMode -Name AzureServiceManagement

Read the help document, explore Azure, and use the REST API.

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

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