Add-ServiceAccounts.ps1


<#PSScriptInfo
.VERSION 2.3
.GUID a8d133a6-dc3b-4dbf-a6f5-1ea8abcbb7bd
.AUTHOR
 Maarten Peeters - SharePointFire - https://sharepointfire.com
.COMPANYNAME
 SharePointFire
.COPYRIGHT
.TAGS
 SharePoint, Active Directory, Service Accounts
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
 ActiveDirectory
.RELEASENOTES
 Version 1.0: Original published version.
 Version 2.0: Removed function
 Version 2.1: Changed Admin to Install
 Version 2.2: Fixed A positional parameter cannot be found that accepts argument
 Version 2.3: Fixed A positional parameter cannot be found that accepts argument
#>
 

<#
.SYNOPSIS
 Simple Function to create needed SharePoint service accounts
 
.DESCRIPTION
 Simple Function to create needed SharePoint service accounts.
 Each service account will receive an unique password.
 
.PARAMETER OU
 Enter the full path to the OU where to add the service accounts.
 For example: OU=Service Accounts,OU=SPFire,DC=sharepointfire,DC=com
 
.PARAMETER UPNSuffix
 Enter the UPNSuffix to be used during creation
 For example: sharepointfire.com
 
.PARAMETER Prefix
 Specify the prefix to be used for the service accounts.
 For example: SA_SP2019 which will create service accounts like SA_SP2019Farm and SA_SP2019Install
 
.PARAMETER LogPath
 Enter the full path to store a .csv file (; delimited) of the created service accounts with their unique password
 For example: C:\Install
 
.EXAMPLE
 Add-ServiceAccounts.ps1 -OU "OU=Service Accounts,OU=SPFire,DC=sharepointfire,DC=com" -UPNSuffix "SharePointFire.com" -Prefix "SA_SP2019" -LogPath "C:\Install"
 
 .NOTES
 Version: 2.3
 Author: Maarten Peeters
 Creation Date: 29-07-2018
 Purpose/Change: Fast creation of Service Accounts
#>


param(
    [Parameter(mandatory=$true)]
    [string] $OU,
    [Parameter(mandatory=$true)]
    [string] $UPNSuffix,
    [Parameter(mandatory=$true)]
    [string] $Prefix,
    [Parameter(mandatory=$true)]
    [string] $LogPath
)

#Array of accounts to be created. Add names if needed as for example Visio Unattented userID
$Accounts = "Install", "Farm", "Services", "Pool", "MySitePool", "Crawl", "Sync", "C2WTS", "SU", "SR"

try{
    #Verify if Active Directory Module is available
    if (Get-Module -ListAvailable -Name activedirectory) {
        #Import Active Directory Module
        import-module activedirectory -ErrorAction SilentlyContinue

        #Verify if the OU exists
        if(get-adorganizationalunit -Filter { DistinguishedName -eq $OU }) {

            #Test if logpath exists
            If(Test-Path $LogPath) { 
                #Loop through all accounts and create them
                foreach($Account in $Accounts){
                    $Password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..16 | Sort-Object {Get-Random})[0..15] -join ''
                    New-ADUser -Name "$($Prefix)$($Account)" -SamAccountName "$($Prefix)$($Account)" -DisplayName "$($Prefix)$($Account)" -UserPrincipalName "$($Prefix)$($Account)@$($UPNSuffix)" -Path $OU -Enabled $true -ChangePasswordAtLogon $false -PasswordNeverExpires $true -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force) -PassThru | out-null
                    $Log += "$($Prefix)$($Account);$($Password) `n"
                }
                $Log | out-file -FilePath "$($LogPath)\SharePointAccounts$((get-date).tostring('sshhMMddyyyy')).csv"
                Write-Host "Accounts created and log located on $($LogPath)" -foregroundcolor green
            } Else { 
                Write-Host "The path $($LogPath) could not be found. Please enter a correct path to store the passwords" -foregroundcolor yellow
            }
        }  else  {
            Write-Host "The OU $($OU) could not be found. Please enter a correct OU to store the accounts" -foregroundcolor yellow
        }
    } else {
        Write-Host "Active Directory module not loaded. Please install Active Directory Management Tools" -foregroundcolor yellow
    }
}
catch{
    write-host "Error occurred: $($_.Exception.Message)" -foregroundcolor red
}