Check Azure Compute Quota with Scheduled GitHub Action

Here’s how you can check your Azure compute quota with a scheduled GitHub Action workflow. This runs on a schedule but can also be run manually on a specific subscription.

name: Maintenance- Check Compute Quota
'on':
  workflow_dispatch:
    inputs:
      environment:
        description: Run on Subscription
        required: true
        default: PROD
        type: choice
        options:
          - NONPROD
          - PROD
  schedule:
  - cron: '0 3 * * 3' # Every Wednesday at 3 AM
jobs:
  Check_Quota:
    runs-on: ubuntu-latest
    environment: '${{ github.event.inputs.environment}}'
    name: Start Process
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Login to Azure
        uses: azure/login@v1
        with:
          creds: '${{secrets.Githubsecrets}}'
          enable-AzPSSession: true
      - name: Check Compute Quota
        id: task-check-ComputeQuota
        uses: azure/powershell@v1
        with:
          azPSVersion: latest
          errorActionPreference: stop
          inlineScript: |
            # List of regions to check. Change regions if Needed.
            $locations = @('westus', 'eastus', 'centralindia', 'westeurope')

            # Define an array to hold the results
            $results = @()
            $payload = @{
              NotificationType = "Teams"
              message = @()
            }
            # Loop through each location
            foreach ($location in $locations) {
                # Get VM usage for the location
                $usages = Get-AzVMUsage -Location $location

                # Loop through each usage
                foreach ($usage in $usages) {
                    if ($usage.Limit -gt 0) { 
                        $percentageUsed = ($usage.CurrentValue / $usage.Limit) * 100

               # Show only items that are in use and/or at or above 90% of the limit
                        if ($usage.CurrentValue -gt 0 || $percentageUsed -ge 90) {
                            $results += [PSCustomObject]@{
                                Resource       = $usage.Name.LocalizedValue;
                                Location       = $location;
                                Used           = $usage.CurrentValue;
                                Limit          = $usage.Limit;
                                UsagePercent  = [math]::Round($percentageUsed, 2);
                            }
                        }
                    }
                }
            }
            
            # Display results as a table
            $results | Format-Table -AutoSize
Share or Save this:
Share