Azure

Using Azure Resource Manager

In the new Azure Portal you create all your resources in Resource Groups, there is also as part of the Azure SDK‘s a module called AzureResourceManager  by default the module loaded for the Azure SDK is AzureServiceManagement. A blurb from one of the Azure documentation page reads

The Azure and Azure Resource Manager modules are not designed to be used in the same Windows PowerShell session. To make it easy to switch between them, we have added a new cmdlet, Switch-AzureMode, to the Azure Profile module.

Azure Resource Manager Commands

To get a list of all commands that are available for the AzureResourceManager module you can run the command

Get-Command -Module AzureResourceManager | Get-Help | Format-Table Name, Synopsis

this will return:

NameSynopsis
—-——–
Add-AzureAccountAdds the Azure account to Windows PowerShell
Add-AzureEnvironmentCreates an Azure environment
Clear-AzureProfileClears an Azure profile
Disable-AzureSqlDatabaseDirectAccessDisables the option to directly access to an Azure Sql database (without auditing)
Disable-AzureSqlDatabaseServerDirectAccessDisables direct access to all Azure Sql databases that use the audit policy of a Sql databa…
Enable-AzureSqlDatabaseDirectAccessEnables the option to directly access to an Azure Sql database (with auditing)
Enable-AzureSqlDatabaseServerDirectAccessEnables direct access to all Azure Sql databases that use the audit policy of a Sql databas…
Get-AzureAccountGets Azure accounts that are available to Azure PowerShell.
Get-AzureADGroupFilters active directory groups.
Get-AzureADGroupMemberGet a group members.
Get-AzureADServicePrincipalFilters active directory service principals.
Get-AzureADUserFilters active directory users.
Get-AzureBatchAccount
Get-AzureBatchAccountKeys
Get-AzureDataFactoryGets information about Data Factory.
Get-AzureDataFactoryGatewayGets information about logical gateways in Data Factory.
Get-AzureDataFactoryHubGets information about hubs in Data Factory.
Get-AzureDataFactoryLinkedServiceGets information about linked services in Data Factory.
Get-AzureDataFactoryPipelineGets information about pipelines in Data Factory.
Get-AzureDataFactoryRunGets runs for a data slice of a table in Data Factory.
Get-AzureDataFactorySliceGets data slices for a table in Data Factory.
Get-AzureDataFactoryTableGets information about tables in Data Factory.
Get-AzureEnvironmentGets Azure environments
Get-AzureLocationGets the resource types and the Azure data center locations that support them.
Get-AzurePublishSettingsFileDownloads the publish settings file for an Azure subscription.
Get-AzureRedisCacheGets details about a single cache or all caches in the specified resource group or all cach…
Get-AzureRedisCacheKeyGets the accesskeys for the specified redis cache.
Get-AzureResourceGets Azure resources
Get-AzureResourceGroupGets Azure resource groups
Get-AzureResourceGroupDeploymentGets the deployments in a resource group.
Get-AzureResourceGroupGalleryTemplateGets resource group templates in the gallery
Get-AzureResourceGroupLogGets the deployment log for a resource group
Get-AzureRoleAssignmentFilters role assignments.
Get-AzureRoleDefinitionFilters role definitions.
Get-AzureSqlDatabaseAuditingPolicyGets an Azure Sql database’s auditing policy.
Get-AzureSqlDatabaseServerAuditingPolicyGets an Azure Sql server’s auditing policy.
Get-AzureSubscriptionGets Azure subscriptions in Azure account.
Get-AzureTagGets predefined Azure tags
Import-AzurePublishSettingsFileImports a publish settings file that lets you manage your Azure accounts in Windows PowerSh…
New-AzureBatchAccount
New-AzureBatchAccountKey
New-AzureDataFactoryCreates a data factory.
New-AzureDataFactoryEncryptValueEncrypts sensitive data.
New-AzureDataFactoryGatewayCreates a gateway for Data Factory.
New-AzureDataFactoryGatewayKeyCreates a gateway key for Data Factory.
New-AzureDataFactoryHubCreates a hub for Data Factory.
New-AzureDataFactoryLinkedServiceLinks a data store or a cloud service to Data Factory.
New-AzureDataFactoryPipelineCreates a pipeline in Data Factory.
New-AzureDataFactoryTableCreates a table in Data Factory.
New-AzureRedisCacheCreates a new redis cache.
New-AzureRedisCacheKeyRegenerates the access key of a redis cache.
New-AzureResourceCreates a new resource in a resource group
New-AzureResourceGroupCreates an Azure resource group and its resources
New-AzureResourceGroupDeploymentAdd an Azure deployment to a resource group.
New-AzureRoleAssignmentCreate a role assignment to some principals at a given scope.
New-AzureTagCreates a predefined Azure tag or adds values to an existing tag
Remove-AzureAccountDeletes an Azure account from Windows PowerShell.
Remove-AzureBatchAccount
Remove-AzureDataFactoryRemoves a data factory.
Remove-AzureDataFactoryGatewayRemoves a gateway from Data Factory.
Remove-AzureDataFactoryHubRemoves a hub from Data Factory.
Remove-AzureDataFactoryLinkedServiceRemoves a linked service from Data Factory.
Remove-AzureDataFactoryPipelineRemoves a pipeline from Data Factory.
Remove-AzureDataFactoryTableRemoves a table from Data Factory.
Remove-AzureEnvironmentDeletes an Azure environment from Windows PowerShell
Remove-AzureRedisCacheRemove redis cache if exists.
Remove-AzureResourceDeletes a resource
Remove-AzureResourceGroupDeletes a resource group.
Remove-AzureRoleAssignmentRemoves a role assignment.
Remove-AzureSqlDatabaseAuditingDisables an Azure Sql database’s auditing.
Remove-AzureSqlDatabaseServerAuditingDisables auditing of all the databases that rely on the auditing policy of the given databa…
Remove-AzureSubscriptionDeletes an Azure subscription from Windows PowerShell.
Remove-AzureTagDeletes predefined Azure tags or values
Resume-AzureDataFactoryPipelineResumes a suspended pipeline in Data Factory.
Save-AzureDataFactoryLogDownloads log files from HDInsight processing.
Save-AzureResourceGroupGalleryTemplateSaves a gallery template to a JSON file
Select-AzureSubscriptionChanges the current and default Azure subscriptions
Set-AzureBatchAccount
Set-AzureDataFactoryGatewaySets the description for a gateway in Data Factory.
Set-AzureDataFactoryPipelineActivePeriodConfigures the active period for data slices.
Set-AzureDataFactorySliceStatusSets the status of slices for a table in Data Factory.
Set-AzureEnvironmentChanges the properties of an Azure environment
Set-AzureRedisCacheSet redis cache updatable parameters.
Set-AzureResourceChanges the properties of an Azure resource.
Set-AzureResourceGroupChanges the properties of a resource group
Set-AzureSqlDatabaseAuditingPolicySets an Azure Sql database’s auditing policy.
Set-AzureSqlDatabaseServerAuditingPolicySets an Azure Sql database server’s auditing policy.
Set-AzureSubscriptionCreates or changes an Azure subscription
Stop-AzureResourceGroupDeploymentCancels a resource group deployment
Suspend-AzureDataFactoryPipelineSuspends a pipeline in Data Factory.
Switch-AzureModeSwitches between the Azure and Azure Resource Manager modules
Test-AzureResourceGroupTemplateDetects errors in a resource group template or template parameters
Use-AzureSqlDatabaseServerAuditingPolicyMarks an Azure Sql database as using its server’s auditing policy.

