Category Archives: Azure

The new Az module – Connecting to Azure

With the introduction of the new Az PowerShell module, the merger of the Azure.* and AzureRM.* modules, comes a new way of connecting to Azure.

When we get a list of the available commands to do something with an AzAccount, you’ll end up with the following:

As you can see, there are now Connect-/Disconnect-AzAccount and Login-/Logout-AzAccount cmdlets. So if you want to connect to Azure and use PowerShell cmdlets to manage your environment, which one do you use?

If you use either Connect-AzAccount or Login-AzAccount, you’ll end up with the following message:

For this, one would require user interaction. Would that not negate the whole concept of automation?

One of our customers contacted me with the request if we could automate this. His idea was that we would write something that could read the url and code, utilize a browser and through that automate the login.

Although I love billing customers, I don’t like to bill them unnecessarily. I decided to educate them instead:

The solution is already available

The solution is simply by using the cmdlet the way it is intended to be used. For an interactive environment, you can simply go to that website and fill in the code. When you require the cmdlet to be used in an automated process / script, you can use the cmdlets’ parameters to tweak its behavior so that it works in automation

.
If you look at help of the cmdlets, you’ll notice that it has quite a few parameters that you can use. Amongst those is the -Credential parameter:

Big fat note:
This approach doesn’t work with Microsoft accounts or accounts that have two-factor authentication enabled.

But what if you’re using an account with Multi Factor Authentication?

Well, let me introduce you to Service Principals and Managed Identities.
Service principals are non-interactive Azure accounts. Like other user accounts, their permissions are managed with Azure Active Directory. By granting a service principal only the permissions it needs, your automation scripts stay secure.

If you want to know how you can create Azure Service Pricipals, take a look here.

Next to the Service Principal, the Connect-AzAccount cmdlet also requires you to provide its application ID, sign-in credentials, and the tenant ID associate with it:

Manage identities are a subset of Service Principals, and have therefor the same constraints.
They are assigned to resources that run in Azure. You can use them for sign-in, and acquire an app-only access token to access other resources. Managed identities are only available on resources running in an Azure cloud.


Working around Azure Tagging Limits – Using JSON formats.

Have you ever ran into the hard-limit in Azure for the amount of tags allowed on asingle resource, or resource group even?
When you work in a large organisation that wants to track everything this mightbe one of the things happening to you.

Let’s dig a bit into the actual limits of tagging currently in azure.
source: https://docs.microsoft.com/en-us/azure/azure-subscription-service-limits

  • Each resource or resource group can have a maximum of 15 tag name/value pairs
  • The tag name is limited to 512 characters
  • The tag value is limited to 256 characters.
  • For storage accounts, the tag name is limited to 128 characters, and the tag value is limited to 256 characters.
  • Tags can’t be applied to classic resources such as Cloud Services.
  • Tag names can’t contain these characters: <, >, %, &, \, ?, /

So this means we can have a Maximum of 15 tag name/value pairs on a resource/resource group.
This means if you want to tag for example: Owner, Team, Manager, CostCenter, Environment,BackupType, Expirationdate, MaintenanceWindow, etc. You will run out of those tags pretty quickly.

Luckily looking at the rest of the limitations: The tag value is limited to 256 characters! (that is a lot of characters!), and tag names cannot contain a few set of characters.

Since we don’t spot curly braces in the ‘cannotcontain’ list, why not start using JSON in as tag value’s to concatenate tags?

{
“Team”: “Solution Architects”,
“BackupType”: “FullBackup”,
“Manager”: “Danny den Braver”,
“ExpirationDate”: “None”,
“MaintenanceWindow”: {
“Days”: [
“Saturday”,
“Sunday”
],
“Hours”: “12:00-20:00”
},
“Environment”: “Development”,
“Owner”: “Danny den Braver”,
“CostCenter”: “12345”
}

Now let’sput this into practise on how we could do this leveraging powershell(specifically as I like splatting more than writing native JSON)

Let’sfirst build our hashtable and convert it to JSON

$ServerDetails = @{
Owner = ‘Danny den Braver’
Team = ‘Solution Architects’
Manager = ‘Danny den Braver’
CostCenter = ‘12345’
Environment = ‘Development’
BackupType = ‘FullBackup’
ExpirationDate = ‘None’
MaintenanceWindow = @{
Days = ‘Saturday’,’Sunday’
Hours = ’12:00-20:00′
}
}

$ServerDetailsJSON = $ServerDetails | convertto-json

Now we can add it as a tag value to ourenvironment

$Tags = @{
‘ServerDetails’ = $ServerDetailsJSON
}

Set-AzureRmResourceGroup -Name db-personal-rg-01 -Tag $Tags

Thisis what it will look like inside the portal:

Thisis what it will look like from PowerShell:

Hopefullythis will give you enough room for moving ahead using Tags within Azure.