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.