using namespace System.Net # Input bindings are passed in via param block. param($Request, $TriggerMetadata) # Set-CellColor Function provided by Martin Pugh @ https://community.spiceworks.com/scripts/show/2450-change-cell-color-in-html-table-with-powershell-set-cellcolor Function Set-CellColor { [CmdletBinding()] Param ( [Parameter(Mandatory,Position=0)] [string]$Property, [Parameter(Mandatory,Position=1)] [string]$Color, [Parameter(Mandatory,ValueFromPipeline)] [Object[]]$InputObject, [Parameter(Mandatory)] [string]$Filter, [switch]$Row ) Begin { Write-Verbose "$(Get-Date): Function Set-CellColor begins" If ($Filter) { If ($Filter.ToUpper().IndexOf($Property.ToUpper()) -ge 0) { $Filter = $Filter.ToUpper().Replace($Property.ToUpper(),"`$Value") Try { [scriptblock]$Filter = [scriptblock]::Create($Filter) } Catch { Write-Warning "$(Get-Date): ""$Filter"" caused an error, stopping script!" Write-Warning $Error[0] Exit } } Else { Write-Warning "Could not locate $Property in the Filter, which is required. Filter: $Filter" Exit } } } Process { ForEach ($Line in $InputObject) { If ($Line.IndexOf("
The following report was run on 10/07/2019 23:02:42.
"@ # Get Azure VM Usage Quota information $AZVMUsage = Get-AzVMUsage -Location $Location | Select-Object @{Name = 'Name';Expression = {"$($_.Name.LocalizedValue)"}},CurrentValue,Limit,@{Name = 'PercentUsed';Expression = {"{0:P2}" -f ($_.CurrentValue / $_.Limit)}} | sort-object { [INT]($_.percentused -replace '%') } -descending # If the percent used is above the threshold defined, color the cell red. $AZVMUsageHTML = $AZVMUsage | ConvertTo-Html | Set-CellColor percentused red -Filter "percentused -ge $Threshold" # Combine HTML elements for output $Header + $Body + $AZVMUsageHTML } # Write to the Azure Functions log stream. Write-Host "PowerShell HTTP trigger function processed a request." # Check if Threshold is passed. # The threshold defines at what % of Capacity Used the HTML Cell is colored red $Threshold = $Request.Query.Threshold if (-not $Threshold) { $Threshold = $Request.Body.Threshold } # Check if Threshold exists. If not, 400 HTTP code is passed back to client. # If threshold was passed, verify the value is between 1 and 100. # If not between 1 and 100, send 400 HTTP code back specifying the value must be between 1 and 100. if ($Threshold) { if (-not([int]$Threshold -in 1..100)) { $status = [HttpStatusCode]::BadRequest $Body = "The Threshold value of $Threshold is not an acceptable threshold value. Please specify a number between 1 and 100 and try again." Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ StatusCode = $status Body = $Body }) exit } } else { $status = [HttpStatusCode]::BadRequest $Body = "Please pass a threshold value between 1 and 100 on the query string or in the request body." Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ StatusCode = $status Body = $Body }) } # Check if Location is passed for the region. # The location defines the region name that will used when checking VM Quota Usage. $Location = $Request.Query.Location if (-not $Location) { $Location = $Request.Body.Location } # Check if Location exists. If not, 400 HTTP code is passed back to client. # If location as passed, verify the region exists. # If location passed is not a valid Azure Region, send 400 HTTP code back spefiying the location is not a valid Azure Region. if ($Location) { $CurrentAZRegions = Get-AZLocation | Select-Object Location if ($CurrentAZRegions.Location -contains $Location) { # If Location passed is a valid Azure Region, run the Get-AZQuotaUsageHTML function to start collecting Quota Usage. $HTML = Get-AZQuotaUsageHTML $status = [HttpStatusCode]::OK Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ headers = @{'content-type' = 'text/html'} StatusCode = $status Body = $HTML }) } else { $status = [HttpStatusCode]::BadRequest $Body = "$Location is not a valid Azure Region. Please specify a valid Azure Region and try again." Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ StatusCode = $status Body = $Body }) exit } } else { $status = [HttpStatusCode]::BadRequest $Body = "Please pass a location on the query string or in the request body." Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ StatusCode = $status Body = $Body }) exit }