pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
987 stars 665 forks source link

Connect-PnPOnline Issue with -Credentials #2298

Open Amysno opened 4 years ago

Amysno commented 4 years ago

Reporting an Issue

I am having trouble with the cmdlet Connect-PnPOnline all of a sudden. I was using Windows autentication manager with a generic autentication : Label : "MyOrg', email : "myaccount@myorg.onmicrosoft.com", password : "MyPassWord" So I call Connect-PnPOnline -Url 'https://myorg.sharepoint.com/sites/MySite' -Credentials MyOrg but this leads to an error. I then tried several things :

I do not want to use Web Login, because the script will be stored as a scheduled task, so no human will enter some creds.

The different code I used are in the Steps to reproduce.

Expected behavior

Connecting to the URL given in parameters, so I can perform some actions.

Actual behavior

Does not connect to given URL. Throws an error : image

Connect-PnPOnline : La référence d'objet n'est pas définie à une instance d'un objet. Au caractère Ligne:7 : 1 ++ Connect-PnPOnline -Url $sommaireURL -UseWebLogin ++ ~~~~~~~~~~~~ ++ CategoryInfo : NotSpecified: (:) [Connect-PnPOnline], NullReferenceException ++ FullyQualifiedErrorId : System.NullReferenceException,SharePointPnP.PowerShell.Commands.Base.Connec tOnline

Steps to reproduce behavior

Here is the code I used :

`Import-Module "$(get-location)\pnpmodules\3.10.1906.0\Modules\SharePointPnPPowerShellOnline\SharePointPnPPowerShellOnline" -Verbose:$false -DisableNameChecking

$orgName='myorgname'
$siteURL = "https://myorgname.sharepoint.com/sites/MySite"
Connect-PnPOnline -Url $siteURL -Credentials $orgName
##then, trying with only Get-Credentials : 
Connect-PnPOnline -Url $siteURL -Credentials Get-Credentials`

Which version of the PnP-PowerShell Cmdlets are you using?

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

SharePointPnPPowerShellOnline v 3.10.1906.0

How did you install the PnP-PowerShell Cmdlets?

Please help me with this, I don't know what to do, it worked like a charm before last week.

ghost commented 4 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

gobigfoot commented 4 years ago

LegacyAuthProtocolsEnabled must be true on your tenant. Run the command “Get-SPOTenant”. Check the property “LegacyAuthProtocolsEnabled”. If this is set to true then Legacy Authentication is enabled else disabled.

This method has been working for me. I have the user and password stored in environment variables to keep them out of source control.

$SecurePass = $Env:SharepointPassword | ConvertTo-SecureString -AsPlainText -Force
Add-PnPStoredCredential -Name MyUser -Username $Env:SharepointUser -Password $SecurePass
Connect-PnPOnline -Url $SiteUrl -Credentials 'MyUser'

It is honestly better to set up Application Authentication for a scheduled task. https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs Follow these instructions then you can connect using

Connect-PnPOnline  -Url "https://tenant.sharepoint.com/sites/mysite" -AppId "AppId" -AppSecret "AppSecret"