0

I have an authentication script below which is deemed as legacy and will soon be blocked by Microsoft.

How can I convert this script to use modern auth methods?

$site = https://mysite.sharepoint.com
$listName = "Events"
$user = myuser@mysite.onmicrosoft.com
$pwd = ConvertTo-SecureString "XXXXXXXXXXX" -AsPlainText -Force
 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,$pwd)
$ctx.Credentials = $credentials
$siteColl = $ctx.Web
$ctx.Load($siteColl)
$ctx.ExecuteQuery() 
2

You could use account with MFA to connect to SharePoint Online:

 #Add required references to OfficeDevPnP.Core and SharePoint client assembly
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\3.29.2101.0\OfficeDevPnP.Core.dll") 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
 
$siteURL = "https://contoso.sharepoint.com/sites/siten_name"
  
$AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
$ctx = $AuthenticationManager.GetWebLoginClientContext($siteURL)
$ctx.Load($ctx.Web)
$ctx.ExecuteQuery()
  
Write-Host "Title: " $ctx.Web.Title -ForegroundColor Green
Write-Host "Description: " $ctx.Web.Description -ForegroundColor Green

For more information, you could refer to; https://social.technet.microsoft.com/wiki/contents/articles/53244.sharepoint-connect-to-site-with-mfa-enabled-using-csom-and-pnp-powershell.aspx

New contributor
Yi Lu_MSFT is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • This looks good but would require the user to sign in with MFA right? This script will run on task scheduler and needs to be fully automated.
    – BennKingy
    Jan 13 at 12:06
1

The only way to bypass MFA requirement is to use app only access. I have multiple scripts that run unattended using this approach on Azure. You can find more information on this Microsoft link:

https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

New contributor
chandan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

https://morgantechspace.com/2021/09/connect-to-sharepoint-site-with-mfa-account-using-csom-and-powershell.html < Theres a guide here that should work without having to consent to MFA by the user :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.