[BUG] Connect-PnPOnline is not working in azure automation runbook with powershell 7.0
Reporting an Issue
Please confirm what it is that your reporting Command "Connect-PnPOnline -Url $sharepointUrl -ClientId $ClientId -ClientSecret $ClientPassword -WarningAction Ignore" is not working azure automation runbook with powershell version 7. Weird part is it is working with powershell version 5 and also local .
Expected behavior
Please describe what output you expect to see from the PnP PowerShell Cmdlets Establish connection with sharepoint with azure automation runbook powershell 7 version
Actual behavior
Please describe what you see instead. Please provide samples of output or screenshots.

Steps to reproduce behavior
Please include complete script or code samples in-line or linked from gists
$ClientId = "<ClientId>"
$ClientPassword = "<ClientSecret>"
$sharepointUrl = "<SharepointURL>"
Write-Verbose "Connecting to sharepoint" -Verbose
Connect-PnPOnline -Url $sharepointUrl -ClientId $ClientId -ClientSecret $ClientPassword -WarningAction Ignore
What is the version of the Cmdlet module you are running?
(you can retrieve this by executing Get-Module -Name "PnP.PowerShell" -ListAvailable)

Which operating system/environment are you running PnP PowerShell on?
- [ ] Windows
- [ ] Linux
- [ ] MacOS
- [ ] Azure Cloud Shell
- [ ] Azure Functions
- [ x ] Azure Automation
@petera5_cat @elstes_cat
I am able to reproduce this using the PnP.PowerShell module using PowerShell 7.1 runtime as well. It appears the Connect-PnPOnline cmdlet is hitting a problem displaying a warning about using client id/secret. Including the stack trace from my repro below:
Connect-PnPOnline: C:\Temp\pt1qjp1j.aas\eb2f676b-5d26-42e4-9036-a86b0a1c0328.ps1:9
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'startIndex')
at System.String.LastIndexOfAny(Char[] anyOf, Int32 startIndex, Int32 count)
at System.String.LastIndexOfAny(Char[] anyOf, Int32 startIndex)
at PnP.PowerShell.Commands.Utilities.CmdletMessageWriter.WordWrap(String text, Int32 maxLineLength) in c:\build\src\Commands\Utilities\CmdletMessageWriter.cs:line 88
at PnP.PowerShell.Commands.Utilities.CmdletMessageWriter.WriteFormattedMessage(PSCmdlet cmdlet, Message message) in c:\build\src\Commands\Utilities\CmdletMessageWriter.cs:line 112
at PnP.PowerShell.Commands.Base.ConnectOnline.ConnectACSAppOnly() in c:\build\src\Commands\Base\ConnectOnline.cs:line 357
at PnP.PowerShell.Commands.Base.ConnectOnline.Connect(CancellationToken& cancellationToken) in c:\build\src\Commands\Base\ConnectOnline.cs:line 264
at PnP.PowerShell.Commands.Base.ConnectOnline.ProcessRecord() in c:\build\src\Commands\Base\ConnectOnline.cs:line 218
at System.Management.Automation.Cmdlet.DoProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
ConnectOnline.cs, line 357 is trying to throw an error, and CmdletMessageWriter, line 88 is trying to wrap it to the screen, the dimensions of which are probably undefined when running under Azure Automation
Can you store the commands in a variable and check like this:
$connection = Connect-PnPOnline -Url $sharepointUrl -ClientId $ClientId -ClientSecret $ClientPassword -WarningAction IgnoreConnect-PnPOnline -Url $sharepointUrl -ClientId $ClientId -ClientSecret $ClientPassword -WarningAction Ignore
This usually fixes the issue in Azure automation related cmdlets.
@gautamdsheth we tried even storing into a variable it did not work
@foobar123-del - can you check if you are using only PnP PowerShell ? Maybe if you are using some modules, then they might conflict which cause this , so you can test by not loading other modules and try ?
If you look at the screen shot there is not other modules installed:

I am able to reproduce the same error as well with PowerShell 7.1 using the Client ID/Client Secret combination.
However, it does work when using CBA.
I created a PR at https://github.com/pnp/powershell/pull/2438 that fixes the issue
hi @hbandaru02 / @StevieBleeds - this issue has been fixed by @jackpoz.
It will be available in tomorrow's nightly and the next major release whenever that happens.
Thanks for raising this issue and thanks @jackpoz for fixing it, much appreciated 😊🙏
@gautamdsheth Issue still exists Same error again
Which PnP.PowerShell version did you use ?