【HOW】用PowerShell腳本修改用戶配置文件屬性顯示次序

首先將以下腳本保存爲PowerShell文件,如:ReorderUserProfileProperty.ps1。shell

在執行此腳本時,若是不輸入任何參數,將列出全部用戶配置文件屬性的名稱和顯示次序;若是隻輸入屬性名稱,則顯示此屬性的顯示次序;若是輸入了屬性名稱和顯示次序,則修改此屬性的顯示次序。app

####################################################################################
# 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()

在使用此結果過程當中,可能會遇到以下問題,其解決辦法以下:wordpress

1. 在執行腳本時提示以下錯誤:ui

New-Object : 使用「1」個參數調用「.ctor」時發生異常:「沒有用戶配置文件應用程序可用於處理請求。

【解決辦法】:
辦法一:在 管理中心 > 應用程序管理 > 管理服務應用程序 頁面中,選中「User Profile Service Application」(但不要跳轉),而後在Ribbon的「管理員」和「權限」中給當前用戶賦予「Full Control」權限。
辦法二:「以管理員身份運行」SharePoint 2010 Management Shell。spa

2. 管理用戶屬性頁面顯示異常code

有時,在執行完腳本後  管理中心 > 應用程序管理 > 管理服務應用程序 > User Profile Service Application > 管理用戶屬性 頁面會提示屬性信息錯誤。orm

【解決辦法】:在頁面中對任意屬性手工排序,則頁面顯示將恢復正常。blog

3. 屬性顯示次序不會自動重排排序

在修改某一屬性的顯示次序時,其餘相鄰屬性的次序不會自動重排。所以,在用此腳本單獨修改屬性後,可能會出現相同顯示次序的屬性,從而達不到指望的顯示次序。get

【解決辦法】:規劃好各屬性的顯示次序後,按以下格式的腳本統一執行:

.\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/

相關文章
相關標籤/搜索