Powershell to create logsearch in bulk for azure vm's to check lowdiskspace
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:
- Creates an Alert with bellow query.
- Sets the threshold, Frequency evaluation 1h i guess is acceptable
- Read from the file where VM's are stored in txt (its working with metrics)
- 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