Powershell script giving error - kusto query

Azuretech 90 Reputation points
2023-02-14T16:45:53.1966667+00:00

I am trying to use below query to get the patch status of vm patched through update management center .

it's working fine in resource graph query but when trying to run using powershell script.

it's giving error on 5th line.

| parse id with * 'Machines/' resourceName '/patchInstallationResults/' *

Search-AzGraph: A positional parameter cannot be found that accepts argument '* Machines/ resourceName /patchInstallationResults/ * '.

below is the complete query. please let me know how to use 5th line for powershell script


$result = Search-AzGraph  -Query 'patchinstallationresources 
| where type =~ "microsoft.compute/virtualmachines/patchinstallationresults" 
| where properties.lastModifiedDateTime > ago(1d )
| extend InstallationStatus = tostring(properties.status)
| parse id with * 'Machines/' resourceName '/patchInstallationResults/' *
| project    resourceName, InstallationStatus
| join kind = leftouter 
(resources
| where type == 'microsoft.compute/virtualmachines'
| extend vmsid = properties.vmId
| project vmsid , name
)
on $left.resourceName == $right.name
| project vmsid, resourceName, InstallationStatus'

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,509 questions
Azure Update Manager
Azure Update Manager
An Azure service to centrally manages updates and compliance at scale.
304 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 54,316 Reputation points
    2023-02-14T17:00:57.0266667+00:00

    Your query string parameter is wrapped in single quotes. Inside the single quotes you are using single quotes again so the compiler sees the single quote on the 'Machines section as the end of the string followed by Machines. If you need to use single quotes inside a string then use double quotes around the outer string. Alternatively escape the single quotes inside the string so it is seen as single quotes.

    $result = Search-AzGraph  -Query "patchinstallationresources 
    | where type =~ 'microsoft.compute/virtualmachines/patchinstallationresults' 
    | where properties.lastModifiedDateTime > ago(1d )
    | extend InstallationStatus = tostring(properties.status)
    | parse id with * 'Machines/' resourceName '/patchInstallationResults/' *
    | project    resourceName, InstallationStatus
    | join kind = leftouter 
    (resources
    | where type == 'microsoft.compute/virtualmachines'
    | extend vmsid = properties.vmId
    | project vmsid , name
    )
    on $left.resourceName == $right.name
    | project vmsid, resourceName, InstallationStatus"
    

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.