Security in Obscurity

A script by Jeff Stein

Run-ADusercreation.ps1

Automates the provisioning of active directory user accounts, November 2016

The script entitled "Run-ADusercreation" will perform the necessary steps to create an active directory user account for each user contained in a CSV file. The script is designed for the Microsoft Windows operating system, leveraging PowerShell 4.0 and above.

DISCLAIMER

This script is offered 'as is' with no warranty. While it has been tested and verified to work in my environment, it is recommended that you test this script in a test environment before utilizing in your own production environment.

Installation

To get started download the files listed in the resource section and save the Run-ADusercreation.txt as a .ps1 file. It should be extracted to the desktop of the Windows machine where the script will run from. The CSV template file should be placed in the same directory. The Active Directory module should be installed on the machine for the script to run properly. Once extracted, the Run-ADusercreation.ps1 file can be right-clicked and "Run with PowerShell" can be selected. It is recommended that this is an account that has administrator privileges. Alternatively, PowerShell can be manually opened on the system and the terminal navigated to the appropriate directory where the script is located and executed from there. With either option, it is recommended the action to run the script is done with an account that has administrator privileges.

Overview

The script will create an active directory user account for each user added to the provided CSV file. You can see an sample of the code below:

Import CSV file

$csvFilename = ".\users.csv"
$csv = Import-Csv $csvFilename -Header @("FirstName","LastName","Username","Title","Department","Manager") | select -Skip 1

After importing the CSV file and stripping the template header from it the commands are run to provision each account using a foreach loop generating the following fields:

  1. First Name
  2. Last Name
  3. Full Name
  4. Display Name
  5. Username
  6. Job Title
  7. Department name or code
  8. Assigned Manager
You can see the sample code to do this below:

Account Creation

foreach ($line in $csv) {
New-ADUser -Name $line.FirstName+$line.LastName -DisplayName $line.FirstName+$line.LastName -GivenName $line$FirstName -Surname $line$LastName -SamAccountName $line.$Username -Title $line.$Title -Department $line.$Department -Manager $line.$Manager
}


Tags

Security Vulnerabilities IDS/IPS Malware Policies PowerShell Python Splunk Cloud Script PKI