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"

 

转载于:https://www.cnblogs.com/splearning/p/4038392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值