Skip to content

Commit 54daaa4

Browse files
author
snoopy82481
authored
Updates
Reworked the way that the create user function worked Removed createSamAccoutName function Added firstname.lastname to the order of available samAccountNames Created the following Functions - createKeyFile - createPasswordFile
1 parent f0918d7 commit 54daaa4

File tree

1 file changed

+69
-97
lines changed

1 file changed

+69
-97
lines changed

Framework.ps1

+69-97
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
This is the file to get started with the framework.
33
#>
44

5-
#Requires -version 3.0;
6-
Write-Output "I'm version 3.0 or above";
5+
#Requires -version 5.0;
6+
Write-Output "I'm version 5.0 or above";
77

88
#Start GLOBAL Params
99

1010
#.NET Dependancies
1111
Add-Type -AssemblyName System.DirectoryServices;
1212
Add-Type -AssemblyName System.DirectoryServices.AccountManagement;
1313

14-
#[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices");
15-
#[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement");
16-
1714
#Imports
1815
Import-Module ActiveDirectory
1916

@@ -31,14 +28,21 @@ $foldersProgramFilesX86 = [Environment]::GetFolderPath("ProgramFilesX86");
3128
[String]${UserDomain},[String]${UserName} = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\");
3229
$localHostName = [System.Net.Dns]::GetHostEntry("localhost").HostName;
3330
$RidMaster = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).RidRoleOwner.name;
34-
$DomainName = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().name
35-
$PrincipleContext = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext(1,$DomainName)
31+
$DomainName = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().name;
32+
$PrincipleContext = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext(1,$DomainName);
33+
$defaultOU = [regex]::Match((Get-ADUser $UserName).distinguishedName, '(?=OU)(.*\n?)(?<=.)').value;
34+
$defaultDomain = [regex]::Match((Get-ADUser $UserName).distinguishedName, '(?=DC)(.*\n?)(?<=.)').value;
35+
36+
#Constants
37+
$keyFile = "$foldersMyDocuments\AES.key"
38+
$passwordFile = "$foldersMyDocuments\password.txt"
3639

3740
#End GLOBAL Params
3841

3942
#Start FUNCTIONS
4043

