0

What I would like is to logon to sharepoint with powershell using the CSOM-module and using the Credential Manager where I have safely stored the password.

This is a working code to logon to sharepoint

    Try 
    {
          $Credentials = New-Object 
          Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,(ConvertTo-SecureString $pass -AsPlainText -Force))
           $Context =  New-Object 
           Microsoft.SharePoint.Client.ClientContext($SiteUrl)
            $Context.Credentials = $Credentials
     }
     Catch 
     {
        exit 
     } 

But how can I use the credential manager. I have installed the module en made this:

#$managedCred = Get-StoredCredential -Target SharepointOnline

Does anyone know the solution.

Gr P

1

Peter, I have answered the same question posted by you in another Technet Thread, please remember to check it:

[TechNet] Log on to SharePoint with Powershell using the Credential Manager

Hi PeetK,

Firstly, create a Generic Credentials in Credential Manager:

enter image description here

In my side, the name is "o365" for example, then in PowerShell, use like this:

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$cred = Get-StoredCredential -Target 'o365'  
#Variables for Processing
$SiteUrl = "https://TenantName.sharepoint.com/sites/SiteName/"

#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)

#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
$Context.ExecuteQuery()
$Web=$Context.Web;
$Context.Load($Web)
$Context.ExecuteQuery()

enter image description here

3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Gaurravs
    Nov 5 '19 at 9:02
  • @Gaurravs, OK, I will update my reply above to include detailed content.
    – Jerry
    Nov 5 '19 at 10:48
  • 1
    @Gaurravs,Done, please check.
    – Jerry
    Nov 5 '19 at 11:00
0

This is possible with PnP PowerShell. I use this all the time as I have dozens of client environments I connect to.

Connect-PnPOnline <url> -Credential <name of stored credential>

https://github.com/SharePoint/PnP-PowerShell/wiki/How-to-use-the-Windows-Credential-Manager-to-ease-authentication-with-PnP-PowerShell#label-approach

If you want to do this without PnP you can check out the code on Github and reverse engineer it.

Your Answer

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