Thursday, April 13, 2023

The ways to connect SharePoint using PnP-Powershell module

There are several ways to connect to sharepoint using Connect-PnPOnline

```sh 

# Example :1 

$PnPModule= Get-InstalledModule PnP.Powershell -ErrorAction SilentlyContinue

if($PnPModule -eq $null) { Install-Module -Name "PnP.PowerShell" -RequiredVersion 1.12.0 -Force -AllowClobber }

$URL="https://contso.sharepoint.com/sites/Pre-Screening"

Connect-PnPOnline -Url $URL

```
In this way ,We can connect to SharePoint prompting for the username and password. When a generic credential is added to the Windows Credential Manager with $URL (https://{tenant}.sharepoint.com)
 PowerShell will not prompt for username and password and use those stored credentials instead. 
 
```sh 
# following is the code snippet for adding generic credentials in Windows credentials manager.
 		 
         $credential=Get-credential -Message "Please enter User Name & Password to connect to sharepoint"
         
         $userName=$credential.UserName
         $password=$credential.GetNetworkCredential().Password
         
         cmdkey /generic:$URL /user:$UserName /pass:$password
```
 Note:
 
  - [Needs permission to access resources in your organization that only an admin can grant.](https://learn.microsoft.com/en-us/answers/questions/985641/needs-permission-to-access-resources-in-your-organ)
  - [configure-admin-consent-workflow in Azure AD](https://learn.microsoft.com/en-us/azure/active-directory/manage-apps/configure-admin-consent-workflow)
  - [manage-user-consent-to-applications-in-microsoft-365](https://blog.admindroid.com/manage-user-consent-to-applications-in-microsoft-365/)

------

```sh 
# Example :2 
 
 $URL="https://contso.sharepoint.com/sites/Pre-Screening"
 
 Connect-PnPOnline -url $URL -Interactive
 
```

 In this way , Connects to the Azure AD, acquires an access token and allows PnP PowerShell to access both SharePoint and the Microsoft Graph.
 By default it will use the PnP Management Shell multi-tenant application behind the scenes, so make sure to run `Register-PnPManagementShellAccess` first.

------
```sh 
 # Example :3
 
 $URL="https://contso.sharepoint.com/sites/Pre-Screening"
 Connect-PnPOnline -Url $URL -Credentials (Get-Credential)
    
```
 
 In this way,Connect to SharePoint prompting for the username and password to use to authenticate.

```sh 
# Example :3.1 

		 $credential=Get-credential -Message "Please enter User Name & Password"
         $userName=$credential.UserName
         $password=$credential.GetNetworkCredential().Password
        cmdkey /generic:SharepointCredentials /user:$UserName /pass:$password  #This is one time activity to add generic credentials in Windows credentials manager.
 
        
        $URL="https://contso.sharepoint.com/sites/Pre-Screening"
        
        Connect-PnPOnline -Url $URL -Credentials SharepointCredentials
```
 In this way, Connect to SharePoint for username and password when a generic credential is added to windows credential manager with **"SharepointCredentials"**
    
 
------

```sh 
 # Example :4 
   
   #In this way , Connect to sharepoint using Ceritifcate authentication 
   
   #Get certificate from Certificate Authority and install certificate in mmc.
   
 
 $Config=@{ URL ="https://contso.sharepoint.com/sites/Pre-Screening";
            ClientID=;
            Tenant=;
            ThumbPrint=}
 
  $connection=Connect-PnPOnline -Url $Config.URL -ClientId $Config.ClientID  -Tenant $Config.Tenant  -ThumbPrint $Config.ThumbPrint -ReturnConnection
  
  Get-PnPSite  -Connection $Connection
  Get-PnPList  -Connection $Connection
  
  $Files ="C:\Users\Documents\*.txt"
  $Destination ="Shared Documents/General/Reports"
  
  gci $File | &{Process{ Add-PnPFile -Folder $Destination -Path $.FullName -Value @{Modified=$_.LastWriteTime}  -Connection $connection}}
  
```


In this way, Connect to SharePoint using clientID and clientSecret from App Registration in Azure AD.

------

```sh 
 # Example :5 
   
 $URL="https://contso.sharepoint.com/sites/Pre-Screening"
 
 Connect-PnPOnline -Url $URL -ClientId 344b8aab-389c-4e4a-8fa1-4c1ae2c0a60d -ClientSecret $clientSecret
```

In this way, Connect to SharePoint using clientID and clientSecret from App Registration in Azure AD.

------

```sh 
 # Example :6 
 
  $URL="https://contso.sharepoint.com/sites/Pre-Screening"
 
  Connect-PnPOnline -Url $URL -DeviceLogin
```
 
In this way, Authenticate user using the PnP Management Shell Multi-Tenant application.

A browser window will have to be opened where you have to enter a code that is shown in your PowerShell window.

------

```sh 

 # Example :7
 
 $URL="https://contso.sharepoint.com/sites/Pre-Screening"
 
 Connect-PnPOnline -Url $URL -DeviceLogin -LaunchBrowser

```
This will authenticate you using the PnP Management Shell Multi-Tenant application. 
A browser window will have to be opened where you have to enter a code that is shown in your PowerShell window.

------

```sh  
#Example :8
 
 $URL="https://contso.sharepoint.com/sites/Pre-Screening"
 
 Connect-PnPOnline -Url $URL -UseWebLogin:$true
 
 Get-PnPTeamsTeam 
 Get-PnPList 

```
 - Connects to SharePoint using legacy cookie based authentication. 
 - Notice this type of authentication is limited in its functionality. We will for instance not be able to acquire an access token for the Graph, 
   and as a result none of the Graph related cmdlets will work. Also some of the functionality of the provisioning engine
 
 - Get-PnPSiteTemplate, Get-PnPTenantTemplate, Invoke-PnPSiteTemplate, Invoke-PnPTenantTemplate) will not work because of this reason. 
 
 - The cookies will in general expire within a few days and if you use -UseWebLogin within that time popup window will appear that will disappear immediatel #>

------

```sh 
	# Example :9
    
    $URL="https://contso.sharepoint.com/sites/Pre-Screening"
    
    Connect-PnPOnline -Url $URL -ManagedIdentity
    
``` 
   
  - Connects using a system assigned managed identity to Microsoft Graph.
  - Using this way of connecting only works with environments that support managed identities: Azure Functions, Azure Automation Runbooks and the Azure Cloud Shell.
    
------

```sh
	#Example :10
     
    $URL="https://contso.sharepoint.com/sites/Pre-Screening"
    
    Connect-PnPOnline -Url $URL -ManagedIdentity -UserAssignedManagedIdentityObjectId 623c1b21-6611-47fd-a616-674d3aec2a52

``` 

  - Connects using an user assigned managed identity with object/principal ID 623c1b21-6611-47fd-a616-674d3aec2a52 to SharePoint Online.
  - Using this way of connecting only works with environments that support managed identities: Azure Functions, Azure Automation Runbooks and the Azure Cloud Shell.
    
------

No comments:

Post a Comment