Import and Export users from user profile service By Powershell

Sharepoint中有2个服务User Profile ServiceUser Profile Synchronization Service跟用户息息相关,其中user profile service是user profile serivce application可运行的前提。

User Profile Synchronization Service:用于同步AD账号信息到Sharepoint Farm(实际是到User profile database),我们在配置user profile service application会创建user Profile相关的数据库

User Profile Service:用于承载user profile service application的正常运行。

user profile service application只有导入数据(通过配置Configure Synchronization Connections),并没有提供导出数据接口。

另外导入数据也不灵活,比如我们只想导入特定的数据,而不是整个AD, 对于这种特殊的需求通过powershell可以得到解决。

至于如何启动User profile service和user profile synchronization servie,配置user profile service application本文就不细说了

本文的学习目标是:如何用powershell来导入和导出用户信息

  • Powershell 导入用户到user profile service 
    $path=‘c:\userlist.csv‘
    $site = $caUrl
    $context = Get-SPServiceContext -Site $site
    $web = Get-SPWeb -Identity $site
    try
    {
    $upm = New-Object -TypeName Microsoft.Office.Server.UserProfiles.UserProfileManager -ArgumentList $context
    }
    catch{}
    if($upm -ne $null)
   {
        $userslist = Import-Csv -Path $path
        $r = 1;
        for($count=0; $count -lt $userslist.Count; $count++)
            {
                 $user_name = $domain +"\"+ $userslist[$count].UserID -replace " ", ""
                 if ($upm.UserExists($user_name)) {} 
                 else  
                 {
                     try
                     {
                         $profileproperty = $upm.CreateUserProfile($user_name)
                $profileproperty["Manager"].Value=‘‘
                $profileproperty["Mail"].Value=‘‘
                $profileproperty["Title"].Value=‘‘
                           $profileproperty.Commit()                       
                     }
                     catch
                     {
                      Write-Host -f red ([String]::Format("{0} Not Exist",$user_name));
                     }
                 } 
            }
    }
  • Powershell 从User profile service中导出用户
$siteUrl = "ca url"
$outputFile = "C:\sharepoint_user_profiles.csv"
$serviceContext = Get-SPServiceContext -Site $siteUrl
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
$profiles = $profileManager.GetEnumerator()

$collection = @()
foreach ($profile in $profiles) {
 
   $profileData = "" | 
   select "AccountName", "PreferredName" , "Manager" , "Office" , "Location" , "WorkEmail" , "Assistant" , "AboutMe" , "Language" , "PictureURL" , "Role"
   
   $profileData.AccountName = $profile["AccountName"]
   $profileData.PreferredName = $profile["PreferredName"]
   $profileData.Manager = $profile["Manager"]
   $profileData.Office = $profile["Office"]
   $profileData.Location = $profile["Location"]
   $profileData.WorkEmail = $profile["WorkEmail"]
   $profileData.Assistant = $profile["Assistant"]
   $profileData.AboutMe = $profile["AboutMe"].Value
   $profileData.Language = $profile["Language"]
   $profileData.PictureURL = $profile["PictureURL"]
   $profileData.Role = $profile["Role"]   
   #$collection += $profileData | ConvertTo-Html -Fragment
   $collection += $profileData
}

$collection | Export-Csv $outputFile -NoTypeInformation -encoding "UTF8"

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。