Home > Software design >  Why is Az powershell returning this error on a simple "show policy definition" command?
Why is Az powershell returning this error on a simple "show policy definition" command?

Time:12-09

    PS /Users/myname> echo $name
    Deploy - Configure Log Analytics extension to be enabled on Windows virtual machines
    
    PS /Users/myname>  az policy definition show --name $name
    (InvalidPolicyDefinitionName) The policy definition name 'Deploy - Configure Log Analytics extension to be enabled on Windows virtual machines' is invalid. The policy definition name length must not exceed '64' characters.
    Code: InvalidPolicyDefinitionName
    Message: The policy definition name 'Deploy - Configure Log Analytics extension to be enabled on Windows virtual machines' is invalid. The policy definition name length must not exceed '64' characters.
 

The maximum length for a policy definition is not 64, but 128, infact the policy actually exists in Azure so it was defined and accepted by Azure. Why is pwsh complaining?

CodePudding user response:

I tried to reproduce the same in my environment and got the same error as below:

enter image description here

The error usually occurs if creating New Policy definition along with definition Name parameter value exceeds 64 characters.

I tried to create a Policy by using below command:

New-AzPolicyDefinition -Name 'VMPolicyDefinition' -DisplayName 'Deploy - Configure Log Analytics extension to be enabled on Windows virtual machines' -Policy '{"if":{"field":"type","equals":"Microsoft.Compute/virtualMachines"},"then":{"effect":"deny"}}'

enter image description here

In the Portal, Policy got created successfully like below:

enter image description here

Policy Definition Resource Name will be like below:

enter image description here

Note that: The policy definition Display Name has length limit of 128 characters and policy Definition Resource Name has the limit of 64 characters.

enter image description here

Reference:

Resource naming restrictions - Azure Resource Manager

  • Related