New-AzPolicyDefinition: Length cannot be less than zero. (Parameter 'length')
See original GitHub issueGetting the above error when running the deploy from PowerShell 7.2.0 preview 5. PowerShell 7.1.3 works as expected.
- Installed PowerShell 7.2.0 preview 5
- Logged into Azure
- Selected my subscription.
- 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:
- Created 2 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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).
@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.