PowerShell Tips and Tricks for Azure

This post has matured and its content may no longer be relevant beyond historical reference. To see the most current information on a given topic, click on the associated category or tag.

Earlier this month, Microsoft announced new ways of working with Azure Resource Manager with PowerShell. You can use a template to image a complete gallery, blog, wiki, or SharePoint portal to create resource groups that contain all of your resources. Once you do this, you can manage resource groups as logical units.

Here’s how you can use Azure PowerShell through Azure Resource Manager.

Before you begin

To complete the tasks and examples in this guide, you will need to have an Azure account and Azure PowerShell. You can open a free Azure account and use the free services including websites without incurring any charges unless you change your account settings. You will receive credits that can also be used toward some of the premium paid Azure services.

PowerShell cmdlets from Azure PowerShell 1.0 Preview don’t have to be installed yet because you can still use Azure PowerShell 0.9.8. The main difference is that the 1.0 Preview cmdlet follows a {verb}-AzureRm{noun} pattern, and the 0.9.8 doesn’t include Rm. If you are working with 0.9.8, you will have to run the Switch-AzureMode AzureResourceManager command, which will enable the Resource Manager. You don’t have to run that step in the 1.0 Preview version.

Help with cmdlets

You can find detailed help for cmdlets by using the Get-Help cmdlet. To do this, simply enter -Get-Help-Detailed To find a list of cmdlets with a help synopsis enter: PS C:> Get-Command-Module AzureRM.Resources | Get-Help | Format-Table Name, Synopsis

Log in to Azure

Before you begin working you need to log in to your Azure account. To do this, use the Login-AzureRmAccount cmdlet. In PowerShell versions earlier than 1.0, you will need to use Add-AzureAccount.

This cmdlet will ask for your Azure account login information. After logging in, your account settings will be available to download to Azure PowerShell. Your account settings need to be refreshed periodically because they expire after some time. To refresh the settings, simply run Login-AzureRmAccount, which is required by the Resource Manager modules, anyway.

When deploying a resource, you’ll need to identify where you want to host it. Different regions support different resource types. You will have to find out which regions support the types of resources you are trying to use. To effectively deploy a resource, you should create them in the same region, even though resource groups may have resources spread out in different locations. You may also want to have your database in the same location as the app that is accessing it. To find out which locations support every resource type, use the cmdlet Get-AzureRmResourceProvider

When you run Get-AzureRmResourceProvider you will see a small table with ProviderNamespace and RegistrationState ResourceTypes. ProviderNamespace is a list of related resource types. These will typically match the services you wish to generate in Azure. IF you want to use a resource provider that isn’t registered, you can register it by running the cmdlet Register-AzureRmResourceProvider and state the provider namespace you want to register.  You can find more information about a provider by stating that specific namespace.

For example, you can run Get-AzureRmResource Provider –ProviderNamespacewhich will show you the RegistrationState, ResourceTypes, and locations.

To limit your search results to display just a list of supported locations, you can run ((Get-AzureRmResourceProvider –Provider Namespace).ResourcTypes | Where-Object ResourceTypeName –eq sites).Locations.

If you’re running this at home, you might see different results than at work. A network administrator for your company could have implemented a policy that limits the regions that can be used in the subscription, or there may be other restrictions related to your country for tax purposes.

Create a new resource group

A resource group provides an area or container for all of your solution’s resources that share the same lifecycle. To create one, run New-AzureRmResourceGroup

This cmdlet uses the Name to state a name for the resource group, and Location will state its location. For example, by running New-AzureRmResourceGroup –Name Test1 – Location “East US” you will now have a resource for the Eastern U.S. region.

Find the API versions for your resources

When using a template, you need to specify which API version you wish to use after you create a new resource. The API versions correlate to the versions of REST API functions that the resource provider deploys. Newer versions of REST API will be released as resource providers offer more features. This is why the version of the API you state in the template will affect the properties available as you create the template.

Now that you’ve had a glimpse into some tips and tricks for using PowerShell cmdlets in Azure, you can go on to create your templates and resource groups. Don’t forget to get the most current API versions for your resources.

With Azure, you can gain scalable and durable cloud storage, backup, and recovery solutions to satisfy all of your data needs. Backed by Agile IT’s superior technical expertise and excellent customer satisfaction, you can improve speed of access, maximize availability, minimize downtime, and much more. To learn more about how AgileDataCenter Azure can help you reap the full rewards of Azure, read on here!