Home > Mobile >  Create User with PowerShell - Multiple OU Path variables based on Department & Location
Create User with PowerShell - Multiple OU Path variables based on Department & Location

Time:01-12

I'm trying to script user creation and am having issues with OU variables. I've managed to script the users to create in OU based on users department but I also need to do it based on location. I've pasted part of my script below, which works fine, I just want to know if there's an easy way of integrating Office variable into it so that if user office is New York and Department equals IT then move to NY IT OU for example. Our OU's are split by region and department so I understand it may be a little complicated but any pointers are appreciated. Thanks in advance.

If ($department -eq “IT”){
     $OU = 'OU=IT,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local'
}elseIf($department -eq “Finance”){
    $OU = 'OU=Finance,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local'
}elseIf($department -eq “Sales”){
    $OU = 'OU=Sales,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,,DC=local'
}elseIf($department -eq “HR”){
    $OU = 'OU=HR,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,,DC=local'
}elseIf($department -eq “Client Services”){
    $OU = 'OU=Client Services,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local'
}else {$OU = 'OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local'
}


New-ADUser `
-Department $Department `
-Name "$Firstname $Surname" `
-UserPrincipalName $UPN `
-Path $OU `
-GivenName $FirstName `
-Surname $Surname `
-SamAccountName "$FirstName.$Surname" `
-AccountPassword (Read-Host -AsSecureString "Input User Password") `
-ChangePasswordAtLogon $False `
-Company "Test" `
-Title $JobTitle `
-EmailAddress "[email protected]" `
-State "LDN" `
-Country "GB" `
-Office "LDN" `
-City "London" `
-DisplayName "$FirstName $Surname" `
-Enabled $True

CodePudding user response:

If you mean you have different OU's for departments in different parts of the world, you could build yourself a lookup Hashtable where each key is the department and each value is another Hashtable that has the abbreviated State as key and the complete OU path as value.

Something like below:

$OULookup = @{
    'IT'      = @{'LDN' = 'OU=IT,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local'
                  'NY'  = 'OU=IT,OU=New York (NY),OU=America,OU=Company Staffing,DC=testcompany,DC=local'
                }
    'Finance' = @{'LDN' = 'OU=Finance,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local'
                  'NY'  = 'OU=Finance,OU=New York (NY),OU=America,OU=Company Staffing,DC=testcompany,DC=local'
                }
    'Sales'   = @{'LDN' = 'OU=Sales,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,,DC=local'
                  'NY'  = 'OU=Sales,New York (NY),OU=America,OU=Company Staffing,DC=testcompany,,DC=local'
                }
}

Now you can retrieve the OU path using that lookup table using

$OULookup['IT']['LDN']       # --> OU=IT,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local
$OULookup['Sales']['NY']     # --> OU=Sales,New York (NY),OU=America,OU=Company Staffing,DC=testcompany,,DC=local
$OULookup['Finance']['LDN']  # --> OU=Finance,OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local

P.S. Please have a look at about_Splatting to see how you can use cmdlets like New-ADUser that take a lot of parameters better without using those horrible backticks

CodePudding user response:

Depending on how many offices you have and how many fields you want to script, Theo's lookup table might be better, but for a simple example using Switch see below:

$department = 'IT'
$office = 'London (LDN)'

Switch ($Office) {
    "London (LDN)" {
        # Look for OU matching $department only in the "London" OU
        $OU = Get-ADOrganizationalUnit -Filter "Name -eq '$department'" -SearchBase 'OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local'
    }
    "New York (NY)" {
        # Look for OU matching $department only in the "New York" OU
        $OU = Get-ADOrganizationalUnit -Filter "Name -eq '$department'" -SearchBase 'OU=New York (NY),OU=America,OU=Company Staffing,DC=testcompany,DC=local'
    }
    default {
        $OU = 'OU=London (LDN),OU=Europe,OU=Company Staffing,DC=testcompany,DC=local'
    }
}


# The $OU variable is now populated correctly
$OU | Select-Object Name, DistinguishedName

Though to be honest - you could get the same effect simply by filtering the Get-ADOrganizationalUnit cmdlet:

$OU = Get-ADOrganizationalUnit -Filter * | Where-Object { ($_.Name -eq $department) -and ($_.DistinguishedName -match $office)}
  • Related