Introduction
This article will describe different ways of managing the credentials for SharePoint online using PowerShell.
We can pass the credentials in three different ways.
Using Secured String
In this type, we are hardcoding the username and password and then passing it for authentication.
Disadvantage
The password is visible to everyone who has access to the PowerShell script.
Using File
By using this file, we can secure our credentials a little bit, as the password is stored in encrypted format.
Disadvantage
The secret file is stored at the local drive on our computer and can be decrypted using some tools.
Using Window Credential Manager.
This type is the most secured and recommended way to pass the credentials.
Disadvantage
In this type, the user should have admin right, otherwise, it will prompt for credentials everytime we run the script.
Using Secured String
Hardcode the credentials in the following variables:
- $Username="Your userName"
- $Password="Your Password"
The complete code will look like:
- Function Login-SharePointOnline()
- {
- Try {
- #Get Credentials to connect
-
- $Username="Your userName"
- $Password="Your Password"
- $ListName="List Name"
- $siteURL="Site Url"
- $query = "<View>
- </View>"
- $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
- $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $securePassword)
- $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $Ctx.Credentials = $Cred
-
- #check context available or not
- if (-not($Ctx)) {
- Write-Host "Error connecting to SharePoint Online, unable to establish context" -foregroundcolor black -backgroundcolor Red
- return
- }
- else
- {
- Write-Host "Connected to the Sharepoint Online successfully" -foregroundcolor green
- }
- #Get CAML Query object
- $camlquery = New-Object Microsoft.SharePoint.Client.CamlQuery;
- $camlquery.ViewXml= $query
-
- $List = $Ctx.Web.Lists.GetByTitle($ListName)
- $Items = $List.GetItems($camlquery)
- $Ctx.Load($Items)
- $Ctx.ExecuteQuery()
-
- #Get each value from list and print
- ForEach($Item in $Items)
- {
- Write-Host "Item in the List is:"$Item["Title"]
- }
- Write-Host $SourceList
-
- }
- catch
- {
- Write-Host "Error connecting to SharePoint Online: $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
- return
- }
- }
- #Calling function
- Login-SharePointOnline
Using File
Follow the below steps to create a secret file and write the username and password into that file.
Step 1
I have created folder Arvind\safe under (D:\) drive. (You can create your own directory to save the secretfile).
Step 2
Open powershell window run as administrator and run the following script which will prompt for credentials.
- $credentials = Get-Credential
- $filename = 'D:\Arvind\safe\secretfile.txt'
- $credentials | Export-Clixml -path $filename
Provide your username and password and click the OK button to generate the secret file.
Ensure that the secret file is generated at our directory (D:\Arvind\safe\) and that it contains the username and password (encrypted format).
Step 3
To read and pass the credentials to sharepoint online, include the following lines in the script:
- $credPath = 'D:\Arvind\safe\secretfile.txt'
- $fileCred = Import-Clixml -path $credpath
- $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($fileCred.UserName, $fileCred.Password)
The complete script for passing the credentials using file will look like the following:
- Function Login-SharePointOnline()
- {
- Try {
- #Get Credentials to connect
-
- $ListName="ListName"
- $siteURL="Site URL"
- $query = "<View>
- </View>"
- $credPath = 'D:\Arvind\safe\secretfile.txt'
- $fileCred = Import-Clixml -path $credpath
- $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($fileCred.UserName, $fileCred.Password)
- $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $Ctx.Credentials = $Cred
-
- #check context available or not
- if (-not($Ctx)) {
- Write-Host "Error connecting to SharePoint Online, unable to establish context" -foregroundcolor black -backgroundcolor Red
- return
- }
- else
- {
- Write-Host "Connected to the Sharepoint Online successfully" -foregroundcolor green
- }
- #Get CAML Query object
- $camlquery = New-Object Microsoft.SharePoint.Client.CamlQuery;
- $camlquery.ViewXml= $query
-
- $List = $Ctx.Web.Lists.GetByTitle($ListName)
- $Items = $List.GetItems($camlquery)
- $Ctx.Load($Items)
- $Ctx.ExecuteQuery()
-
- #Get each value from list and print
- ForEach($Item in $Items)
- {
- Write-Host "Item in the List is:"$Item["Title"]
- }
- }
- catch
- {
- Write-Host "Error connecting to SharePoint Online: $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
- return
- }
- }
- #Calling function
- Login-SharePointOnline
Using Window Credential Manager
First, we require SharePoint Online PnP cmdlets to use the above method.
To install the SharePoint online cmdlets refer the following
article or enter the following command in PowerShell.
- Install-Module SharePointPnPPowerShellOnline
Step 1
Open the window credential manager by typing credential manager in the search options.
Step 2
Click on Add a generic credential link under windows credentials.
Step 3
Enter the following details and click on the ok button.
Note
Under Internet or network address, it is a logical name that is used in the script to read the credentials.
Step 4
To read the credentials from the window credentials manager pass the logical name(created in step 3) as a parameter in the script.
- Connect-PnPOnline -Url $siteURL -Credentials SharePointCredentials
The complete code for passing the credentials using PNP cmdlets and window credentials manager is as follows:
- Function Login-SharePointOnline()
- {
- Try {
-
- $ListName="ListName"
- $siteURL="site Url"
- Connect-PnPOnline -Url $siteURL -Credentials SharePointCredentials
- $ctx = Get-PnPContext
- #check context available or not
- if (-not($Ctx)) {
- Write-Host "Error connecting to SharePoint Online, unable to establish context" -foregroundcolor black -backgroundcolor Red
- return
- }
- else
- {
- Write-Host "Connected to the Sharepoint Online successfully" -foregroundcolor green
- }
- #Get CAML Query object
- $camlquery = New-Object Microsoft.SharePoint.Client.CamlQuery;
- $camlquery.ViewXml= $query
-
- $List = $Ctx.Web.Lists.GetByTitle($ListName)
- $Items = $List.GetItems($camlquery)
- $Ctx.Load($Items)
- $Ctx.ExecuteQuery()
-
- #Get each value from list and print
- ForEach($Item in $Items)
- {
- Write-Host "Item in the List is:"$Item["Title"]
- }
- }
- catch
- {
- Write-Host "Error connecting to SharePoint Online: $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
- return
- }
- }
- #Calling function
- Login-SharePointOnline
The complete script can be downloaded from attachment.