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!