KQL query with regards to policies

Joel Pangilinan 30 Reputation points
2023-02-15T17:52:03.71+00:00

Hi, I'm trying to do a KQL query using Azure Resource Graph on policyresources and I'm having difficulty producing an output/csv that will show the following table below. I need to show all policies including policies under an initiative in the "Policy or Initiative Name" column. If you could please help crafting the KQL query needed for this.

Policy or Initiative Name | Definition Type (Policy or Initiative) | Assignment ID | Definition ID | Assigned by | Created when | Built-in or Custom | Scope

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
868 questions
{count} votes

3 answers

Sort by: Most helpful
  1. AnuragSingh-MSFT 21,361 Reputation points
    2023-02-22T08:51:01.3266667+00:00

    @Joel Pangilinan Thank you for posting the question. Based on the requirement in question the following query should help you

    PolicyResources 
    | where kind != 'policyassignments' //the Policy/Initiative assigned
    | extend definitionType = iff(kind == 'policysetdefinitions', 'Initiative','Policy') 
    | extend custom_Or_Builtin = iff(properties.policyType == 'Custom', 'Custom', 'BuiltIn')
    | project name = properties.displayName , definitionType, id, custom_Or_Builtin, createdDate  = iff(custom_Or_Builtin == 'Custom', properties.metadata.createdOn, properties.metadata.lastSyncedToArgOn)
    //Policy or Initiative has created data only for 'Custom' Policy. For 'BuiltIn', has lastSyncedToArgOn property denoting when was thepolicy synced to subscription
    | join kind=leftouter (PolicyResources | where kind == 'policyassignments' | project id= tostring(properties.policyDefinitionId), assignedBy = properties.metadata.assignedBy, assignedScope = properties.scope) 
    on id 
    | project-away id1
    
    

    Hope this helps. Please let me know if you have any questions.


  2. User A 0 Reputation points
    2023-11-14T21:29:52.51+00:00

    maybe this is good but in which table? reson for non compliance is found? eg version in 12 compared to expected 14? where the information on Policy dashboard is taken from? why is it that MS always sucks to give information what is where?

    0 comments No comments

  3. J.I. PRASHANDH 0 Reputation points
    2024-09-03T12:43:20.83+00:00

    Hello @AnuragSingh-MSFT

    I am checking on the query that you provided. But I am working on something that should list the Policy assignment , and its Scope, and its Type and then for each policy assignment I need the Policy definition Version, Category, Definition Type (Custom or Built In )

    0 comments No comments

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.