Thursday 1 December 2016

Add User Profiles in SharePoint 2013 using PowerShell Script

Step 1: Generate the list of accounts to be created

Not really applicable in a real environment where you would have real accounts, but for my development environment I decided to create a Xml file using PowerShell with various elements for a User such as FirstName, LastName, EmailAddress etc.

<User>
 <Account>User0001</Account>
 <EmailAddress>User0001@WORKGROUP.com</EmailAddress>
 <FirstName>FirstName_User1</FirstName>
 <LastName>LastName_User1</LastName>
</User>
Here is the script which generate the specified number of user data in form of an xml file. It takes the number of accounts to generated as input and results into a Users.xml file.

$Path = (Get-Item -Path ".\" -Verbose).FullName + "\Users.xml"
 
$XmlWriter = New-Object System.XMl.XmlTextWriter($Path,$Null)
 
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
 
$xmlWriter.WriteStartDocument()
 
# create root element "Users" and add some attributes to it
$XmlWriter.WriteComment('List of Users')
$xmlWriter.WriteStartElement('Users')

$nrOfUsers = Read-Host 'Please enter the numbers of users you want to create:'
 
# add a couple of random entries
for($x=1; $x -le  [int]$nrOfUsers; $x++)
{
    $User = 'User{0:0000}' -f $x
    $email = $User +  '@' +  (gwmi WIN32_ComputerSystem).Domain
 
    $guid = [System.GUID]::NewGuid().ToString()
 
    # each data set is called "User"
    $xmlWriter.WriteStartElement('User')
   
    # add three pieces of information:
    $xmlWriter.WriteElementString('Account',$User)
    $xmlWriter.WriteElementString('EmailAddress',$email)
    $xmlWriter.WriteElementString('FirstName','User')
 $xmlWriter.WriteElementString('LastName',$x)
 
    # close the "User" node:
    $xmlWriter.WriteEndElement()
}
 
# close the "Users" node:
$xmlWriter.WriteEndElement()
 
# finalize the document:
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()

Step 2: Create local account using PowerShell

PowerShell is very useful for automating Active Directory. It allows to quickly and relatively easy automate mundane actions or perform same operations with many objects.
PowerShell provides very broad set of methods to work with Active Directory. There is some of them:
  • Microsoft Active Directory Module for Windows PowerShell
  • ADSI adapter
  • .Net Classes
  • Non Microsoft free extensions, such as Quest Active Directory Cmdlets or AD provider from PowerShell Community Extensions.
Read more about these methods here on MSDN.

$HostName =(gwmi WIN32_ComputerSystem).Name
[ADSI]$server= "WinNT://$HostName" 

$localUsers = $server.Children | where {$_.SchemaClassName -eq 'user'}  |  % {$_.name[0].ToString()}

#Add a new local user after checking that it does not exist
if($localUsers -NotContains $user.Account){
 $Account = $server.Create("User", $user.Account) 
 $password = [System.Web.Security.Membership]::GeneratePassword(10,2)
 $Account.SetPassword($password)
 $Account.SetInfo() 
}
else{
 Write-Host "User Account " $user.Account " already exists"
}

Step 3: Add the User Profile to SharePoint 2013 using PowerShell

You also need to add the user account which is running the PowerShell script to have the Full Control on the User Profile Service application via the Central Administration. To add the permission:
  • Go to Central Administration
  • Go to Manage Service Applications
  • Select User Profile Service Application and click on the Permissions button in the ribbon.
  • Select the user who is running the script and give him ‘Full Control’ access.
User Profile Service Application Permission
You must create the UserProfileManager object before you access a UserProfile object. You can then retrieve the user profiles that the user can access. Full access to everyone’s user profile requires “Manage User Profiles” rights. Full access to your own profile requires “Use Personal Features” rights. Everyone has read access to all profiles.

#instantiate the user profile manager
$mysite = new-object microsoft.sharepoint.spsite($siteurl)
$servicecontext = get-spservicecontext($mysite)
$userprofilemanager = new-object microsoft.office.server.userprofiles.userprofilemanager($servicecontext)
Next, use the userprofilemanager object to check if the profile exists for this particular user. If it does not, the create a new user profile as shown below.
However, this is a crude implementation as I wanted to use it one time. To make it even better, you might also update the existing profile if any with the new data specified in the xml file.

#Add a new user profile for the user
if($userprofilemanager.UserExists($user.Account)){
 Write-Host "User Profile for " $user.Account " already exists"
}
else{
 $newprofile = $userprofilemanager.createuserprofile($user.Account)
 $newprofile.DisplayName = $user.Account
 $newprofile["FirstName"].add($user.FirstName)
 $newprofile["LastName"].add($user.LastName)
 $newprofile["WorkEmail"].add($user.EmailAddress)
 $newprofile.commit()
}    
If you run the CreateUserProfiles.ps1 from the script download, it asks you the following parameters to enter.
  • Number of users to be created. I entered 1000 in this case which seemed to be created without any issues.
  • Site Url of the Central Administration site. This is required to get the context of the service application.
Create User Profile
And the result is as below. Without any manual effort, we have 1000 user profiles ready in our development environment.
User Profiles

No comments: