question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New-AzPolicyDefinition: Length cannot be less than zero. (Parameter 'length')

See original GitHub issue

Getting the above error when running the deploy from PowerShell 7.2.0 preview 5. PowerShell 7.1.3 works as expected.

  1. Installed PowerShell 7.2.0 preview 5
  2. Logged into Azure
  3. Selected my subscription.
  4. Tried to deploy the new Azure Policy Definition.
New-AzPolicyDefinition -Name "allowed-locations3" `
    -DisplayName "Allowed locations3" `
    -Policy 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/allowed-locations/azurepolicy.rules.json' `
    -Parameter 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/allowed-locations/azurepolicy.parameters.json' `
    -Mode Indexed

Expected behavior


 ~\Desktop >  New-AzPolicyDefinition -Name "allowed-locations3" `
    -DisplayName "Allowed locations3" `
    -Policy 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/allowed-locations/azurepolicy.rules.json' `
    -Parameter 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/allowed-locations/azurepolicy.parameters.json' `
    -Mode Indexed

Name               : allowed-locations3
ResourceId         : /subscriptions/XXXXXX-XXXX-XXXX-XXXX-XXXXXX/providers/Microsoft.Authorization/policyDefinitions/allowed-locations3
ResourceName       : allowed-locations3
ResourceType       : Microsoft.Authorization/policyDefinitions
SubscriptionId     : XXXXXX-XXXX-XXXX-XXXX-XXXXXX
Properties         : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy.PsPolicyDefinitionProperties
PolicyDefinitionId : /subscriptions/XXXXXX-XXXX-XXXX-XXXX-XXXXXX/providers/Microsoft.Authorization/policyDefinitions/allowed-locations3

Actual behavior


C:\Program Files\PowerShell\7-preview> New-AzPolicyDefinition -Name "allowed-locations3" `
>>     -DisplayName "Allowed locations3" `
>>     -Policy 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/allowed-locations/azurepolicy.rules.json' `
>>     -Parameter 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/allowed-locations/azurepolicy.parameters.json' `
>>     -Mode Indexed
New-AzPolicyDefinition: Length cannot be less than zero. (Parameter 'length')

C:\Program Files\PowerShell\7-preview>  Get-Error

Exception             :
    Type       : System.ArgumentOutOfRangeException
    Message    : Length cannot be less than zero. (Parameter 'length')
    ParamName  : length
    TargetSite :
        Name          : HandleException
        DeclaringType : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase
        MemberType    : Method
        Module        : Microsoft.Azure.PowerShell.Cmdlets.ResourceManager.dll
    StackTrace :
   at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.HandleException(ExceptionDispatchInfo capturedException)
   at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.ExecuteCmdlet()
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0(T c)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`1 executor)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
    Source     : Microsoft.Azure.PowerShell.Cmdlets.ResourceManager
    HResult    : -2146233086
CategoryInfo          : CloseError: (:) [New-AzPolicyDefinition], ArgumentOutOfRangeException
FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzurePolicyDefinitionCmdlet
InvocationInfo        :
    MyCommand        : New-AzPolicyDefinition
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 5
    Line             : New-AzPolicyDefinition -Name "allowed-locations3" `

    PositionMessage  : At line:1 char:1
                       + New-AzPolicyDefinition -Name "allowed-locations3" `
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : New-AzPolicyDefinition
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

Environment data


Name                           Value
----                           -----
PSVersion                      7.2.0-preview.5
PSEdition                      Core
GitCommitId                    7.2.0-preview.5
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cormacpaynecommented, May 11, 2021

@daxian-dbw Hey Dongbo, I’m no longer a part of the Azure PowerShell team, so I’ll defer to @markcowl since (I believe) he’s still a part of the team (I think @dingmeng-xue may also be able to help get traction on this item).

0reactions
daxian-dbwcommented, May 12, 2021

@dingmeng-xue Thanks for confirming this is a another case of the minimal progress rendering issue. So this is a duplicate of #15251, and the PR is out #15264. I will close this one as duplicate and get the PR merged soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Length cannot be less than zero Parameter Name: length
1 Answer 1 ... Change the catch code to MessageBox.Show(ex.ToString()...) instead of ex.Message. This way when error happens you will get a ...
Read more >
Connect-PnpOnline: Getting error Length can not be zero. ...
While connecting to SharePoint Online inside azure queue function I am getting error "Length can not be zero. Parameter:Length". I am using self....
Read more >
Length cannot be less than zero : r/PowerShell
Below is my code and I am getting an error on $TVShowName : exception calling "substring" with "2" argument(s): Length cannot be less...
Read more >
Microsoft Sharepoint error "Length cannot be less than ...
Typically this error is indicating that a negative number is being passed to a sub-string which is not valid (i.e a value of...
Read more >
Length cannot be less than zero. Parameter name
The error message is clear. The second argument (length) to Substring() is out of range, specifically it is negative.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found