powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[BUG] Connect-PnPOnline is not working in azure automation runbook with powershell 7.0

Open hbandaru02 opened this issue 2 years ago • 7 comments

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. image

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) image

Which operating system/environment are you running PnP PowerShell on?

  • [ ] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [ ] Azure Functions
  • [ x ] Azure Automation

hbandaru02 avatar Jul 21 '22 15:07 hbandaru02

@petera5_cat @elstes_cat

hbandaru02 avatar Jul 21 '22 15:07 hbandaru02

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

schumatt-msft avatar Jul 21 '22 18:07 schumatt-msft

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 avatar Aug 31 '22 19:08 gautamdsheth

@gautamdsheth we tried even storing into a variable it did not work

hbandaru02 avatar Aug 31 '22 20:08 hbandaru02

@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 ?

gautamdsheth avatar Sep 02 '22 11:09 gautamdsheth

If you look at the screen shot there is not other modules installed: image

hbandaru02 avatar Sep 02 '22 15:09 hbandaru02

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.

Screenshot 2022-09-05 180209

StevieBleeds avatar Sep 05 '22 17:09 StevieBleeds

I created a PR at https://github.com/pnp/powershell/pull/2438 that fixes the issue

jackpoz avatar Oct 09 '22 14:10 jackpoz

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 avatar Oct 10 '22 09:10 gautamdsheth

@gautamdsheth Issue still exists Same error again

hbandaru02 avatar Oct 21 '22 19:10 hbandaru02

Which PnP.PowerShell version did you use ?

jackpoz avatar Oct 21 '22 21:10 jackpoz