Last active
February 22, 2018 21:23
-
-
Save JaekelEDV/99c5e9756bdbac96fc2d16ff90d12188 to your computer and use it in GitHub Desktop.
Powershell Script to create LabUsers based on csv-file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name | SamAccountName | UPN | GivenName | Surname | DisplayName | EmailAddress | Group | Department | |
---|---|---|---|---|---|---|---|---|---|
Jasper Beardley | Jasper | [email protected] | Jasper | Beardley | Jasper Beardley | [email protected] | Retired | Retired | |
Patty Bouvier | Patty | [email protected] | Patty | Bouvier | Patty Bouvier | [email protected] | Bouvier | Bouvier | |
Selma Bouvier | Selma | [email protected] | Selma | Bouvier | Selma Bouvier | [email protected] | Bouvier | Bouvier | |
Kent Brockman | Kent | [email protected] | Kent | Brockman | Kent Brockman | [email protected] | |||
Charles Montgomery Burns | Charles Montgomery | [email protected] | Charles Montgomery | Burns | Charles Montgomery Burns | [email protected] | Powerplant | Powerplant | |
Carl Carlson | Carl | [email protected] | Carl | Carlson | Carl Carlson | [email protected] | Powerplant | Powerplant | |
Maude Flanders | Maude | [email protected] | Maude | Flanders | Maude Flanders | [email protected] | Flanders | Flanders | |
Ned Flanders | Ned | [email protected] | Ned | Flanders | Ned Flanders | [email protected] | Flanders | Flanders | |
Rod Flanders | Rod | [email protected] | Rod | Flanders | Rod Flanders | [email protected] | Flanders | Flanders | |
Todd Flanders | Todd | [email protected] | Todd | Flanders | Todd Flanders | [email protected] | Flanders | Flanders | |
Matt Groening | Matt | [email protected] | Matt | Groening | Matt Groening | [email protected] | |||
Barney Gumble | Barney | [email protected] | Barney | Gumble | Barney Gumble | [email protected] | |||
Julius Hibbert | Julius | [email protected] | Julius | Hibbert | Julius Hibbert | [email protected] | |||
Bernice Hibbert | Bernice | [email protected] | Bernice | Hibbert | Bernice Hibbert | [email protected] | |||
Jimbo Jones | Jimbo | [email protected] | Jimbo | Jones | Jimbo Jones | [email protected] | |||
Edna Krabappel | Edna | [email protected] | Edna | Krabappel | Edna Krabappel | [email protected] | |||
Lenny Leonard | Lenny | [email protected] | Lenny | Leonard | Lenny Leonard | [email protected] | Powerplant | Powerplant | |
Helen Lovejoy | Helen | [email protected] | Helen | Lovejoy | Helen Lovejoy | [email protected] | |||
Timothy Lovejoy | Timothy | [email protected] | Timothy | Lovejoy | Timothy Lovejoy | [email protected] | |||
Otto Mann | Otto | [email protected] | Otto | Mann | Otto Mann | [email protected] | |||
Nelson Muntz | Nelson | [email protected] | Nelson | Muntz | Nelson Muntz | [email protected] | |||
Abraham Simpson | Abraham | [email protected] | Abraham | Simpson | Abraham Simpson | [email protected] | Retired | Retired | |
Bart Simpson | Bart | [email protected] | Bart | Simpson | Bart Simpson | [email protected] | Simpsons | Simpsons | |
Homer Simpson | Homer | [email protected] | Homer | Simpson | Homer Simpson | [email protected] | Simpsons | Simpsons | |
Lisa Simpson | Lisa | [email protected] | Lisa | Simpson | Lisa Simpson | [email protected] | Simpsons | Simpsons | |
Maggie Simpson | Maggie | [email protected] | Maggie | Simpson | Maggie Simpson | [email protected] | Simpsons | Simpsons | |
Marge Simpson | Marge | [email protected] | Marge | Simpson | Marge Simpson | [email protected] | Simpsons | Simpsons | |
Agnes Skinner | Agnes | [email protected] | Agnes | Skinner | Agnes Skinner | [email protected] | |||
Seymour Skinner | Seymour | [email protected] | Seymour | Skinner | Seymour Skinner | [email protected] | |||
Waylon Smithers | Waylon | [email protected] | Waylon | Smithers | Waylon Smithers | [email protected] | Powerplant | Powerplant | |
Moe Szyslak | Moe | [email protected] | Moe | Szyslak | Moe Szyslak | [email protected] | |||
Kirk van Houten | Kirk | [email protected] | Kirk | van Houten | Kirk van Houten | [email protected] | van Houten | van Houten | |
Luann van Houten | Luann | [email protected] | Luann | van Houten | Luann van Houten | [email protected] | van Houten | van Houten | |
Milhouse van Houten | Milhouse | [email protected] | Milhouse | van Houten | Milhouse van Houten | [email protected] | van Houten | van Houten | |
Clancy Wiggum | Clancy | [email protected] | Clancy | Wiggum | Clancy Wiggum | [email protected] | Wiggum | Wiggum | |
Ralph Wiggum | Ralph | [email protected] | Ralph | Wiggum | Ralph Wiggum | [email protected] | Wiggum | Wiggum | |
Sarah Wiggum | Sarah | [email protected] | Sarah | Wiggum | Sarah Wiggum | [email protected] | Wiggum | Wiggum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Function New-LabUsers creates User-Accounts and Groups for Lab Environments based on a csv-file. | |
.DESCRIPTION | |
This Script creates User-Accounts for a Lab based on a csv-file. | |
Be sure to save the csv as UTF.8. I prefer working with CSVed by Sam Francke, see here: http://csved.sjfrancke.nl/ | |
Right now the script will look for the headers Name,SamAccountName,UPN,GivenName,Surname,DisplayName,EmailAddress,Group,Department. | |
Of course you might add others as well. Adjust the csv and the hashtable for New-ADUser accordingly. | |
The users will get a Password which you might set in the parameter section below. | |
The Script has two mandatory Parameters (see the parameters help section): You must point to your csv-file and you must | |
specify a OU in which the users will be created. If this OU doesn't exist, the script will create it for you. | |
If users will be found in the csv that already exist in the AD, you'll get an info but the script will continue. | |
If there is a group-header in your csv, this group will also be created and the user will join this group. | |
You'll find a corresponding csv for a Lab-Domain named test.local and the most up-to-date version of this script at https://gist.github.com/JaekelEDV. | |
Rock it! | |
.PARAMETER CSVPath | |
Please enter the Path where your csv-file lives. | |
.PARAMETER OU | |
Please enter the Name of the OU where your new users shall live. There is no need of using the DistinguishedName - just write a name. | |
.EXAMPLE | |
New-LabUser -CSVPath .\testusers.csv -OU Foo | |
.NOTES | |
Author: Oliver Jäkel | [email protected] | @JaekelEDV | |
#> | |
Function New-LabUser | |
{ | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)][string] $CSVPath, | |
[Parameter(Mandatory = $true)][string] $OU | |
) | |
#region (=BEGIN) Starting Transcript, setting Variables checking if AD-Module is present and creating desired OU. | |
Begin | |
{ | |
#Set-StrictMode -Version 2.0 - Do not uncomment this. Just for further testing and developing. | |
Start-Transcript -Path $env:userprofile\Desktop\LOG-NewLabUser.txt -IncludeInvocationHeader | |
$ErrorActionPreference = 'SilentlyContinue' #Just to suppress the ugly ErrorMessages if an object already exists. | |
$LoadedModules = (Get-Module).Name | |
$CSVUser = Import-Csv -LiteralPath $CSVPath | |
$Password = (ConvertTo-SecureString -String 'Pa$$w0rd' -AsPlainText -Force) #Change the Password here if you like. | |
if ($LoadedModules -notcontains 'ActiveDirectory') | |
{ | |
Import-Module -Name ActiveDirectory | |
} | |
else | |
{ | |
Write-Verbose -Message 'ActiveDirectory Module already loaded' | |
} | |
$VerbosePreference = 'Continue' #No need to type -verbose when running the function. | |
$Domain = (Get-ADDomain).DistinguishedName | |
Try | |
{ | |
New-ADOrganizationalUnit -Name $OU -ProtectedFromAccidentalDeletion $false -Verbose | |
} | |
Catch | |
{ | |
Write-Verbose -Message "OU $OU already exists!" | |
} | |
$DestOU = (Get-ADOrganizationalUnit -Identity "ou=$OU,$Domain")#We need the DN in the next steps! | |
} | |
#endregion (=END BEGIN) | |
#region (=PROCESS) Importing csv-file, creating ADUsers and ADGroups and adding Users to Groups (when defined in csv) | |
Process | |
{ | |
foreach ($user in $CSVUser) | |
{ | |
if (Get-ADUser -Filter * -Properties SamAccountName| Where-Object {$_.SamAccountName -eq $User.SamAccountName}) | |
{Write-Verbose -Message "User $($User.SamAccountName) already exists!" | |
} | |
else | |
{ | |
$hash = @{ | |
Name = $user.Name | |
Displayname = "$($user.GivenName) $($user.Surname)" | |
Path = $DestOU | |
Samaccountname = $user.SamAccountName | |
UserPrincipalName = $user.UPN | |
Surname = $user.Surname | |
GivenName = $user.GivenName | |
EmailAddress = $user.EmailAddress | |
Department = $user.Department | |
AccountPassword = $Password | |
Enabled = $True | |
} | |
New-ADUser @hash -PassThru | |
} | |
if (Get-ADGroup -Filter * -Properties SamAccountName| Where-Object {$_.SamAccountName -eq $User.Group}) | |
{Write-Verbose -Message "Group $($User.Group) already exists!" | |
$groups = ($user).Department | |
$members = Get-ADUser -Filter * -SearchBase $DestOU -Properties department | Where-Object {$_.department -eq $groups} | |
Add-ADGroupMember -Identity $groups -Members $members | |
} | |
else | |
{ | |
New-ADGroup -Name $user.Group -SamAccountName $user.Group -GroupCategory Security -GroupScope Global -DisplayName $user.Group -Path $DestOU -Verbose | |
$groups = ($user).Department | |
$members = Get-ADUser -Filter * -SearchBase $DestOU -Properties department | Where-Object {$_.department -eq $groups} | |
Add-ADGroupMember -Identity $groups -Members $members | |
} | |
} | |
} | |
#endregion (=END PROCESS) | |
#region (=END) Create log with User, Groups SID Info, stopping Transcript, cleaning. | |
End | |
{ | |
Write-Verbose -Message 'Ready! All Users and Groups successfully created!' | |
Write-Verbose -Message 'Writing another log-file: User, SID and GroupMembership' | |
$log = "$env:userprofile\Desktop\UsersSIDGroups.txt" | |
(Get-ADUser -Filter * -SearchBase $DestOU | Select-Object Name, SID) | Out-File -FilePath $log | |
(Get-ADGroup -Filter * -SearchBase $DestOU | Select-Object Name, SID) | Out-File -FilePath $log -Append | |
(Get-ADUser -Filter * -SearchBase $DestOU -Properties * | Select-Object Name, MemberOf) | Out-File -FilePath $log -Append | |
$VerbosePreference = 'SilentlyContinue' | |
$ErrorActionPreference = 'Continue' | |
Stop-Transcript | |
} | |
#endregion (=END END) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment