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

Ladi-1118 20 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,397 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

Accepted answer
  1. Pranay Reddy Madireddy 1,310 Reputation points Microsoft Vendor
    2024-12-26T16:33:56.9266667+00:00

    Hi Ladi-1118
    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

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

    Solution:
    Will describe here how my setup is: First the VM's are connected with Insight metrics and loganalytic workspace. Also on the monitoring Data collection rule has been connected and showing Monitored under -->Monitoring-->Insight-->Virtual Machines. Then with powershell save this script as ps1. under c:\Scripts or what ever folder you need to navigate. Script reads also from file C:\Scripts\Vm\VMNames.txt --> put your vm names in on each line per vm.

    Query is define to check for space logical disk when it fels under 10 percent checks every 15 minutes. I have pasted here as txt you need to change parameters as described inside the script.Alert_query_logicaldisk.txt

    Hope this helps to anyone that needs the Alert query with loganalytics with Powershell automation.

    If you have any other questions or are still running into more issues, please let me know. Thank you again for your time and patience throughout this issue.

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.