Powershell Script to extract Azure VM Metrics data
Hi Community, hope you are doing well.
I am currently playing around with powershell scripting and trying to extract Azure VM utilization data using Get-AzMetric powershell module. I am trying to extract VM metrics through my script for all VMs in my current subscription (free trial) and outputting the same to a csv file. I can see the data getting extracted when I run in console but when I run the script I am unable to see the data getting outputted to my csv file. Please find below my script:
# Modules importation
#$modules = ‘Az.Accounts’,’Az.Compute’, ‘Az.Reservations’ , ‘Az.Storage’ , ‘Az.Billing’ ,’Az.BillingBenefits’ ,’Az.Monitor’,’Az.ResourceGraph’, ‘Join-Object’ ,’PSExcel’ ,’Az.Resources’, ‘Az.CostManagement’,’ImportExcel’ # PS Module required
#Install-Module -Name $modules -Scope CurrentUser -Force
#Powershell-5.1
# Suppress breaking changes
Set-Item Env:SuppressAzurePowerShellBreakingChangeWarnings “true” # Connect to Azure
Connect-AzAccount
# Name of the analyze
[void][Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’)
$title = ‘ Azure VM Usage’
$msg = ‘Please enter the name of the analyze:’
$checklistname = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
# Subscription(s) selection – CTRL & click to select more than 1 subscription
$subquery = (Get-AzSubscription | Out-GridView -Title “Select an Azure Subscription” -PassThru)
$sub = $subquery.Id
Write-Host “Subscription(s) selected: $sub” -ForegroundColor Green
# Creation of the directroy
New-Item -Path “c:” -Name “Azurecost$checklistname” -ItemType “directory” -force
set-location c:azurecost$checklistname
#$csvFileVM = New-Object System.IO.StreamWriter(“c:azurecost$checklistnameVM-Usage.csv”)
#$csvFileVM.WriteLine(“Name, Id, ResourceGroup, MaxCPU”)
foreach ($subscription in $sub) {
# Set the subscription context
Set-AzContext -Subscription $subscription
$vms = Get-AzVM
$vmUtilizationData = @()
# Loop through each VM to get utilization metrics
foreach ($vm in $vms) {
$vmName = $vm.Name
$resourceId = $vm.Id
$Resourcegroup = $vm.ResourceGroupName
# Get metrics for the VM (e.g., CPU Percentage)
$metric = Get-AzMetric -ResourceId $resourceId -MetricName “Percentage CPU” -TimeGrain 12:00:00 -StartTime (Get-Date).AddDays(-3) -EndTime (Get-Date)
$MaxCPU = $metric.data.maximum | Measure-Object -Maximum | Select-Object -property Maximum
#$csvFileVM.WriteLine(” $MaxCPU”)
$vmUtilizationData += [PSCustomObject]@{
VMName = $vmName
ResourceGroup = $Resourcegroup
MaxCPU = $MaxCPU
}
}
}
$vmUtilizationData | Export-Csv -Path “c:Azurecost$checklistnameVMUsage.csv” -NoTypeInformation
Write-Host “Your script has finished running.”
pause
Hi Community, hope you are doing well. I am currently playing around with powershell scripting and trying to extract Azure VM utilization data using Get-AzMetric powershell module. I am trying to extract VM metrics through my script for all VMs in my current subscription (free trial) and outputting the same to a csv file. I can see the data getting extracted when I run in console but when I run the script I am unable to see the data getting outputted to my csv file. Please find below my script: # Modules importation
#$modules = ‘Az.Accounts’,’Az.Compute’, ‘Az.Reservations’ , ‘Az.Storage’ , ‘Az.Billing’ ,’Az.BillingBenefits’ ,’Az.Monitor’,’Az.ResourceGraph’, ‘Join-Object’ ,’PSExcel’ ,’Az.Resources’, ‘Az.CostManagement’,’ImportExcel’ # PS Module required
#Install-Module -Name $modules -Scope CurrentUser -Force
#Powershell-5.1
# Suppress breaking changes
Set-Item Env:SuppressAzurePowerShellBreakingChangeWarnings “true” # Connect to Azure
Connect-AzAccount
# Name of the analyze
[void][Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’)
$title = ‘ Azure VM Usage’
$msg = ‘Please enter the name of the analyze:’
$checklistname = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
# Subscription(s) selection – CTRL & click to select more than 1 subscription
$subquery = (Get-AzSubscription | Out-GridView -Title “Select an Azure Subscription” -PassThru)
$sub = $subquery.Id
Write-Host “Subscription(s) selected: $sub” -ForegroundColor Green
# Creation of the directroy
New-Item -Path “c:” -Name “Azurecost$checklistname” -ItemType “directory” -force
set-location c:azurecost$checklistname
#$csvFileVM = New-Object System.IO.StreamWriter(“c:azurecost$checklistnameVM-Usage.csv”)
#$csvFileVM.WriteLine(“Name, Id, ResourceGroup, MaxCPU”)
foreach ($subscription in $sub) {
# Set the subscription context
Set-AzContext -Subscription $subscription
$vms = Get-AzVM
$vmUtilizationData = @()
# Loop through each VM to get utilization metrics
foreach ($vm in $vms) {
$vmName = $vm.Name
$resourceId = $vm.Id
$Resourcegroup = $vm.ResourceGroupName
# Get metrics for the VM (e.g., CPU Percentage)
$metric = Get-AzMetric -ResourceId $resourceId -MetricName “Percentage CPU” -TimeGrain 12:00:00 -StartTime (Get-Date).AddDays(-3) -EndTime (Get-Date)
$MaxCPU = $metric.data.maximum | Measure-Object -Maximum | Select-Object -property Maximum
#$csvFileVM.WriteLine(” $MaxCPU”)
$vmUtilizationData += [PSCustomObject]@{
VMName = $vmName
ResourceGroup = $Resourcegroup
MaxCPU = $MaxCPU
}
}
}
$vmUtilizationData | Export-Csv -Path “c:Azurecost$checklistnameVMUsage.csv” -NoTypeInformation
Write-Host “Your script has finished running.”
pause Please help me understand what am I missing here since other details like VM name, Resource group name are getting outputted to my csv file through this script except the metric values. Read More