首页 > 代码库 > 用powershell批量新增user profile

用powershell批量新增user profile

 SharePoint 2013 新系统,要在User Profile Service里把人全加一下,其实同步ad更方便,但ad里的人太多,没必要全要,只要大中华区就行了,问hr要了一份人员名单,写了个脚本

 

先来个xml,把要加的人ad账号列一下

<Users>
  <UserName Name="user_a" />
  <UserName Name="user_b" />
  <UserName Name="user_c" />
  <UserName Name="user_d" />
  <UserName Name="user_e" />
  <UserName Name="user_f" />

</Users>

 

再来一段简单的脚本

 

#********************************************************************
# Create New User
#
#********************************************************************

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.SharePoint.Powershell‘}
if ($snapin -eq $null)
{
  Write-Host "Loading SharePoint Powershell Snapin..."
  Add-PSSnapin "Microsoft.SharePoint.Powershell"
  Write-Host "SharePoint Powershell Snapin Loaded"
}
# Get XML Configuration file
[xml]$xmlData=Get-Content "C:\\test.xml"

Write-host ""
Write-host -f Yellow "Starting create new user"

# create a connection to the User Profile Manager
$MySite = Get-SPSite <siteUrl>
$context = Get-SPServiceContext $MySite

# Get UserProfileManager Object
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

$xmlData.Users.UserName | ForEach-Object {

  if($profileManager.UserExists($_.Name) -eq $false){
    $profileManager.CreateUserProfile($_.Name)
    Write-host -f Green $_.Name"created"
  }

  else{
    Write-host -f Red $_.Name"already exist"
  }

}