templates/CapacityMonitor/run.ps1

# Input bindings are passed in via param block.
param($Timer)

# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()

# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"


# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

$AzContext = Get-AzContext
$KeyVaultName = $env:APPSETTING_KEYVAULT_NAME
$DefaultUtilizationThreshold = 80
if ($env:APPSETTING_DEFAULT_UTILIZATION_THRESHOLD) {
    $DefaultUtilizationThreshold = [int]($env:APPSETTING_DEFAULT_UTILIZATION_THRESHOLD)
}
$SubscriptionId = $AzContext.Subscription.Id
Write-Host "Using Azure subscription: $SubscriptionId"

. $PSScriptRoot/Utils.ps1

Write-Host "Checking CBS capacity utilization..."

$AuthHeader = Get-AzureAuthHeader -AzContext $AzContext
$CBSArrays = Connect-CBSArray -KeyVaultName $KeyVaultName -DefaultUtilizationThreshold $DefaultUtilizationThreshold
Start-UpgradeCheck -CBSArrays $CBSArrays -AuthHeader $AuthHeader