Azure Data Services
5 TopicsPowershell 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 usingGet-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\$checklistname\VM-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\$checklistname\VMUsage.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.208Views0likes0Commentspowershell: how to remove a calendar for many users?
Hi all, I've hundreds users and they should remove a calendar (having the same name for each user) from their outlook. No way to ask them to go into outlook doing right click - remove... Any chances to go through powershell commands?858Views0likes3CommentsInstall-Module MSOnline - not working
Hi all, I am struggling to install:Install-Module MSOnline I get: I have tried: Get-PSRepository WARNING: Unable to find module repositories. When I run:Register-PSRepository -Default I get no error but when trying to runGet-PSRepository again i getWARNING: Unable to find module repositories. Any ideas on how to fix this?Solved20KViews0likes1CommentRemove Metadata from MMD Column with Powershell
Trying to remove a managed metadata value from a file via Powershell - there are a lot of resources online discussing PnP Powershell and how to manipulate metadata, that is, set metadata, edit metadata, and also creating a column default value from term store metadata. However, I have the situation that I need to "remove" metadata, and leave the file's particular column with an "unassigned" state, not the word "Unassigned" of course, but just EMPTY. Which is considered, "unassigned". If you're interested in why I need this, it is because we wrote a powershell runbook that looks for moved files - then makes sure the metadata is correct. If the file was moved from a specific folder depth in the library to depth - 1, I have to set that deepest folder to "unassigned". Currently working with a terrible workaround/cludge-fix that uses a special metadata term "_" - ugly. Please help1.1KViews0likes0Comments