In Part 1, we looked at creating an Azure Function, assigning it a Managed Service Identity (MSI) and assigning the MSI RBAC Permissions. We then look through the default PowerShell code that is created when creating a PowerShell Function in order to better understand how to code an Azure PowerShell Function.
In this second part, we take a look at updating the code in our PowerShell Function to build a report on Azure Virtual Machine Quota limits for a specific region and color code cells above a certain threshold. We will then create our Logic App to call our Function and take the HTML Results from the Function and send it in an e-mail whether that may be daily, weekly, monthly, etc.
Part 1 – Create Azure Function App, create a Managed Service Identity, Assign RBAC Permissions, and learn how to modify the default PowerShell Function code.
Part 2 – Adding PowerShell code to our Function and Creating a Logic App to call the Function and send the HTML Output to your e-mail.
Coding our PowerShell Function
The code can be downloaded by clicking the Download button.
Let’s walk through what some of the areas of the code are. I won’t explain it in detail but rather what the various pieces do.
The function Set-CellColor was written by a gentleman named Martin Pugh which provided this code here. The purpose of this code is to pass a filter to the function (i.e. if threshold is above 80, highlight the cell or row as the defined color).
The function Get-AZQuotaUsageHTML does the following:
- Defines the HTML Style
- Defines the HTML Body
- Pulls data on Azure Virtual Machine Quota Usage. It takes this data, converts it to HTML, checks the threshold value and calls the Set-CellColor to color cells where the percentage of Quota Usage is above the defined threshold, and builds the header, body and Virtual Machine Quota results.
The code outside the body checks the following:
- Value of Threshold confirming the value is between 1 and 100. If not, submit an HTTP Bad Request back to the requesting client informing the requesting HTTP client that the Threshold value is not between 1 and 100.
- Value of Location is a valid Azure Region. If not, submit an HTTP Bad Request back to requesting client informing the requesting HTTP client that the Location submitted is not a valid Azure Region.
If Threshold is between 1 and 100 and the Location submitted is a valid Azure Region, the Get-AZQuotaUsageHTML function is called and the HTML Report is submitted back to the requesting HTTP client.
Running our PowerShell Function
Take the code and replace the default PowerShell Function Code.

Click the button “Save and run.” Enter the following JSON code into the HTTP Body. We’re going to check the errors first. Let’s start by putting in an invalid threshold and an invalid Azure Region.

Now let’s change the Threshold Value to an acceptable range between 1 and 100. Let’s set it to 60.

Now that our Threshold is an acceptable value between 1 and 100, we can see that it now errors on the Location. That is because northcentral is not a name of an Azure Region. What we meant to put in was northcentralus. So let’s change it to northcentralus and run the function again.

There we go! We finally get a status of 200 OK and we can see our HTML Results. Now let’s go create our Logic App so we can run this code daily, weekly, or even monthly, obtain the HTML Results, and send out an e-mail with the HTML Results.
Building our Logic App
We will leverage Logic Apps to run at at
To create the LogicApp, go to All Services, search for logic apps, and click Logic Apps.

Choose Add to create a new Logic App.

When creating the Logic App, ensure you are specifying the same location as your Azure Function. I also elect to place it into the same Resource Group as the Function App to ensure latency is minimal as well to ensure there are no unnecessary egress costs from one region to another.

We will want to choose the Recurrence trigger when creating our Logic App. Recurrence allows us to run our Logic App at certain time intervals. For example, hourly, daily, weekly, monthly, etc…

We will select 1 for the internal with the frequency as month so the Logic App runs once every month. Feel free to change it as you see fit.

Add a new action and search for the word function. Choose Azure Functions.

Choose our HTMLReportFunction Function App.

Select the HTTPTrigger1 Function within our HTMLReportFunction Function App.

For the Request Body, add the HTTP Request Body using JSON. As was shown before, we are using the Location parameter to represent the Azure Region we want quota limit data for. And we are specifying the Threshold for the Virtual Machine CPU Core percent used. For example, if you have 10 cores available and you have used 6 out of 10 CPU cores, the threshold would be met. The PercentUsed Column in the HTTP report would be highlighted as red.

Add a new action and search for the word Office 365 Outlook. Choose Office 365 Outlook.

Scroll down or search for Send an email and select it.

You will be prompted to sign in. Go ahead and sign in with the Office 365 Outlook E-mail Account you want to leverage to send e-mails from.

Specify the To Address you want e-mails delivered to. Specify the Subject. And for the Body, on the right under HttpTrigger1 function, click see more.

Click Body. This means that the Body response we retrieve from the Azure Function will be used for the Body of the e-mail.

And since the Body of the Function Response is formatted in HTML, we need to click Add New Parameter and choose “Is HTML”

Then we need to choose Yes

Ultimately, our Send an email will look like the following

Save your Logic App near the top left.
Running our Logic App
Now we have our Logic App Saved. Click Run.

Success! All green checkmarks. You can click on either Recurrence, HttpTrigger1, or Send an email to get more details as to what occured.
The thing I really want to see is the e-mail! Let’s head to the e-mail account that was defined in the To: field.
And voila, there’s the e-mail! You’ll see I only have 1 CPU core running in northcentralus.

Testing out the Threshold Color Coding.
I’m creating a Virtual Machine that has 8 CPU cores. Our threshold is defined as 60% so I want to choose a SKU that will put me above the 60% threshold. Because my limit is 10 CPU cores, I need at least 6 total Regional vCPUs to be active in the northcentralus region for the PercentUsed to be colored red.
This is the SKU/configuration I used to put me above the 60% threshold for PercentUsed.

Now that our Virtual Machine is created, let’s go ahead and click the Run button on our Logic App. That is unless you want to wait for 1 month until the Logic App runs again to see. No? Didn’t think so. Click the Run button.

Another success! This time it only took 7 seconds rather than 6 seconds for the first run. This function runs relatively quick even on Consumption Plan.

This time, you can see that the PercentUsed cell is color coded. The highest PercentUsed cells are also sorted with the highest percentages on top. I had to do some specific PowerShell code to make the sort happen even with the % symbol in there. If you’re curious, this portion of the code is what allows for that to happen:
sort-object { [INT]($_.percentused -replace '%') } -descending
I force percentused to be an integer value by using [INT] and choose to replace the % symbol with nothing. I then add -descending.
Thanks for reading to the end. Hopefully this helps you folks out with your PowerShell Functions. And feel free to comment if you have any questions.
Leave a Reply