41-
function updateManager {
44+
function updateManager
45+
{
4246
#Convert to use .NET vs AD module
4347
<#
4448
.SYNOPSIS
@@ -99,112 +103,79 @@ function CreateUser
99103
function to create a user account
100104
.DESCRIPTION
101105
This function is used to create a new user account in AD.
102-
.PARAMETER Name
106+
.PARAMETER fullUserName
103107
The full name of the user
108+
.PARAMETER Type
109+
What type of samAccountName is it, first.last or first inital and last name. You can use initallastname, iln, firstnamelastname, fnln, firstnamedotlastname, fndln, lastnameinitial, lni.
104110
.PARAMETER Template
105111
Are you using a template account to copy from?
106112
#>
107113

108114
[CmdletBinding()]
109115
param(
110-
[Parameter(Mandatory=$TRUE,HelpMessage='What is the full name of the user?')][String()]$fullUserName,
111-
[Parameter(Mandatory=$FALSE,HelpMessage='Will this be based off a template or no?')][Bool()]$Template
116+
[Parameter(Mandatory=$TRUE,HelpMessage='What is the full name of the user?')][String]$fullUserName,
117+
[Parameter(Mandatory=$TRUE,HelpMessage='What type of samAccountName?')][String]$type,
118+
[Parameter(Mandatory=$FALSE,HelpMessage='Will this be based off a template or no?')][Bool]$Template
112119
)
113120

114-
function createsamAccountName
115-
{
116-
#steps to create username for environment
117-
118-
<#
119-
.SYNOPSIS
120-
function to formulate the samAccountName of an account.
121-
.DESCRIPTION
122-
function used to forumulate a samAccountName for the environment.
123-
.PARAMETER NameInput
124-
The full name of the user
125-
.PARAMETER Type
126-
What type of samAccountName is it, first.last or first inital and last name. You can use initallastname, iln, firstnamelastname, fln.
127-
.PARAMETER Password
128-
The password that will be set on account creation
129-
#>
130-
131-
[CmdletBinding()]
132-
param(
133-
[Parameter(Mandatory=$TRUE,HelpMessage='What is the name of the person?')][String()]$NameInput,
134-
[Parameter(Mandatory=$TRUE,HelpMessage='What is the type of samAccountName is it, first.last or first inital and last name?')][String()]$Type,
135-
[Parameter(Mandatory=$TRUE,HelpMessage='What is the password you would like to set?')][String()]$Password
136-
)
137-
138-
function initallastname
139-
{
140-
$FirstInitial = $NameInput.split(" ")[0].Substring(0,1).ToLower();
141-
$LastName = $NameInput.split(" ")[1].ToLower();
142-
143-
$OutputName = ("{0}{1}" -f $FirstInitial,$LastName).ToLower();
144-
145-
return $outputName;
146-
}
147-
148-
function firstnamelastname
149-
{
150-
$OutputName = ($NameInput.replace(" ",".")).ToLower();
151-
152-
return $outputName;
153-
}
154-
155-
function lastnameinitial
156-
{
157-
$FirstInitial = $NameInput.split(" ")[0].Substring(0,1).ToLower();
158-
$LastName = $NameInput.split(" ")[1].ToLower();
159-
160-
$OutputName = ($Lastname+$FirstIntial);
161-
162-
return $OutputName;
163-
}
164-
165-
switch ($type)
166-
{
167-
{$_ -in "initallastname","iln"} {$samAccountName = initallastname};
168-
{$_ -in "firstnamelastname","fln"} {$samAccountName = firstnamelastname};
169-
{$_ -in "lastnameinital","lni"} {$samAccountName = lastnameinitial};
170-
}
171-
172-
return $samAccountName;
173-
}
174-
121+
[String]${FirstName}, [String]${LastName} = $fullUserName.split(" ");
122+
[String]$FirstInitial = $FirstName.Substring(0,1).ToLower();
123+
124+
[String]$FirstName = (Get-Culture).TextInfo.ToTitleCase($FirstName);
125+
[String]$LastName = (Get-Culture).TextInfo.ToTitleCase($LastName);
126+
127+
$Key = Get-Content $keyFile
128+
$Password = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key
129+
130+
175131
switch ($type)
176132
{
177-
"Initial" {$samAccountName = createsamAccountName $fullUserName initallastname};
178-
"FullName" {$samAccountName = createsamAccountName $fullUserName firstnamelastname};
179-
"LastNameInitial" {$samAccountName = createsamAccountname $fullUserName LastNameInitial};
133+
{$_ -in "initallastname","iln"} {$samAccountName = "$FirstInitial$LastName"};
134+
{$_ -in "firstnamelastname","fnln"} {$samAccountName = "$FirstName$LastName"};
135+
{$_ -in "firstnamedotlastname","fndln"} {$samAccountName = "$FirstName.$LastName"};
136+
{$_ -in "lastnameinital","lni"} {$samAccountName = "$Lastname$FirstInitial"};
180137
}
181138

182-
$userPrincipalName = "$samAccountName@test.com"
139+
Try
140+
{
141+
$samAccountName = $samAccountName.SubString(0,20).ToLower();
142+
}
143+
catch
144+
{
145+
$samAccountName = $samAccountName.ToLower();
146+
}
147+
148+
$userPrincipalName = "$samAccountName@$DomainName"
183149

184-
if($Template){
150+
if($Template)
151+
{
185152
$TemplateAccount = Get-ADUser -Identity "templateaccount";
186153
New-ADUser -Instance $TemplateAccount -SamAccountName $samAccountName;
187154
}
188155
else
189156
{
190-
New-ADUser -Name $NameInput -GivenName ((Get-Culture).Textinfo.ToTitleCase($NameInput.split(" ")[0])) -Surname ((Get-Culture).Textinfo.ToTitleCase($NameInput.split(" ")[1])) -samAccountName $samAccountName -UserPrincipalName $userPrincipalName -AccountPassword $Password -PassThru | Enable-ADAccount
191-
}
192-
193-
<#
194-
switch ($stuff)
195-
{
196-
default {New-ADUser -Name $NameInput -GivenName ((Get-Culture).Textinfo.ToTitleCase($NameInput.split(" ")[0])) -Surname ((Get-Culture).Textinfo.ToTitleCase($NameInput.split(" ")[1])) -samAccountName $samAccountName -UserPrincipalName $userPrincipalName -AccountPassword $Password -PassThru | Enable-ADAccount}
197-
template {
198-
$TemplateAccount = Get-ADUser -Identity "templateaccount";
199-
New-ADUser -Instance $TemplateAccount -SamAccountName $samAccountName;
200-
}
157+
New-ADUser -Name $fullUserName -GivenName $FirstName -Surname $LastName -samAccountName $samAccountName -UserPrincipalName $userPrincipalName -AccountPassword $Password -PassThru | Enable-ADAccount
201158
}
202-
#>
203159
}
160+
161+
Function createKeyFile
162+
{
163+
$Key = Byte[] 32
164+
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
165+
$Key | out-file $KeyFile
166+
}
167+
168+
Function createPasswordFile
169+
{
170+
$Key = Get-Content $keyFile
171+
$Password = "Temppassword1$" | ConvertTo-SecureString -AsPlainText -Force
172+
$Password | ConvertFrom-SecureString -Key $Key | Out-File $passwordFile
173+
}
174+
204175
#End FUNCTIONS
205176

206177
#$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($PrincipleContext, "a_valid_samaccountname")
207-
#$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($PrincipleContext, "sbeale")
178+
#$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($PrincipleContext, "mmeyers")
208179

209180
Class createuser
210181
{
@@ -216,10 +187,10 @@ Class createuser
216187
[ValidateSet('Onsite','Offsite')]
217188
[string]$EmployeeLocation
218189
[ValidatePattern("^OU=")]
219-
[String]$OU
220-
hidden static [String]$Domain = ""
221-
222-
[string]static GetNewUserName([string]$FirstName,[string]$LastName){
190+
[String]$OU = $defaultOU
191+
hidden static [String]$Domain = $defaultDomain
192+
193+
[string]SamAccountName([string]$FirstName,[string]$LastName){
223194

224195
$UName = ($FirstName.Substring(0,1) + $LastName).ToLower()
225196

@@ -237,9 +208,10 @@ Class createuser
237208

238209
}
239210

240-
return $SamAccountName
211+
return $this.SamAccountName
241212
}
242-
213+
214+
243215
createuser(){
244216
}
245217

@@ -249,7 +221,7 @@ Class createuser
249221
$this.EmployeeLocation = $EmployeeLocation
250222
$this.FirstName = $FirstName
251223
$this.LastName = $LastName
252-
$this.UserName = [createuser]::GetNewUserName($FirstName,$LastName)
224+
$this.UserName = [createuser]::SamAccountName($FirstName,$LastName)
253225

254226
}
255227

0 commit comments

Comments
 (0)