Powershell to create logsearch in bulk for azure vm's to check lowdiskspace

Ladi-1118 0 Reputation points
2024-12-11T19:56:05.9833333+00:00

Hello,

We have hundreds of Virtual machines on azure wanted to automate the script with PowerShell to create alert that checks the low space 95% for c and d. Manual process and query works just fine and we do get P1 alerts. But automatic this with PowerShell to create for 100 vm's has been a nightmare. Other metrics I was able to automate with PowerShell such as- CPU, Memory, Network etc. Our VM's are connected to workspace via Log Analytics Connection and we have Data Collection rule working.

I would love for this last peace to work with PW . Anyone that can help on creating a script that or any links on how to will be appreciated:

  1. Creates an Alert with bellow query.
  2. Sets the threshold, Frequency evaluation 1h i guess is acceptable
  3. Read from the file where VM's are stored in txt (its working with metrics)
  4. Set the Action group

Query:

InsightsMetrics

| where Origin == "vm.azm.ms"

| where Namespace == "LogicalDisk" and Name == "FreeSpacePercentage"

| extend Disk = tostring(todynamic(Tags)["vm.azm.ms/mountId"])

| summarize LogicalDiskSpacePercentageFreeAverage = avg(Val) by bin(TimeGenerated, 15m), Computer, _ResourceId, Disk

| where _ResourceId == '{VMResourceId}' and LogicalDiskSpacePercentageFreeAverage >= 95

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,396 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,708 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.