当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 获取所有用户配置文件

作者:精品下载站 日期:2024-12-14 15:44:46 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 获取所有用户配置文件


要求:使用 PowerShell 获取 SharePoint Online 中的所有用户配置文件。

如何在 SharePoint Online 中获取用户配置文件?

SharePoint 中的用户配置文件是存储有关用户的信息、提供网站成员身份并跟踪使用情况数据的中心位置。它支持“我的网站”、社交功能以及跨租户中的多个站点共享配置文件。要获取 SharePoint Online 中的所有用户配置文件,请执行以下操作:

  1. 登录 SharePoint Online 管理 >> 单击左侧导航中的“更多功能” >> 单击“用户配置文件”旁边的“打开”按钮。

    [玩转系统] SharePoint Online:使用 PowerShell 获取所有用户配置文件

  2. 在“用户配置文件”页面中,单击“人员”选项卡下的“管理用户配置文件”。使用搜索来获取用户的用户配置文件。

    [玩转系统] SharePoint Online:使用 PowerShell 获取所有用户配置文件

该页面帮助我们一一获取各个用户的个人资料和属性。现在,让我们看看如何使用 PowerShell 获取 SharePoint Online 中的所有用户配置文件。

SharePoint Online:用于获取所有用户配置文件的 PowerShell

让我们使用 PowerShell 获取 SharePoint Online 中的所有用户配置文件数据和报告。在执行此脚本之前,请确保已安装 Azure AD PowerShell 模块。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
 
#Variables
$AdminSiteUrl = "https://crescent-admin.sharepoint.com"
$CSVPath = "C:\Temp\UserProfiles.csv"

#Get Credentials
$Cred = Get-Credential

#Connect to AzureAD
Connect-AzureAD -Credential $Cred

#Get All Users from AzureAD
$AllUsers = Get-AzureADUser -All:$True
Write-host "Total Number of User Profiles Found:"$AllUsers.Count

#Setup the context
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
$Ctx.Credentials = $Credentials

#Collect User data
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
$UserDataCollection = @()

$Counter = 1
ForEach($User in $AllUsers)
{
    #Get User Profile
    $UserProfile = $PeopleManager.GetPropertiesFor(('i:0#.f|membership|'+$User.UserPrincipalName))
    $Ctx.Load($UserProfile)
    $Ctx.ExecuteQuery()

    Write-Progress -Activity "Extracting User Profile Data..." -Status "Getting User Profile $Counter of $($AllUsers.Count)" -PercentComplete (($Counter / $AllUsers.Count)  * 100)
    #Get User Profile Data
    $UserData = New-Object PSObject
    ForEach($Key in $UserProfile.UserProfileProperties.Keys)
    {   
        $UserData | Add-Member NoteProperty $Key($UserProfile.UserProfileProperties[$Key])
    }
    $UserDataCollection += $UserData
    $Counter++
}

#Export Data to CSV File
$UserDataCollection | Export-Csv -Path $CSVPath -NoTypeInformation

此 PowerShell 获取 SharePoint Online 中的所有用户配置文件和属性。

[玩转系统] SharePoint Online:使用 PowerShell 获取所有用户配置文件

使用 PnP PowerShell 导出所有用户的用户配置文件属性值

此 PowerShell 脚本使用 Get-PnPUserProfileProperty cmdlet 从所有用户配置文件获取“Office”属性值,并将其导出到 CSV 文件:


#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$SPOPropertyName = "Office"
$CSVPath = "C:\Temp\UserProfileProperty.csv"

Try {
    #Connect to AzureAD
    Connect-AzureAD
 
    #Get All Users of the Domain from AzureAD
    $AllUsers = Get-AzureADUser -All:$True -Filter "UserType eq 'Member'"
    Write-host "Total Number of User Profiles Found:"$AllUsers.Count 
 
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Interactive
 
    #Iterate through All Users
    $Counter = 1
    $UserProfileData = @()
    ForEach($User in $AllUsers)
    {
        Write-host "`nGetting User Profile Property for: $($User.UserPrincipalName)" -f Yellow
 
        #Get the User Property value from SharePoint   
        $UserProfile = Get-PnPUserProfileProperty -Account ($User.UserPrincipalName)
 
        #Collect the data
        $UserProfileData += New-Object PSObject -Property @{
            'User Account' = $UserProfile.UserProfileProperties["UserName"]
            'Location' = $UserProfile.UserProfileProperties[$SPOPropertyName]
        }

        $Counter++
        Write-Progress -Activity "Getting User Profile Data..." -Status "Getting User Profile $Counter of $($AllUsers.Count)" -PercentComplete (($Counter / $AllUsers.Count)  * 100)
    }
    #Export the data to CSV
     $UserProfileData | Export-Csv $CSVPath -NoTypeInformation
 
     write-host -f Green "User Profiles Data Exported Successfully to:" $CSVPath
}
Catch {
    write-host -f Red "Error Getting User Profile Property!" $_.Exception.Message
}

总之,使用 PowerShell 脚本检索 SharePoint Online 中所有用户配置文件的列表是管理和组织组织内用户信息的强大而有效的方法。通过使用 PowerShell,您可以轻松地从 SharePoint Online 租户检索所有用户配置文件的列表。您可以使用 Microsoft Excel 进一步分析、排序和过滤用户配置文件数据。

这是我获取所有用户配置文件属性的另一篇文章:SharePoint Online:PowerShell 获取用户配置文件属性

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