1
+ function Invoke-ComplianceEvaluation {
2
+ <#
3
+ . SYNOPSIS
4
+ Function to trigger compliance evalution for Azure Policies on a specific Resource Group or Subscription
5
+ . description
6
+ The code assume you are already authenticated to azure
7
+ . example
8
+ # Load the function
9
+ . ./invoke-complianceevaluation
10
+ # Trigger Policy Compliance evaluation against current subscription
11
+ Invoke-ComplianceEvaluation
12
+ . example
13
+ # Load the function
14
+ . ./invoke-complianceevaluation
15
+ # Trigger Policy Compliance evaluation against specified subscription
16
+ Invoke-ComplianceEvaluation -subscriptionid <uid>
17
+ . example
18
+ # Load the function
19
+ . ./invoke-complianceevaluation
20
+ # Trigger Policy Compliance evaluation against specified resource group in the current subscription
21
+ Invoke-ComplianceEvaluation -ResourceGroupName MyRg
22
+
23
+ . example
24
+ # Load the function
25
+ . ./invoke-complianceevaluation
26
+ # Trigger Policy Compliance evaluation against specified resource group in the specified subscription
27
+ Invoke-ComplianceEvaluation -ResourceGroupName MyRg -subscriptionid <uid>
28
+
29
+ #>
30
+ param ($resourceGroupName , $subscriptionId )
31
+
32
+ if (-not $subscriptionId ){
33
+ $subscriptionId = (Get-AzContext ).subscription.id
34
+ }
35
+ $uri = " https://management.azure.com/subscriptions/$subscriptionId /providers/Microsoft.PolicyInsights/policyStates/latest/triggerEvaluation?api-version=2018年07月01日-preview"
36
+
37
+ if ($resourceGroupName ){
38
+ $uri = " https://management.azure.com/subscriptions/$subscriptionId /resourceGroups/$resourceGroupName /providers/Microsoft.PolicyInsights/policyStates/latest/triggerEvaluation?api-version=2018年07月01日-preview"
39
+ }
40
+
41
+ $azContext = Get-AzContext
42
+ $azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider ]::Instance.Profile
43
+ $profileClient = New-Object - TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient - ArgumentList ($azProfile )
44
+ $token = $profileClient.AcquireAccessToken ($azContext.Tenant.Id )
45
+ $authHeader = @ {
46
+ ' Content-Type' = ' application/json'
47
+ ' Authorization' = ' Bearer ' + $token.AccessToken
48
+ }
49
+ Invoke-RestMethod - Method Post - Uri $uri - UseBasicParsing - Headers $authHeader
50
+ }
0 commit comments