【HOW】用PowerShell脚本修改用户配置文件属性显示次序

时间:2023-03-08 16:38:12
【HOW】用PowerShell脚本修改用户配置文件属性显示次序

首先将如下脚本保存为PowerShell文件,如:ReorderUserProfileProperty.ps1。

在执行此脚本时,如果不输入任何参数,将列出所有用户配置文件属性的名称和显示次序;如果只输入属性名称,则显示此属性的显示次序;如果输入了属性名称和显示次序,则修改此属性的显示次序。

####################################################################################
# Function: Re-order a user profile property, or show display order of a or all properties.
# Return: If $PropertyName is null, the display order of all properties will be shown.
# if $DisplayOrder is null, the display order of $PropertyName will be shown.
# Otherwise, the display order of $PropertyName will be re-ordered.
####################################################################################
param(
[string]$PropertyName,
[int]$DisplayOrder,
[string]$SiteUrl
) [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles.UserProfileManager")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") if(!$SiteUrl)
{
$SiteUrl = 'http://SharePointServer:80/'
} $site = new-object Microsoft.SharePoint.SPSite($SiteUrl);
$context = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
if (!$context)
{
write-host "context is empty, quitting!"
break
} $UPAConfMgr = new-object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($context)
if (!$UPAConfMgr)
{
write-host "confmgr is empty, quitting!"
break
} $userprofiletype = [Microsoft.Office.Server.UserProfiles.ProfileType]::User
$userProfileSubTypeManager = [Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::Get($context)
$userProfile = $userProfileSubTypeManager.GetProfileSubtype([Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName($userprofiletype))
$userProfileProperties = $userProfile.Properties if($PropertyName)
{
$sec = $userProfileProperties.GetPropertyByName($PropertyName)
Write-Host 'Current Order: '$sec.DisplayOrder
}
else
{
$userProfileProperties | Format-Table -Property Name,DisplayOrder,DisplayName -AutoSize
} if ($DisplayOrder)
{
$userProfileProperties.SetDisplayOrderByPropertyName($PropertyName,$DisplayOrder)
Write-Host 'New Order: '$sec.DisplayOrder
} $userProfileProperties.CommitDisplayOrder()

在使用此结果过程中,可能会遇到如下问题,其解决办法如下:

1. 在执行脚本时提示如下错误:

New-Object : 使用“1”个参数调用“.ctor”时发生异常:“没有用户配置文件应用程序可用于处理请求。

【解决办法】:
办法一:在 管理中心 > 应用程序管理 > 管理服务应用程序 页面中,选中“User Profile Service Application”(但不要跳转),然后在Ribbon的“管理员”和“权限”中给当前用户赋予“Full Control”权限。
办法二:“以管理员身份运行”SharePoint 2010 Management Shell。

2. 管理用户属性页面显示异常

有时,在执行完脚本后  管理中心 > 应用程序管理 > 管理服务应用程序 > User Profile Service Application > 管理用户属性 页面会提示属性信息错误。

【解决办法】:在页面中对任意属性手工排序,则页面显示将恢复正常。

3. 属性显示次序不会自动重排

在修改某一属性的显示次序时,其他相邻属性的次序不会自动重排。因此,在用此脚本单独修改属性后,可能会出现相同显示次序的属性,从而达不到期望的显示次序。

【解决办法】:规划好各属性的显示次序后,按如下格式的脚本统一执行:

.\ReorderUserProfileProperty.ps1 MIS-StaffNo 11
.\ReorderUserProfileProperty.ps1 Department 12
.\ReorderUserProfileProperty.ps1 MIS-JobTitle 13
.\ReorderUserProfileProperty.ps1 SPS-JobTitle 14

参见:
http://www.sharepointstuffs.com/creating-managing-and-ordering-user-profile-properties-using-powershell-in-sharepoint-2013/
http://littletalk.wordpress.com/2010/12/09/no-user-profile-application-available-to-service-the-request/