Creating a workbook tile using KQL which utilises parameters – in this case VirtualMachines.
If I run the following with only 1 VM selected in the paramater, the results are displayed. If I select 2 or more VMs there is an error explaining the results could not be parsed, with the error output showing VM1, VM2.
Delimiter configured for the parameter is ,
How could I correct the following query to work with multiple VirtualMachine parameter selections?
InsightsMetrics
| where Origin == "vm.azm.ms"
| where Namespace == "LogicalDisk" and Name == "FreeSpacePercentage"
| extend Disk=tostring(todynamic(Tags)["vm.azm.ms/mountId"])
| extend vmName = tostring(toupper(Computer))
| extend VM_Name = tostring(split(vmName,'.')[0])
| summarize AggregatedValue = avg(Val) by VM_Name, Disk, _ResourceId
| where VM_Name in '{VirtualMachines:label}'
| top 30 by AggregatedValue asc
Thanks
3
Answers
Thanks everyone for your help. I achieved this by using the following:
Thanks to
Craig
for suggesting the same.You should replace
["VM1", "VM2"]
with the actual parameter name that contains the selectedVMs
.Method : 1
Output:
Method 2:
To make the query work with multiple
VirtualMachine
parameter selections, Instead of using thein
operator, you should use thein
operator along with thedynamic
function to construct the appropriate dynamic expression.By using the
in (selectedVMs)
expression, the query will filter the results based on theVM names
present in theselectedVMs
array.Output:
FYI: For single value parameters in workbooks, the value of a parameter is just text, so you need to wrap it in quotes inside the query text
so if
VirtualMachines
is a single value param, you’d use it like this in a query:which then turns into
at query time.
HOWEVER, when using multi value dropdown params, the parameter settings has options for what to use to for quote (
'
by default) and delimiter (,
) by default, so you have to use it like this:which at query time gets resolved into
for multi value params, especially if optional, the best way to do this is with a let:
because then you can use it like
which will be syntactically valid even if there’s no machines selected
(so you could do like
|where array_length(machines)==0 or machine in (machines)
to allow ALL machines if nothing is selected