This commit is contained in:
Ray 2024-04-12 22:13:08 +10:00 committed by GitHub
parent 1d142bbe7b
commit 2088972db4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 5 deletions

View File

@ -2,7 +2,7 @@
# Contact: rayyan.hodges@studytafensw.edu.au # Contact: rayyan.hodges@studytafensw.edu.au
# Program Name: AutoOUADCreator # Program Name: AutoOUADCreator
# Purpose of script: Create a batch set of OU' using a CSV file within an existing Active Directory Forest. # Purpose of script: Create a batch set of OU' using a CSV file within an existing Active Directory Forest.
# Extra Notes: Modify line 37 with appropriate domain info. (AlphaDelta.com is used in this scenario) # Extra Notes: Modify line 37 and 59 with appropriate domain info. (AlphaDelta.com is used in this scenario)
#Import the Active Directory module to allow modifcations to the forest. #Import the Active Directory module to allow modifcations to the forest.
import-module ActiveDirectory import-module ActiveDirectory
@ -29,13 +29,16 @@ echo $fpath
#Create OU's within the Active Directory forest by looping throughout each row within the CSV file. #Create OU's within the Active Directory forest by looping throughout each row within the CSV file.
foreach ($row in $fous) { foreach ($row in $fous) {
# Get the name of the OU from the CSV # Get the name of the OU from the CSV
$ouName = $row.Name $ouName = $row.ouName
# Debug output - Check if program is actually reading CSV containing OU names.
Write-Host "Processing OU name: '$ouName'"
# Ensure $ouName is not null or empty # Ensure $ouName is not null or empty
if (-not [string]::IsNullOrWhiteSpace($ouName)) { if (-not [string]::IsNullOrWhiteSpace($ouName)) {
# Get all existing OUs in the specified path # Get all existing OUs in the specified path
try { try {
$existingOUs = Get-ADOrganizationalUnit -Filter * -SearchBase "DC=PSTest,DC=local" | Select-Object -ExpandProperty Name $existingOUs = Get-ADOrganizationalUnit -Filter * -SearchBase "DC=alphadelta,DC=com" | Select-Object -ExpandProperty Name
} catch { } catch {
Write-Host "Error retrieving existing OUs: $_" Write-Host "Error retrieving existing OUs: $_"
exit exit
@ -50,10 +53,10 @@ foreach ($existingOU in $existingOUs) {
} }
} }
i if (-not $ouExists) { if (-not $ouExists) {
# Create the OU # Create the OU
try { try {
New-ADOrganizationalUnit -Name $ouName -Path "DC=PSTest,DC=local" -ErrorAction Stop New-ADOrganizationalUnit -Name $ouName -Path "DC=alphadelta,DC=com" -ErrorAction Stop
Write-Host "OU '$ouName' created successfully." Write-Host "OU '$ouName' created successfully."
} catch { } catch {
Write-Host "Error creating OU '$ouName': $_" Write-Host "Error creating OU '$ouName': $_"