Search This Blog

Wednesday, March 19, 2014

SharePoint Service account creation with PowerShell from CSV


Quick easy way to create Service Accounts for SharePoint.

Users.csv (layout):
FirstName, LastName, SamAccountName, -acc password-
sp_cacheSuperUser, sp_cacheSuperUser, sp_cacheSuperUser, -acc password-
sp_cacheSuperReader,sp_cacheSuperReader,sp_cacheSuperReader, -acc password-
sp_farm,sp_farm,sp_farm, -acc password-
sp_portal_mysite,sp_portal_mysite,sp_portal_mysite, -acc password-
sp_portal_intranet,sp_portal_intranet,sp_portal_intranet, -acc password-
sp_searchContent,sp_searchContent,sp_searchContent, -acc password-
sp_searchService,sp_searchService,sp_searchService, -acc password-
sp_services,sp_services,sp_services, -acc password-
sp_userProfiles,sp_userProfiles,sp_userProfiles, -acc password-

PowerShell Script:
# Created this script using Windows Server 2012# Import Active Directory module
Import-Module ActiveDirectory -ErrorAction SilentlyContinue

# Set OU for the user accounts
$OU = “OU=SharePoint,OU=Service Accounts,DC=dev,DC=local”

# Get domain name
$dnsroot = ‘@’ + (Get-ADDomain).dnsroot

# Import the file with the users.
$users = Import-Csv .\users.csv #-Delimiter “;”

Write-Output $users

foreach ($user in $users)
{
    # Get password from CSV File
    $Password = ConvertTo-SecureString $user.Password -asPlaintext -Force

    # Create Accounts
    #$FullName = ($user.FirstName + ” ” + $user.LastName)
    $FullName = ($user.LastName)
    $UserPrincipalName = ($user.SamAccountName + $dnsroot)

    try
    {
        New-ADUser -SamAccountName $user.SamAccountName –Name $FullName –DisplayName $FullName -GivenName $user.FirstName -Surname $user.LastName –UserPrincipalName $UserPrincipalName -Enabled $true -ChangePasswordAtLogon $false -PasswordNeverExpires $false -Path $OU -AccountPassword $Password -PassThru | Out-Null
        Write-Output “Created user $($user.SamAccountName)
    }
    catch [System.Object]
    {
        Write-Output “Could not create user $($user.SamAccountName), $_
    }

No comments: