Home > Mobile >  separating the output with title
separating the output with title

Time:03-05

I have customized a script so that 3 MetricName ‘s can be pulled with this script which is happening fine. But problem is that output will provide their values in a list (as attached here) so it is difficult make out which value is meant for which MetricName. Can some one please help me here so that the output values will have a title so that I can easily identify the metrics with correspondent values.

$metric =
Get-AzMetric -ResourceId "/subscriptions/111111-3333-4444-666666/resourceGroups/ad-fg-ggproviders/Microsoft.Sql/servers/server/databases/db01" -MetricName @('CPU_used','cpu_percent','deadlock') -DetailedOutput

$metric.Data

enter image description here

CodePudding user response:

You need to map the metric names back to the corresponding output in the data list by index.

I'd suggest writing a small function to handle that:

function Get-AggregateAzMetric
{
  param(
    [string]$ResourceId,
    [string[]]$MetricName
  )

  $metric = Get-AzMetric @PSBoundParameters -DetailedOutput

  # prepare dictionary to hold properties that we want the output object to have
  $outputProperties = [ordered]@{
      TimeStamp = $metric.Data[0].Timestamp
  }

  for($i = 0; $i -lt $MetricName.Length; $i  ){
    # Grab the metric name from the input list - map to the corresponding metric value
    $outputProperties[$MetricName[$i]]  =  $metric.Data[$i].Average
  }

  # convert the properties we've collected to a custom object
  [pscustomobject]$outputProperties
}

Once you've defined the above function, you can do:

Get-AggregateAzMetric -ResourceId "/subscriptions/111111-3333-4444-666666/resourceGroups/ad-fg-ggproviders/Microsoft.Sql/servers/server/databases/db01" -MetricName @('CPU_used','cpu_percent','deadlock')

Since the resulting object will only have 4 properties (Timestamp, CPU_used, cpu_percent, and deadlock), PowerShell will default to show it in a table view.

For objects with more than 4 visible properties, you can also use Format-Table to explicitly request table view formatting:

$aggMetric = Get-AggregateAzMetric -MetricName @('CPU_used','cpu_percent','deadlock','log_write_percent') -ResourceId "/subscriptions/111111-3333-4444-666666/resourceGroups/ad-fg-ggproviders/Microsoft.Sql/servers/server/databases/db01"

# this will default to a list view
$aggMetric |Out-Default

# but you can request a different format if you want to
$aggMetric |Format-Table
  • Related