I am trying to create our Azure Policies as code. To do this i have exported all of our Azure policies into json format, and then i which to upload/create the policies with an azure powershell script. The actual problems arises when i try to execute my powershell scripts through azure devops. But to simplefy the problem, i recreated the problem in in the azure cloud shell.
I have created a policy in the following location:
/home/sokr/clouddrive/deploy_azure_monitor_agent_extension_for_windows_VMs/policy.json
My azure cli script is located here: /home/sokr/clouddrive/policy_deployment.ps1
and it contains the following code:
`$managementGroup = "mg_sandbox"
New-AzPolicyDefinition -Name 'deploy_azure_monitor_agent_extension_for_windows_VMs' -managementGroup $managementGroup -Policy '/deploy_azure_monitor_agent_extension_for_windows_VMs/policy.json'
If i execute my script from path: /home/sokr/clouddrive/. Then everything works perfectly. But if I execute my script from another path like from /home/sokr/ with command ./clouddrive/policy_deployment.ps1 i get error:
New-AzPolicyDefinition: /home/sokr/clouddrive/policy_deployment.ps1:4
Line |
4 | New-AzPolicyDefinition -Name 'deploy_azure_monitor_agent_extension_fo …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Unexpected character encountered while parsing value: d. Path '', line 0, position 0.
New-AzPolicyDefinition: /home/sokr/clouddrive/policy_deployment.ps1:5
Obviously i need to change the path i defined within my azure cli script with currently is '\deploy_azure_monitor_agent_extension_for_windows_VMs\policy.json'. But No matter what is change it to i get the same error. Any help with this?
I have changed the path to anything like: '/clouddrive/deploy_azure_monitor_agent_extension_for_windows_VMs/policy.json'
'/home/sokr/clouddrive/cloud/drive/deploy_azure_monitor_agent_extension_for_windows_VMs/policy.json' and many many more. But i keep getting the same error
I have tried to reproduce the same as per your commands in azure cloud shell and got the same issue when I have given relative path to the policy.json file.
I have followed the below steps to resolve the issue.
Step 1: Go to the location where you have placed policy.json file and use Get-Location
command to get the full path as below to prepend this path to the policy.json file in the script.
Step 2: Create policy_deployment.ps1 in the desired path as shown in below screenshot.
Now I have executed this script from multiple paths to test and found no issues.