Вопрос

I am able to log in to my SharePoint online site using my credential but when I am connecting to the same SharePoint online site using the same credentials using PowerShell CSOM - getting the invalid credential error - tried to connect to the same site using sharegate tool it works but only with powershell it fails.

Connection sample code :

$UserName="myaccount@mydomain.com"
$Password ="MyPassword"

#Setup Credentials to connect
   $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
 $Context.Credentials = $credentials

Thanks in advance - any help is greatly appreciated.

Это было полезно?

Решение

If the account has MFA (Multi Factor Enabled), I mean apart from username and password we need to pass the one time OTP which we will get in our mobile - this is called MFA enabled password. Example: if we run the hybrid configuration onboarding powershell script which is provided by Microsoft that also gets failed incase of MFA enabled password.

To overcome this issue - for time being disable the MFA for your account and after executing the script enable it back. However, this is fine if this is one time execution but if we want to write and execute the PowerShell script on regular basis, then this solution is not recommended. In that scenario we need to create a dedicated account like service account in SharePoint online with administrative access and without MFA enabled, and this account we need to use for any powershell script execution.

Другие советы

If you have multifactor authentication enforced for your user account, you need to create and use an app password instead of your normal password. To create an app password in office 365, visit aka.ms/createapppassword

Make sure you are on VPN if your tenant has MFA enabled

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top