2024-04-12 11:49:34 +00:00
|
|
|
|
# Copyright 2024 Rayyan Hodges, TAFE NSW, AlphaDelta
|
2024-04-12 09:43:27 +00:00
|
|
|
|
# Contact: rayyan.hodges@studytafensw.edu.au
|
|
|
|
|
# Program Name: AutoOUADCreator
|
|
|
|
|
# Purpose of script: Create a batch set of OU' using a CSV file within an existing Active Directory Forest.
|
2024-04-12 12:13:08 +00:00
|
|
|
|
# Extra Notes: Modify line 37 and 59 with appropriate domain info. (AlphaDelta.com is used in this scenario)
|
2024-04-12 09:43:27 +00:00
|
|
|
|
|
|
|
|
|
#Import the Active Directory module to allow modifcations to the forest.
|
|
|
|
|
import-module ActiveDirectory
|
|
|
|
|
|
|
|
|
|
#Get user to specify path of the CSV file containing OU names to be added into the Active Directory.
|
2024-04-12 10:42:13 +00:00
|
|
|
|
$fpath = Read-Host -Prompt "Please enter the path to your CSV file containing OU info to be added within the Active Directory Domain Forest"
|
2024-04-12 09:43:27 +00:00
|
|
|
|
|
2024-04-12 11:00:54 +00:00
|
|
|
|
# Check if the CSV file exists
|
|
|
|
|
if (Test-Path $fpath) {
|
|
|
|
|
# Display path to file given by end-user
|
2024-04-12 11:49:34 +00:00
|
|
|
|
Write-Host "CSV file path: $fpath"
|
2024-04-12 11:00:54 +00:00
|
|
|
|
|
2024-04-12 09:43:27 +00:00
|
|
|
|
# Display path to file given by end-user
|
|
|
|
|
echo $fpath
|
|
|
|
|
|
2024-04-12 11:49:34 +00:00
|
|
|
|
# Import OU info from CSV file with error checking
|
|
|
|
|
try {
|
|
|
|
|
$fous = Import-Csv $fpath -ErrorAction Stop
|
|
|
|
|
} catch {
|
|
|
|
|
Write-Host "Error importing CSV file: $_"
|
|
|
|
|
exit
|
|
|
|
|
}
|
2024-04-12 09:43:27 +00:00
|
|
|
|
|
|
|
|
|
#Create OU's within the Active Directory forest by looping throughout each row within the CSV file.
|
2024-04-12 11:12:06 +00:00
|
|
|
|
foreach ($row in $fous) {
|
2024-04-12 09:43:27 +00:00
|
|
|
|
# Get the name of the OU from the CSV
|
2024-04-12 12:13:08 +00:00
|
|
|
|
$ouName = $row.ouName
|
|
|
|
|
|
|
|
|
|
# Debug output - Check if program is actually reading CSV containing OU names.
|
|
|
|
|
Write-Host "Processing OU name: '$ouName'"
|
2024-04-12 09:43:27 +00:00
|
|
|
|
|
2024-04-12 11:49:34 +00:00
|
|
|
|
# Ensure $ouName is not null or empty
|
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace($ouName)) {
|
|
|
|
|
# Get all existing OUs in the specified path
|
|
|
|
|
try {
|
2024-04-12 12:13:08 +00:00
|
|
|
|
$existingOUs = Get-ADOrganizationalUnit -Filter * -SearchBase "DC=alphadelta,DC=com" | Select-Object -ExpandProperty Name
|
2024-04-12 11:49:34 +00:00
|
|
|
|
} catch {
|
|
|
|
|
Write-Host "Error retrieving existing OUs: $_"
|
|
|
|
|
exit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Check if the OU already exists
|
|
|
|
|
$ouExists = $false
|
|
|
|
|
foreach ($existingOU in $existingOUs) {
|
|
|
|
|
if ($existingOU -eq $ouName) {
|
|
|
|
|
$ouExists = $true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-12 11:19:53 +00:00
|
|
|
|
|
2024-04-12 12:13:08 +00:00
|
|
|
|
if (-not $ouExists) {
|
2024-04-12 11:49:34 +00:00
|
|
|
|
# Create the OU
|
|
|
|
|
try {
|
2024-04-12 12:13:08 +00:00
|
|
|
|
New-ADOrganizationalUnit -Name $ouName -Path "DC=alphadelta,DC=com" -ErrorAction Stop
|
2024-04-12 11:49:34 +00:00
|
|
|
|
Write-Host "OU '$ouName' created successfully."
|
|
|
|
|
} catch {
|
|
|
|
|
Write-Host "Error creating OU '$ouName': $_"
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Write-Host "OU '$ouName' already exists, proceeding to next entry."
|
|
|
|
|
}
|
2024-04-12 09:43:27 +00:00
|
|
|
|
} else {
|
2024-04-12 11:49:34 +00:00
|
|
|
|
Write-Host "Skipping empty OU name."
|
2024-04-12 09:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-12 11:12:06 +00:00
|
|
|
|
} else {
|
2024-04-12 11:49:34 +00:00
|
|
|
|
Write-Host "The specified CSV file does not exist."
|
2024-04-12 11:03:48 +00:00
|
|
|
|
}
|