SOME SORT OF PROGRESS???

This commit is contained in:
Ray 2024-04-12 21:49:34 +10:00 committed by GitHub
parent fd7d3143d0
commit 1d142bbe7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 40 additions and 14 deletions

View File

@ -1,4 +1,4 @@
# Copyright 2024 Rayyan Hodges, TAFE NSW, AlphaDelta # Copyright 2024 Rayyan Hodges, TAFE NSW, AlphaDelta
# 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.
@ -13,31 +13,57 @@ $fpath = Read-Host -Prompt "Please enter the path to your CSV file containing OU
# Check if the CSV file exists # Check if the CSV file exists
if (Test-Path $fpath) { if (Test-Path $fpath) {
# Display path to file given by end-user # Display path to file given by end-user
echo $fpath Write-Host "CSV file path: $fpath"
# Display path to file given by end-user # Display path to file given by end-user
echo $fpath echo $fpath
#Import OU info from CSV file. # Import OU info from CSV file with error checking
$fous = Import-Csv $fpath try {
$fous = Import-Csv $fpath -ErrorAction Stop
} catch {
Write-Host "Error importing CSV file: $_"
exit
}
#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.Name
# Ensure $ouName is not null or empty
if (-not [string]::IsNullOrWhiteSpace($ouName)) {
# Get all existing OUs in the specified path # Get all existing OUs in the specified path
$existingOUs = Get-ADOrganizationalUnit -Filter * -SearchBase "DC=alphadelta,DC=com" | Select-Object -ExpandProperty Name try {
$existingOUs = Get-ADOrganizationalUnit -Filter * -SearchBase "DC=PSTest,DC=local" | Select-Object -ExpandProperty Name
} catch {
Write-Host "Error retrieving existing OUs: $_"
exit
}
# Check if the OU already exists # Check if the OU already exists
if (-not (Get-ADOrganizationalUnit -LDAPFilter "(Name=$ouName)")) { $ouExists = $false
foreach ($existingOU in $existingOUs) {
if ($existingOU -eq $ouName) {
$ouExists = $true
break
}
}
i if (-not $ouExists) {
# Create the OU # Create the OU
New-ADOrganizationalUnit -Name $ouName -Path "DC=alphadelta,DC=com" -ErrorAction SilentlyContinue try {
New-ADOrganizationalUnit -Name $ouName -Path "DC=PSTest,DC=local" -ErrorAction Stop
Write-Host "OU '$ouName' created successfully." Write-Host "OU '$ouName' created successfully."
# If OU name already exists, simply display a message stating it exists and skip to next entry. } catch {
Write-Host "Error creating OU '$ouName': $_"
}
} else { } else {
Write-Host "OU '$ouName' already exists, proceeding to next entry." Write-Host "OU '$ouName' already exists, proceeding to next entry."
} }
} else {
Write-Host "Skipping empty OU name."
}
} }
} else { } else {
Write-Host "The specified CSV file does not exist." Write-Host "The specified CSV file does not exist."