且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 SharePoint 在线租户获取所有用户并通过 Powershell 设置用户配置文件属性

更新时间:2021-09-14 07:42:20

正如您提到的,没有使用 SharePoint Online CSOM API 在 SharePoint 租户中获取所有用户配置文件的直接方法.但您可以考虑以下方法:

As you mentioned, there is no straightforward ways of getting all user profiles within SharePoint tenant using SharePoint Online CSOM API. But you could consider the following approach:

  • utilize Azure Active Directory Module for Windows PowerShell to retrieve all the users in tenant (Get-MsolUser cmdlet)
  • iterate users and utilize SharePoint User Profiles CSOM API to retrieve user profile

示例

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"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"



Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
{
   $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
   $context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
   return $context
}


Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
   $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
   return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}



Function Print-UserProfileInfo([Microsoft.SharePoint.Client.UserProfiles.PeopleManager]$PeopleManager,[string]$AccountName){
   $ctx = $PeopleManager.Context
   $accountName = "i:0#.f|membership|" + $AccountName  #claim format  
   $userProfile = $PeopleManager.GetPropertiesFor($AccountName)
   $ctx.Load($userProfile)
   $ctx.ExecuteQuery()
   Write-Host $userProfile.PersonalUrl
}




$tenantUrl = "https://contoso.sharepoint.com/"
$userName = "jdoe@contoso.onmicrosoft.com" 
$password = "password"

$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($userName, $secPassword)
Connect-MsolService -Credential $cred
$allUsers = Get-MsolUser




$Context = Get-SPOContext -Url $tenantUrl -UserName $userName -Password $password
$peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
$allUsers |  % { Print-UserProfileInfo -PeopleManager $peopleManager -AccountName $_.UserPrincipalName }
$Context.Dispose()