Search code examples
powershellvariablespowershell-cmdlet

ADSI not validating correctly?


I have a CSV with 1500+ users there are 30 users per class OU, and then 6 class OU's per Intake OU. What I'm trying to achieve is, foreach (user in CSV), check if OU exists, then check if parent OU exists, create the parent OU only if it doesn't exist, then create the OU, then create the user, or just create the user if the OU exists.

Here is the code I'm using:

$ErrorActionPreference = "Stop"
Import-Module ActiveDirectory
$CSV = Import-Csv "C:\Scripts\AddPupils.csv" 

foreach ($user in $CSV) {
    # Variables
    $GivenName = $user.GivenName
    $SurName = $user.SurName
    $UserName = $user.UserName
    $Class = $user.YearClass
    $Intake = $user.Intake
    $DisplayName = $GivenName+" "+$SurName

    # Create User
    $UserOUParent = "OU=Year "+$iIntake+" Intake,OU=Students,OU=Users,OU=Roding,DC=Zulbag,DC=com"
    $UserOU = "OU=Class "+$Class.Substring(1,1)+",OU=Year "+$iIntake+" Intake,OU=Students,OU=Users,OU=Roding,DC=Zulbag,DC=com"
    $NewUserOUParentCheck = [ADSI]::Exists("LDAP://$UserOUParent")
    $NewUserOUCheck = [ADSI]::Exists("LDAP://$UserOU")

    if ($NewUserOUCheck -eq $false){
        if ($NewUserOUParentCheck -eq $false){
            "Create Parent Ou"
            New-ADOrganizationalUnit `
                -Name ("Year "+$Intake+" Intake") `
                -Path "OU=Students,OU=Users,OU=Roding,DC=Zulbag,DC=Com" `
                -ProtectedFromAccidentalDeletion $False
        }
        "Create OU"
        New-ADOrganizationalUnit `
            -Name ("Class "+$Class.Substring(1,1)) `
            -Path ("OU=Year "+$Intake+" Intake,OU=Students,OU=Users,OU=Roding,DC=Zulbag,DC=Com") `
            -ProtectedFromAccidentalDeletion $False
    }
    "Create User"
    New-ADUser `
        -Name $DisplayName `
        -SurName $SurName `
        -GivenName $GivenName `
        -DisplayName $DisplayName `
        -SamAccountName $UserName `
        -UserPrincipalName ($UserName+"@Zulbag.com") `
        -AccountPassword (ConvertTo-SecureString "Testing123" -AsPlainText -force) `
        -CannotChangePassword $true `
        -ChangePasswordAtLogon $false `
        -PasswordNeverExpires $true `
        -EmailAddress ($UserName+"@Zulbag.com") `
        -Country "GB" `
        -Path ("OU=Class "+$Class.Substring(1,1)+",OU=Year "+$Intake+" Intake,OU=Students,OU=Users,OU=Roding,DC=Zulbag,DC=Com") `
        -ProfilePath ("D:\Shares\User Accounts\Students\Intake Year "+$Intake+"\Class "+$Class.Substring(1,1)+"\Profiles\"+$DisplayName) `
        -Enabled $true

    Start-Sleep -Seconds 5
    # Add To Group  
    $Group = "CN=Redirection "+$Intake.Substring(2,2)+$Class.Substring(1,1)+",OU=Intake "+$Intake+",OU=Security Groups,OU=Roding,DC=Zulbag,DC=Com"
    $GroupOU = "OU=Intake "+$Intake+",OU=Folder Redirection Groups,OU=Security Groups,OU=Roding,DC=Zulbag,DC=Com"
    $NewGroupCheck = [ADSI]::Exists("LDAP://$Group")
    $NewGroupOUCheck = [ADSI]::Exists("LDAP://$GroupOU")

    if ($NewGroupCheck -eq $false) {
        if ($NewGroupOUCheck -eq $false) {
            "Create OU"
            New-ADOrganizationalUnit `
                -Name ("Intake "+$Intake) `
                -Path "OU=Folder Redirection Groups,OU=Security Groups,OU=Roding,DC=Zulbag,DC=Com" `
                -ProtectedFromAccidentalDeletion $False
        }
        "create Group"
        New-ADGroup `
            -Name ("Redirection "+$Intake.Substring(2,2)+$Class.Substring(1,1)) `
            -GroupScope "Global" `
            -Path ("OU=Intake "+$Intake+",OU=Folder Redirection Groups,OU=Security Groups,OU=Roding,DC=Zulbag,DC=Com")
    }
    "Add Member"
    Add-ADGroupMember ("Redirection "+$Intake.Substring(2,2)+$Class.Substring(1,1)) $UserName
}

pause

Sample CSV:

GivenName,SurName,Class,UserName,Intake
Ali,Grisdale,1B,AGris,2016
Ayomiposi,Olayera,1B,AOlay,2016

In tests with Write-Output, the [ADSI] validates correctly but it appears to be validating it incorrectly here, the error message I keep getting is:

New-ADOrganizationalUnit : An attempt was made to add an object to the 
directory with a name that is already in use
At C:\Scripts\AddPupils-Afzal.ps1:24 char:13
+             New-ADOrganizationalUnit 
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (OU=Year 2016 In...C=Zulbag,DC=Com:String) [New-ADOrganizationalUnit], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8305,Microsoft.ActiveDirectory.Management.Commands.NewADOrganizationalUnitdirectory

It successfully creates the parent OU, class OU and user, but fails to create the second user, instead validates the OU incorrectly again.

Any Ideas?


Solution

  • Check the script for path errors!