用powershell更新user profile service中的一个字段

http://blog.karstein-consulting.com/2010/12/23/read-and-write-user-profile-properties-of-sharepoint-2010-user-profile-service-application-with-powershell/

I needed to change some properties of user profiles of my SharePoint 2010 farm.

Of cource I wanted to do this with PowerShell!

Before you can execute the following script, be sure that your user account is “Admin” on your “User Profile Service Application” and has “Full Control” permissions on the same Service App! – You may get this error: New-Object : Exception calling ".ctor" with "1" argument(s): "No User Profile Application available to service the request. Contact your farm administrator." – You can verify or change this settings in the Central Administration –> Manage Service Applications –> (select User Profile Service Application) –> than:

  1. Click “Administrators” –> Add the user that will execute the PowerShell script –> Set “Full Control” for this user –> Click “OK”
  2. Click “Permissions” –> Add the user that will execute the PowerShell script –> Set “Full Control” for this user –> Click “OK”

So…

Here is the Script for reading properties from the profile system:

#region Check x64 host
if( [System.IntPtr]::Size -ne 8) {
  Write-Error "Please use a x64 PowerShell host!"
  return
}
#endregion

#region Load SharePoint SnapIn and DLL
  Remove-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  
  [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | out-null    
  
  #Check available SharePoint Cmdlets  if( (Get-Command -Noun SPWeb*) -eq $null ) {
    Write-Error "SharePoint SnapIn not loaded. SharePoint cmdlets missing!"
    return
  }
#endregion

#User must be set as Full Control *Admin* and with Full Control *Permission* on Central Admin->Manager Service Application->User Profiler Service App!!
$site=(Get-SPSite "http://sharepoint.local")
$sc = [Microsoft.Office.Server.ServerContext]::GetContext($site)

$upm = New-Object "Microsoft.Office.Server.UserProfiles.UserProfileManager" -ArgumentList ($sc)
$enumerator = $upm.GetEnumerator()
$enumerator.Reset()
while( $enumerator.MoveNext() -eq $true ) {
  $currentUserProfile = $enumerator.Current
  $propertiesCollection = $currentUserProfile.ProfileManager.PropertiesWithSection
  if( $currentUserProfile -ne $null -and $propertiesCollection -ne $null ) {
    foreach($p in $propertiesCollection) {
      Write-Output $p.Name $currentUserProfile[$p.Name].Value
    }
    
    #write to a property:    #$currentUserProfile["FirstName"].Value="Test"    #$currentUserProfile.Commit()  }
}

If you need to write property values than uncomment the two lines on the bottom of the script and change them.

posted @ 2012-12-22 17:07  举重-若轻  阅读(337)  评论(0编辑  收藏  举报