Skip to content

[BUG] Connect-PnPOnline will not work after multiple runs #2375

@2nh

Description

@2nh

Reporting an Issue or Missing Feature

I am in the process of running some test for downloading/uploading files to SharePoint online.
These are automation scripts that run without any user interaction.

I start my day out perfectly fine. All code and functions are running as expected. Able to perform MULTIPLE tests that work flawlessly.
However after a couple of tests I am unable to run Connect-PnPOnline

I am authenticating with a certificate thumbprint.
Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Thumbprint $cert.Thumbprint -Tenant $TenantId

Error:

Connect-PnPOnline : An error occurred while sending the request.
At line:1 char:1
+ Connect-PnPOnline -Url $SiteURL -ClientId $CilentID ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Connect-PnPOnline], HttpRequestException
    + FullyQualifiedErrorId : System.Net.Http.HttpRequestException,PnP.PowerShell.Commands.Base.ConnectOnline


I have noticed that SOMETIMES when I close out of PowerShell ISE and relaunch it connects back again. But most of the time this does not resolve the issue.

I am also running Disconnect-PnPOnline after every download/upload is completed.

Expected behavior

Expected behavior is for it to connect to PnP online and receive an access token.

Actual behavior

Code will sometimes connect to PnPOnline fine.
After a couple of runs it begins to fail.

I have tried running the Connect-PnPOnline inside of a function, outside of a function.
I have tried to store the connection within a variable. No luck.

Steps to reproduce behavior

To reproduce the behavior I run my script multiple times.

It will begin to error out randomly on the Connect-PnPOnline.
I run multiple tests on different sites but it will always find away to error out eventually. Not sure what I am doing wrong.

    Try {
        $cert = Get-ChildItem -Path Cert:\LocalMachine\My\ID
        
        Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Thumbprint $cert.Thumbprint -Tenant $TenantId
        Write-Host -f Green "Connected to PnP Online..."

        $Files = Get-PnPFolderItem -Connection $connection -FolderSiteRelativeUrl $SourceFolder -ItemType File
 
        foreach($file in $Files){
            Try{
                Get-PnPFile -Url $file.ServerRelativeUrl -Path $TargetFolder -Filename $file.Name -AsFile -Force
                Write-host -f Green "Downloaded the File: "$file.Name
            } Catch {
                Write-host -f Red "Error Downloading File: "$file.Name
            }
        }

        Disconnect-PnPOnline
        Write-Host -f Green "...Disconnected from PnP Online"

    } Catch { 
        Write-Host $_
        Write-host -f Red "Error Downloading Multiple files"
    }

What is the version of the Cmdlet module you are running?

PnP.PowerShell Version 1.11.0

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

  • [x ] Windows
  • Linux
  • MacOS
  • Azure Cloud Shell
  • Azure Functions
  • Other : please specify

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions