Converting from 5.1 to 7.2 for automation

I am trying to convert code I have that works fine in 5.1. I am converting to 7.2 since Microsoft Managed Identities only seems to work under 7.2 online. That being said, it seems I got Azure and MgGraph to authenticate, however, I cannot get sharepoint online to connect, using the 5.1 method or managed Identites in 7.2 and not sure what I am doing wrong. I apologize in advance if this is more of an $MSFT question than powershell but I keep getting told that powershell is open source…

Connect-AzAccount -Identity

$token = Get-AzAccessToken -ResourceUrl “https://graph.microsoft.com

Connect-MgGraph -AccessToken $token.Token

#Connect SharePoint Online

$credObject = Get-AutomationPSCredential -Name ‘’

$Url = “https://-admin.sharepoint.com/”

Connect-PnPOnline -Url $URL -Credentials $credObject

#Also tried this, no go, complains about the -ManagedIdentity flag
#Connect-PnPOnline -Url https://-admin.sharepoint.com/ -ManagedIdentity

#Remove old list items from People Directory

$RemovePnPPeople = Get-PnPListItem -List “People Directory2” | Remove-PnPListItem -Force

#Re-add users to People directory

$users = @()

#Trading

$users += get-mguser -Property DisplayName,Department,Surname,GivenName,Mail -All | select Displayname,Department,Surname,GivenName,Mail | where {$_.Department -eq “Trading”} | sort Surname

#IT

$users += get-mguser -Property DisplayName,Department,Surname,GivenName,Mail -All | select Displayname,Department,Surname,GivenName,Mail | where {$_.Department -eq “Information Technology”} | sort Surname

#Quantitative Analysts

$users += get-mguser -Property DisplayName,Department,Surname,GivenName,Mail -All | select Displayname,Department,Surname,GivenName,Mail | where {$_.Department -eq “Quantitative Analysts”} | sort Surname

#Meteorology

$users += get-mguser -Property DisplayName,Department,Surname,GivenName,Mail -All | select Displayname,Department,Surname,GivenName,Mail | where {$_.Department -eq “Meteorology”} | sort Surname

#QTT

$users += get-mguser -Property DisplayName,Department,Surname,GivenName,Mail -All | select Displayname,Department,Surname,GivenName,Mail | where {$_.Department -eq “QTT”} | sort Surname

#Operations

$users += get-mguser -Property DisplayName,Department,Surname,GivenName,Mail -All | select Displayname,Department,Surname,GivenName,Mail | where {$_.Department -eq “Operations”} | sort Surname

#Facilities

$users += get-mguser -Property DisplayName,Department,Surname,GivenName,Mail -All | select Displayname,Department,Surname,GivenName,Mail | where {$_.Department -eq “Facilities”} | sort Surname

#Office of the CEO

$users += get-mguser -Property DisplayName,Department,Surname,GivenName,Mail -All | select Displayname,Department,Surname,GivenName,Mail | where {$_.Department -eq “Office of the CEO”} | sort Surname

foreach ($user in $users){

$AddPNPPeople = Add-PnPListItem -List “People Directory2” -Values @{Title=$user.Mail;Name=$user.Mail;Department=$user.Department;FirstN=$user.GivenName;LastN=$user.Surname;DisplayName=$user.DisplayName}

}

Output as follows:
Environments Context


{[AzureChinaCloud, AzureChinaCloud], [AzureUSGovernment, AzureUSGovernment], [AzureCloud, AzureCloud]} Microsoft.Azure.…

Welcome To Microsoft Graph!

Connect-PnPOnline:
Line |
11 | Connect-PnPOnline -Url $URL -Credentials $credObject
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Could not load file or assembly ‘System.IdentityModel.Tokens.Jwt, Version=6.27.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’. Could not find or load a specific file. (0x80131621)

Get-PnPListItem:
Line |
17 | $RemovePnPPeople = Get-PnPListItem -List “People Directory2” | Remove …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.

Get-MgUser_List1:
Line |
22 | $users += get-mguser -Property DisplayName,Department,Surname,GivenNa …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Insufficient privileges to complete the operation.

Get-MgUser_List1:
Line |
25 | $users += get-mguser -Property DisplayName,Department,Surname,GivenNa …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Insufficient privileges to complete the operation.

Get-MgUser_List1:
Line |
28 | $users += get-mguser -Property DisplayName,Department,Surname,GivenNa …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Insufficient privileges to complete the operation.

Get-MgUser_List1:
Line |
31 | $users += get-mguser -Property DisplayName,Department,Surname,GivenNa …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Insufficient privileges to complete the operation.

Get-MgUser_List1:
Line |
34 | $users += get-mguser -Property DisplayName,Department,Surname,GivenNa …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Insufficient privileges to complete the operation.

Get-MgUser_List1:
Line |
37 | $users += get-mguser -Property DisplayName,Department,Surname,GivenNa …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Insufficient privileges to complete the operation.

Get-MgUser_List1:
Line |
40 | $users += get-mguser -Property DisplayName,Department,Surname,GivenNa …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Insufficient privileges to complete the operation.

Get-MgUser_List1:
Line |
43 | $users += get-mguser -Property DisplayName,Department,Surname,GivenNa …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Insufficient privileges to complete the operation.

Any help would be greatly appreciated!

Not all code or modules are compatible with 7.x. 5.1 uses full .NET framework, 7.x uses .NET Core with a stripped-down version that can run cross platform. There may be dependencies on .NET Framework that are causing the issue, not code. If it works in 5.1 and same code doesn’t work in 7.x, then it’s likely .NET dependencies.

This might help Differences between Windows PowerShell 5.1 and PowerShell 7.x - PowerShell | Microsoft Learn

The most current version of Windows PowerShell is 5.1 which is built on top of .Net Framework.
Windows PowerShell and .Net Framework are the closed source/proprietary versions.

The most current version of PowerShell is 7.3 which is built on top of .Net Core. These versions are the open-source versions.

They really are not the same thing, even though they have a lot in common.

I don’t think I missed it in your code, but I am not seeing where you create a proper URL for connecting to SharePoint Online.

$Url = “https://-admin.sharepoint.com/”

compare to the examples here.
Connect-SPOService (Microsoft.Online.SharePoint.PowerShell) | Microsoft Learn

It also looks like you are trying to use PnP PowerShell module. This isn’t the official SharePoint PS module, and it does require PowerShell 7.x

Learn more about it here
Installing PnP PowerShell | PnP PowerShell

If you want to read up on the official SharePoint modules see here, Which uses 5.1 and .net framework.
Intro to SharePoint Online Management Shell | Microsoft Learn

Regardless of which tool you need/choose, I believe one of your root issues is the URL is incomplete. Also, verify that the issues Rob brings up do not apply.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.