0
votes

While trying to create new policy definition using Azure powershell below exception is thrown, kindly let me know if there is error in cmdlet, or the definition file.

New-AzPolicyDefinition : Unexpected character encountered while parsing value: <. Path '', line 0, position 0. At line:1 char:15 + ... efinition = New-AzPolicyDefinition -Name "application-role-tag" -Disp ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [New-AzPolicyDefinition], JsonReaderException + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzurePolicyDefinitionCmdlet

I have used the below command, the policy definition and parameters definition is reference from from azure devops (git) repository,

$definition = New-AzPolicyDefinition -Name "application-role-tag" -DisplayName "Allow resource creation if Application Role tag value in allowed values" -description "Allows resource creation if the Application Role tag is set to one of the following values: webServer, appServer, data." -Policy 'https://company.visualstudio.com/DefaultCollection/Gullfoss/_git/Azure Governance?path=/Policies/Tags/application-role-tag/azurepolicy.rules.json' -Parameter 'https://company.visualstudio.com/DefaultCollection/Gullfoss/_git/Azure Governance?path=/Policies/Tags/application-role-tag/azurepolicy.parameters.json' -Mode Indexed

Policy definition file:

{
    "properties": {
      "displayName": "Allow resource creation if 'application role' tag value in allowed values",
       "policyType": "Custom",
       "mode": "Indexed",
       "description": "Allows resource creation if the 'application role' tag is set to one of the following values: webserver, appserver, data, gateway, messageBroker.",
       "parameters": {},
       "policyRule": {
          "if": {
             "not": {
                "field": "tags['applicationRole']",
                "in": [
                   "webServer",
                   "appServer"                  
                ]
             }
          },
          "then": {
             "effect": "audit"
          }
       }
    }
 }

Policy parameters definition:

{
    "parameters": {}
}
1

1 Answers

0
votes

I had wrongly referenced the webpage url instead of fetching the file content using DevOps REST API. The url to fetch file content is as below:

https://dev.azure.com/{organization}/{project}/_apis/sourceProviders/{providerName}/filecontents?repository={repository}&commitOrBranch={commitOrBranch}&path={path}&api-version=5.1-preview.1

{providerName}: tfsgit or tfvs {commitOrBranch}: Name of the branch, such as 'master'