Step 0

As a step 0 lets open up everything we need.

Azure SDK

So to get started you need to install the Azure SDK which you can get from the SDK downloads page. I am using Azure SDK 2.5 version for this post.

PowerShell ISE

Open the PowerShell ISE using Win + R and then %WINDIR%\system32\WindowsPowerShell\v1.0\powershell_ise.exe. You can also use the standard PowerShell window if you want.

Azure Management Portal

Open and sign in to the Azure Management Portal (https://manage.windowsazure.com/)

Azure Portal

Open and sign in to the Azure Portal (http://portal.azure.com/)

Step 1

Let’s start by getting the Azure Management Portal pieces out the way. In the Azure Management Portal we will just be creating a new AAD user that we can use to automatically login through PowerShell. If you want to use a MSA just leave the credentials bit off in Step 3 and you will receive a prompt for credentials at which time you can use MSA or AAD credentials and you can now move to step 2. If you want to create the new user follow my other post Creating a new Azure Active Directory User to create a user for this demo.

Step 2

In the PowerShell ISE we will kick off by switching the Azure SDK to use the resource manager module.

Switch-AzureMode -Name AzureResourceManager

Step 3

After we have switched to the AzureResourceManager module we are able to use the commands that are part of it. Let’s start off by adding our Azure Account we just created using the snippet below (I keep my username and password in txt files and reference from multiple sample scripts for ease of use but you can place them straight in the script if you wanted

[string]$currentUsername = Get-Content "Z:\_PowerShell\Azure\currentUser.txt"
[string]$currentPassword = Get-Content "Z:\_PowerShell\Azure\currentPass.txt"
$secpasswd = ConvertTo-SecureString $currentPassword -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($currentUsername, $secpasswd)
Add-AzureAccount -credential $mycreds

This would then return the account added:

c113098e-cd29-4363-88ff-434801c16a04

At this point if you ran Get-AzureAccount it will show you this account and any others you have previously added.

18a18571-6497-46c7-9922-d01a5437222e

Step 4

Next we’ll explore some meta data for Step 5.

Getting a Resource Group Template

We can run the command:

Get-AzureResourceGroupGalleryTemplate

and it will return a list of all the current resources group templates that we can create. For the list at the time of writing this post you can refer to one of my GitHub Gists at https://binary-stuff.com/gist/ea0884f5ba00c62a83e4. We are going to be using one of the templates found around line 2220 which is the website and sql database.

a6489048-c54a-415a-a891-89ca37aedbf5

Get a list of Azure Resource Locations

When creating Azure Resources you need to specify were those resources are located geographically. Now you could guess or Bing what the locations are or you could use the handy command:

Get-AzureLocation

which will return you a list of every location that every resource is available in. This list as with the one above can be found as one of my GitHub Gists at https://binary-stuff.com/gist/87aad8bfbbbcd7421b83. From the long list of locations we are just going to use West Europe for all our resources:

Step 5

At this point we have all the info we need to create our resources using the resource manager in powershell so we’ll use the snippet:

$ResourceManagerTest = "ResourceManagerTest"
$AzureDataCenterLocation = "West Europe"
$administratorLoginPassword = ConvertTo-SecureString "$($ResourceManagerTest)DbP@ssw0rd" -AsPlainText -Force
New-AzureResourceGroup -Name "$($ResourceManagerTest)" `
                        -Location "$AzureDataCenterLocation" `
                        -GalleryTemplateIdentity Microsoft.WebSiteSQLDatabase.0.2.2-preview `
                        -siteName "$($ResourceManagerTest)Site" `
                        -hostingPlanName "$($ResourceManagerTest)Plan" `
                        -siteLocation "$AzureDataCenterLocation" `
                        -serverName "$($ResourceManagerTest.ToLowerInvariant())dbserver" `
                        -serverLocation "$AzureDataCenterLocation" `
                        -administratorLogin "$($ResourceManagerTest)DbLogin" `
                        -administratorLoginPassword $administratorLoginPassword `
                        -databaseName "$($ResourceManagerTest)DbName" `
                        -Verbose

This will then go off and create our website and database along with an Application Insights resource that we can use for our website when we deploy it. When the command finishes you should see an output similar to the one below which because we specified the -verbose flag tells us the status of each resource created:

41b95e80-0711-4b5a-aa4c-7e9d4a9050bb

At this point we really are finished with what the subject of the blog post is but we’ll continue on to explore what we have just created, how we would have had to create it using the Azure Portal and how we can remove the resource group using PowerShell.

Step 6

Open the Azure Portal. The first thing you should notice is that you already have a notification and when you open that it says that a deployment has succeeded.

eead9f51-919b-4c2b-bdf3-b9032f620e50

Clicking on the success notification will open that resource group:

cb674115-da32-480a-9eae-74bf1ff3dfd1

From here you can use the resources 100% as if you created them in the portal.

Step 7

Before we see how we would have had to create those resources manually let’s remove the resource group from our subscription. To do this we run the simple command below keeping in mind that the $ResourceManagerTest variable should still be set from the previous command in step 5:

Remove-AzureResourceGroup -Name $ResourceManagerTest -Force -Verbose

this will then proceed to remove the resource group, again because of the -verbose flag we don’t get as much info but rather just that it’s deleting the resource group and then comes back when it’s done.

a92edd54-adc5-42e4-943e-a4b965c5cdb0

Step 8

The last thing that I’ll show on this post is how we would of have to do this manually (or at least where to find template). Back in the Azure Portal click new in the bottom left corner and then click on Everything:

06e2e7b9-6b89-4673-a4a0-883c2807fd13

Next click on the Web category/section and then you’ll see the Website + SQL option which is what we created:

9da548b9-cded-43dd-a95b-3d5b54507e56

clicking on that option will give you a little info about the template and from here you’d just click on create:

bc7fc5a1-0ec6-41dc-a0a7-3c2a1309a6b2

From here you will configure the Resource Group Name, all the settings for your Website resource and SQL resource and then click create.

3bf2c1a8-e9ef-4c0b-ab48-50d6b8fca82c

When that process completes you will be in the sample place as that small PowerShell script gets you too :)

Thoughts and comments

If you have any thoughts or comments about any of the pieces of this post please do share below.

Reference: Using Azure Resource Manager from our NCG partner Gordon Beeming at the binary-stuff.com blog.

Gordon Beeming

Gordon is a Software Developer in Durban, South Africa working for Derivco. He is also a Visual Studio ALM Ranger and ALM MVP. Hew writes about anything that he feels might be useful to anyone in the community.

Related Articles

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button