Adding Ou Structure using Powershell
Starting of, we need to set up a couple of OrganizationalUnits in our test environment. Following the structure set up in Part 1.1.0:
Organizational Unit Structure
- ou = Series
- l = Location
- Description = Starship
- Child OU: Computers
- Child OU: Groups
- Child OU: Users
PS > Import-Csv StarTrek.csv | Select-Object Series, Starship, Location Series Starship Location ------ -------- -------- Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha QuadrantNow we,ve managed to retrieve the specified columns and rows. Next we need to narrrow the list down to only unique entries. We can achieve this through the -Unique SwitchParameter.
PS > Import-Csv .StarTrek.csv | Select-Object Series, Starship, Location -Unique Series Starship Location ------ -------- -------- Star Trek: The Next Generation USS Enterprise (NCC-1701-D) Alpha Quadrant Star Trek: Deep Space Nine Deep Space Nine Alpha Quadrant Star Trek: Voyager USS Voyager (NCC-74656) Delta Quadrant Star Trek: Enterprise Enterprise (NX-01) Alpha QuadrantNow that we’ve narrowed down our list, we can start creating the OU structure. The OU:s Name should be “Series”, so the structure that we’re looking for is:
- Star Trek: The Next Generation
- Star Trek: Deep Space Nine
- Star Trek: Voyager
- Star Trek: Enterprise
function Check-distinguishedName ([string]$Domain, [string]$OU) {
trap { $Script:distinguishedNameDoesntExist = $True ; continue }
.\Get-AD.ps1 -Domain $Domain -OU $OU -Filter distinguishedName | Out-Null
}
The Variable $Script:distinguishedNameDoesntExist is set to $True if we test the function on a non existing OU. Here’s an example on how it works:
PS > Check-distinguishedName -Domain powershell.nu -OU "OU=Domain Controllers,DC=powershell,DC=nu" PS > $distinguishedNameDoesntExistSince Domain Controllers Exist, the variable $distinguishedNameDoesntExist is not set to anything. Running the same function on a OU that does not exist:
PS > Check-distinguishedName -Domain powershell.nu -OU "OU=Non Existing OU,DC=powershell,DC=nu" PS > $distinguishedNameDoesntExist True PS > $distinguishedNameDoesntExist = $NullThis time the variable $distinguishedNameDoesntExist was set to True, which means that the OU does not exist and it’s available for creation. I also set the Variable to $Null so that i can reuse the function. Moving on. If the $distinguishedNameDoesntExist equals $True we can start building up the OU Structure. In the script, there’s a paramter called -Domain which takes the domain name as an argument. In my examples I’m going to use the powershell.nu domain. Setting this as an argument is similar to creating a Variable holding the domain name:
PS > $Domain = "powershell.nu PS > $Domain powershell.nuWith this information we can create a Connection string that we can use when connecting to Active-Directory. We can make use of the methods() withing System.String to alter the string as we want it. I’m using Replace() and Insert() to get the result I want:
PS > $Connection = ($Domain.Replace(".",",DC=")).Insert(0,"LDAP://DC=")
PS > $AD = [adsi] $Connection
PS > $AD
distinguishedName
-----------------
{DC=powershell,DC=nu}
Now that we’ve set up a connection we can start creating an OU. It’s pretty straight forward, nothing fancy here:
PS > $OU = $AD.Create("OrganizationalUnit", "ou=$Series")
PS > $OU.SetInfo()
PS > $OU.put("l", $Location)
PS > $OU.put("Description", $Starship)
PS > $OU.setinfo()
When creating Child OU:s, we need to alter the Connection string so that we connect to the OU that we’ve just created, here’s an example on doing that:
$NewConnection = "LDAP://OU=" + $Series + ($Domain.Replace(".",",DC=")).Insert(0,",DC=")
$NewOU = [adsi]$NewConnection
Now we can start creating Child OU:s that are structured after our purpose:
$Users = $NewOU.Create("OrganizationalUnit", "ou=Users")
$Users.SetInfo()
$Users.put("l", $Location)
$Users.put("Description", $Starship)
$Users.setinfo()
This describes the steps that I’ve set up in the Script. Running the script in the test environment would look like this:
If i repeat the script, the Check function finds that the top OU:s already exists and the following is returned to the host:
Finally, taking a peek in dsa.msc.
Click Here to Download the Complete Script.
The Get-AD.ps1 script is also required.
Click here to download the Csv File
[?]
