Azure RunBook Giving me an error

Nipuna Weerasinghe 21 Reputation points
2022-06-29T17:22:18.883+00:00

Hi All,
I m new to Azure Run book and built a small runbook as follows.
When I test using the Test Pane I m getting an error "Error occurred: Length cannot be less than zero. (Parameter 'length')"

Can someone please tell me what might be the issue here?

Param
(
[parameter(Mandatory=$true)]
[string] $webUrlAdmin,
[parameter(Mandatory=$true)]
[string] $webUrl
)

$appId = Get-AutomationVariable -Name 'appId'
$tenantName = Get-AutomationVariable -Name 'tenantName'
$tenantId = Get-AutomationVariable -Name 'tenantId'
$certificate = Get-AutomationCertificate -Name 'AzureRunAsCertificate'

try {

Connect-PnPOnline -Url $webUrlAdmin -Tenant $tenantName -ClientId $appId -Certificate $certificate  
Set-PnPSite $webUrl -DenyAddAndCustomizePages 0  

}
catch {
Write-Output "Error occurred: $PSItem"
}
finally {

Disconnect-MgGraph

Disconnect-PnPOnline

}

216120-image.png

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,307 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. tbgangav-MSFT 10,421 Reputation points
    2022-07-02T03:00:38.723+00:00

    Hi @Nipuna Weerasinghe ,

    Set-PnPSite cmdlet does not have parameter called as DenyAddAndCustomizePages so correct it with DenyAndAddCustomizePages.

    Set-PnPSite $webUrl -DenyAndAddCustomizePages $false  
    

    To understand if this is Azure Automation level or code-level or sharepoint/pnp level issue, try same script in your local or sharepoint/pnp environment and see if scripts works without any issues or not.

    If issue persists at code-level then that means somewhere you are accessing length property of some object, which is not having any data in it. So, it would throw that exception. Hence, debug your code and see the line where it throws the exception. Before doing anything with the length, just check if has any data or not.

    If the issue is at sharepoint/pnp level, then AFAIK the "length cannot be less than zero" error in this case is generally seen on a sharepoint data source and is typically filtered out after a few attempts from server. 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 -1 is being passed). In order to locate which value it is, you might have to reach out to technical support team to have them resolve the parameter length and point you to which value is sending an incorrect value.

    Nonetheless, if script works fine in your local or sharepoint/pnp environment then capture debug stream in the runbook and see if it gives more information.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.