Update and a much better way to approach this:
Use a SharePoint App Only Client Id and Secret to access the site, list, or library.
Microsoft documentation:
https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
You can create an app principle that is limited to a single site, list, library, or a combination of them:
https://piyushksingh.com/2018/12/26/register-app-in-sharepoint/
$sampleConnect = Connect-PnPOnline -Url "https://YOURsite.sharepoint.com/sites/parent/child" -AppId "12345-94c3-4149-bda5-abcedffadsf" -AppSecret "643r4er5sfdadsfadsfdsf=" -ReturnConnection
Write-Host $sampleConnect.Url
In this example, I’m connecting to a Site Collection on my tenant.
Assumptions:
1) You have created a token in your o365 site
1.1) https://portal.office.com/account/
1.2) On the left site of the page click Security & privacy, then click Create and manage app passwords
1.3) In the app password page click the create button and give it a name.
1.4) Save the password to a secure location.
1.5) There is a better way of doing this that I will cover in a future post.
2) You have downloaded to CSOM DLL(s) from Nuget
Clear-Host
$userName = "me@sharepointed.com"
$pw = "abc123taco" # I"M USING AN APP PASSWORD
$siteCollectionUrl = "https://sharepointed.sharepoint.com/sites/taco"
#Secure the password
$securePassword = ConvertTo-SecureString $pw -AsPlainText -Force
Add-Type -Path "C:\Code\DLL\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Code\DLL\Microsoft.SharePoint.Client.Runtime.dll"
#Create Context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteCollectionUrl)
#Authorise
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securePassword)
$web = $ctx.Web
$properties = $web.AllProperties
$ctx.Load($web)
$ctx.Load($properties)
$ctx.ExecuteQuery()
Write-Host " Site Collectione URL: $($web.Url)"
Write-Host "Properties are "
foreach ($prop in $properties) {
$prop.FieldValues
}