Azure

Print Azure WebApp Metrics in PowerShell Console as table

Here is the script:

$WebAppName = Read-Host “Please enter the WebApp Name that the Website(s) belongs to”
$Days= Read-Host “Please enter Last #Days needed to be checked in the report”
$WebApp= Get-AzWebApp -Name $WebAppName
$RgName=$WebApp.ResourceGroup

$ResourceNames = Get-AzResource -ResourceGroupName $RgName -ResourceType “Microsoft.Web/sites”

foreach ($ResourceName in $ResourceNames)
{
$WebappName = $ResourceName.Name
$URLName=(Get-AzWebApp -Name $WebappName).DefaultHostName

inputs

$WarningPreference = ‘SilentlyContinue’
$TimeGrain = [timespan]::FromDays(1)

$Start = [datetime]::Now.AddDays(-$Days)
$End = [datetime]::Now

# capture resource metrics
$Resource = Get-AzResource -ResourceName $WebappName -ResourceGroupName $RgName -ResourceType “Microsoft.Web/sites”
$ResourceID = $Resource.ResourceId

$MetricName = ‘Requests’
$Splat = @{
ResourceId = $ResourceID
MetricName = $MetricName
TimeGrain = $TimeGrain
StartTime = $Start
EndTime = $End
}
$Data = Get-AzMetric @Splat
$Datpoints = $data.data.average.foreach({[int]$_})
Write-Output $URLName
Write-Output $MetricName
Write-Output $data.data | format-table TimeStamp, Total -AutoSize

$MetricName = ‘BytesReceived’
$Splat = @{
ResourceId = $ResourceID
MetricName = $MetricName
TimeGrain = $TimeGrain
StartTime = $Start
EndTime = $End
}
$Data = Get-AzMetric @Splat
$Datpoints = $data.data.average.foreach({[int]$_})
Write-Output $URLName
Write-Output $MetricName
Write-Output $data.data | format-table TimeStamp, Total -AutoSize

$MetricName = ‘BytesSent’
$Splat = @{
ResourceId = $ResourceID
MetricName = $MetricName
TimeGrain = $TimeGrain
StartTime = $Start
EndTime = $End
}
$Data = Get-AzMetric @Splat
$Datpoints = $data.data.average.foreach({[int]$_})
Write-Output $URLName
Write-Output $MetricName
Write-Output $data.data | format-table TimeStamp, Total -AutoSize

}