I am running Windows PowerShell as administrator and I am trying to create a file with information I get from Sharepoint Online. I run the below and get a message with a link I follow (devicelogin), enter the code given and then after I add my credentials, I am signed in.
Import-Module PnP.PowerShell
Update-Module SharePointPnPPowerShell*
Connect-PnPOnline -Url "https://<mydomain>-admin.sharepoint.com" -PnPManagementShell
$siteUrl = "https://<mydomain>.sharepoint.com/sites/ASI-DropPoint"
$cred = Get-Credential
#Connect-PnPOnline -Url $siteUrl -Credential $cred
#or the below to get more detailed info about connection process
Connect-PnPOnline -Url $siteUrl -Credential $cred -Verbose
$filePath = "C:\Users\ASI_app_eng\Desktop\To do"
$DocLibraryName="Documents"
# testing if the path can be accessed because sometimes I get an error that the access is denied.
if (Test-Path $filePath) {
Write-Output "It has access"
} else {
Write-Output "Cannot access the specified file path"
}
# Get the list items from the document library in batches of 1000 items
$listItems = Get-PnPListItem -List $DocLibraryName -PageSize 1000
# Create a new file and write the names of the files and last accessed date to it with tab (default) separator
$listItems | Select-Object Name,LastAccessedDate | Out-File -FilePath "$filePath\filenames.txt"
The error I get after some minutes is the below. Get-PnPListItem : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
The library contains almost 7000 documents/items.
I found some information online about the problem here, here and on these 2 stackoverflow posts: 1, 2.
I am completely new to PowerShell and Sharepoint management; I practically came up with this script after days of asking chatgpt and solving bug after bug. Are the above solutions suitable for my issue? I am afraid I might do something I'm not supposed to and cause problems to my work laptop and/or sharepoint.
I tried the below, in case it was a connection issue, but this doesn't seem to be the problem. My connection is fast and stable.
Connect-PnPOnline -Url $siteUrl -Credential $cred -RetryCount 3 -RetryWait 10