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

[玩转系统] 在 SharePoint Server 中使用 PowerShell 获取和导出用户配置文件属性

作者:精品下载站 日期:2024-12-14 14:03:55 浏览:13 分类:玩电脑

在 SharePoint Server 中使用 PowerShell 获取和导出用户配置文件属性


您是否需要从 SharePoint 导出用户配置文件属性以用于报告或其他目的?从用户配置文件属性准备报告是 SharePoint 中的常见要求。以下是我的 PowerShell 脚本,用于获取 SharePoint 中的用户配置文件属性。

重要提示:在运行这些脚本之前,请确保 User Profile Service 应用程序已创建、启动并运行!并以农场管理员身份运行!

使用 PowerShell 获取 SharePoint 2013/2016 中的用户配置文件属性

PowerShell 是一种多功能工具,可用于自动执行 SharePoint 中的许多任务。在这篇博文中,让我们获取给定用户的用户配置文件属性值:


#Configuration Variables
$SiteURL = "https://mysite.crescent.com"
$UserLogin="Crescent\Salaudeen"

#Get Objects
$ServiceContext  = Get-SPServiceContext -site $SiteURL
$UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

#Get the User Profile
$UserProfile = $UserProfileManager.GetUserProfile($UserLogin) 

#Retrieve User Profile Properties
Write-host $UserProfile["PreferredName"]
Write-host $UserProfile["WorkEmail"]

您不仅限于这些字段,但用户配置文件中还有更多字段。要获取所有可用的用户配置文件属性,请使用:


$UserProfile.Properties | Select DisplayName, Name

PowerShell 查询和导出 SharePoint 中所有用户的用户配置文件属性:

让我们使用 PowerShell 获取所有用户配置文件属性并将其导出到 CSV。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "https://portal.crescent.com"
$outputReport = "c:\user_profiles.csv"

#Get Objects
$ServiceContext  = Get-SPServiceContext -site $SiteURL
$UPM = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

#Get All User Profiles
$UserProfiles = $UPM.GetEnumerator()
Write-host "Total Number of User Profiles found:"$UserProfiles.Count

#Array to hold Profiles
$ProfileDataCollection = @() 

#Iterate through each profile
foreach ($Profile in $UserProfiles)
{
    $ProfileData = New-Object PSObject 
    #Retrieve User profile Properties  
    $ProfileData | Add-Member -MemberType NoteProperty -name "Account" -value $Profile["AccountName"]
    $ProfileData | Add-Member -MemberType NoteProperty -name "Display Name" -value $Profile["PreferredName"]
    $ProfileData | Add-Member -MemberType NoteProperty -name "E-Mail" -value $Profile["WorkEmail"]
    $ProfileData | Add-Member -MemberType NoteProperty -name "Manager" -value $Profile["Manager"]
    $ProfileData | Add-Member -MemberType NoteProperty -name "Department" -value $Profile["Department"]
   
    #Add to Array
    $ProfileDataCollection+=$ProfileData
    write-host "Processed Profile:"$profile["PreferredName"]
}
#Export User Profile data to CSV
$ProfileDataCollection | Export-Csv $outputReport -NoType

此脚本将所有用户的给定用户配置文件属性导出为 CSV 文件:

[玩转系统] 在 SharePoint Server 中使用 PowerShell 获取和导出用户配置文件属性

如果要从 SharePoint Online 导出用户配置文件属性,请使用:SharePoint Online:使用 PowerShell 将用户配置文件属性导出到 CSV

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

取消回复欢迎 发表评论:

关灯