Created
February 8, 2018 20:31
-
-
Save Deanout/e61e85cb5ac172503026e0844c026dc8 to your computer and use it in GitHub Desktop.
Homework 2 sysadmin stuff
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
# Lab 2 - AD PowerShell Basics | |
# Name: Dean DeHart | |
# Date: 02/06/18 | |
# Description: Reads users from a given CSV and adds them to the Domain Users in ADUC. | |
# Also contains function for changing password reset on logon and deleting users. | |
# Imports | |
Import-Module ActiveDirectory | |
# Variable Declarations | |
$users = import-csv Names.txt | |
# Function Declarations | |
# Loops through users and creates accounts for each. | |
function add_users { | |
foreach($user in $users) { | |
$ou = "OU=dddehart,OU=students,DC=csi3670,DC=local" | |
$pw = "Temp12345" | |
$detailed_name = $user.FirstName + " " + $user.LastName | |
$firstletter_first_name = $user.FirstName.Substring(0,1) | |
$SAM = "dddehart-" + $user.FirstName + $user.LastName | |
New-AdUser -Name $detailed_name -SamAccountName $SAM -UserPrincipalName $SAM -DisplayName $detailed_name -GivenName $user.FirstName -Surname $user.LastName -AccountPassword (ConvertTo-SecureString $pw -AsPlainText -Force) -Enabled $true -Path $ou | |
} | |
} | |
# Loops through users and creates accounts for each. | |
function change_pw_on_logon { | |
foreach($user in $users) { | |
$ou = "OU=dddehart,OU=students,DC=csi3670,DC=local" | |
$pw = "Temp12345" | |
$detailed_name = $user.FirstName + " " + $user.LastName | |
$firstletter_first_name = $user.FirstName.Substring(0,1) | |
$SAM = "dddehart-" + $user.FirstName + $user.LastName | |
$ad_user = Get-AdUser $SAM | |
if ($ad_user) { | |
Set-AdUser $SAM -ChangePasswordAtLogon $true | |
} else { | |
Write-Output "User " + $SAM + " was not found!" | |
} | |
} | |
} | |
# Loops through users and creates accounts for each. | |
function remove_users { | |
foreach($user in $users) { | |
$ou = "OU=dddehart,OU=students,DC=csi3670,DC=local" | |
$pw = "Temp12345" | |
$detailed_name = $user.FirstName + " " + $user.LastName | |
$firstletter_first_name = $user.FirstName.Substring(0,1) | |
$SAM = "dddehart-" + $user.FirstName + $user.LastName | |
#New-AdUser -Name $detailed_name -SamAccountName $SAM -UserPrincipalName $SAM -DisplayName $detailed_name -GivenName $user.FirstName -Surname $user.LastName -AccountPassword (ConvertTo-SecureString $pw -AsPlainText -Force) -Enabled $true -Path $ou | |
$ad_user = Get-AdUser $SAM | |
if ($ad_user) { | |
# Delete The User | |
Remove-ADUser -Identity $ad_user | |
} else { | |
Write-Output "User " + $SAM + " was not found!" | |
} | |
} | |
} | |
# Function Calls | |
# add_users | |
# change_pw_on_logon | |
remove_users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment