posted 21/12/09

Create Lab AD – Part 5 (Changed)

By Fredrik Wall

I have been doing some changes to the last part.

All of my scripts are under constantly changes.

So before I post the next part of the script I

want to post this change.

I have been doing some changes to the $labDomain.

Now, by default you don’t need to do change anything.

It uses your users default domain and will use It all

the way.

# Name of the AD
# Default is the domain where your user are.
# Change $labDomain if you want to change this.
# $labDomain = "dc=poweradmin,dc=local"
$activeLabDomain = new-object DirectoryServices.DirectoryEntry
$labDomain = $activeLabDomain.distinguishedName
# Creating the "root" Lab OU
$search = [System.DirectoryServices.DirectorySearcher]"[ADSI]LDAP://$labDomain"
$search.Filter = "(&(name=$labOU)(objectCategory=organizationalunit))"
$result = $search.FindOne()

if ($result -eq $null) {
    $labADSIDomain = [ADSI]"LDAP://$labDomain"
    $objOU = $labADSIDomain.Create("OrganizationalUnit", "ou=" + $labOU)
    $objOU.SetInfo()
    Write-Host $labOU "created"
}
else
{
    Write-Host $labOU "exists"
}

# Creating all OUs in the Lab OU
$labDomainOU = [ADSI]"LDAP://ou=$labOU,$labDomain"
foreach ($labUnit in $labOUs) {
    $search = [System.DirectoryServices.DirectorySearcher]$labDomainOU
    $search.Filter = "(&(name=$labUnit)(objectCategory=organizationalunit))"
    $result = $search.FindOne()
    if ($result -eq $null) {
        $objOU = $labDomainOU.Create("OrganizationalUnit", "ou=" + $labUnit)
        $objOU.SetInfo()

        Write-Host $labUnit "created"
    }
    else
    {
        Write-Host $labUnit "exists"
    }

}
1 Comment
read more
posted 21/12/09

Create Lab AD – part 5

By Fredrik Wall

It’s time to take our information and create the AD.
First of all we need to setup the lab structure in our AD.
I use my lab active directory named poweradmin.local

# Name of the AD
$labDomain = [ADSI]"LDAP://dc=poweradmin,dc=local"

And then we need to decide what our Lab OU “root” should be called.

 

# LAB OU

$labOU = "Lab OU"

And then we put all of our OUs that we need.

# OUs to create

$labOUs = "Finance","IT","Marketing","Operations","Service","Customer Support"

Now It’s time to create the “root” lab OU.

First of all we check If it exists and It doesn’t we create it.

 
# Creating the "root" Lab OU

$search = [System.DirectoryServices.DirectorySearcher]$labDomain

$search.Filter= "(&(name=$labOU)(objectCategory=organizationalunit))"

$result = $search.FindOne()

if ($result -eq $null) {

   $objOU = $labDomain.Create("OrganizationalUnit","ou=" + $labOU)

  
$objOU.SetInfo()

  
Write-Host $labOU "created"

}

else

{

   Write-Host $labOU "exists"

}

When we have the “root” OU setup It’s time to create the rest.

# Creating all OUs in the Lab OU

$labDomainOU = [ADSI]"LDAP://ou=$labOU,dc=poweradmin,dc=local"

foreach ($labUnit in $labOUs) {

  
$search = [System.DirectoryServices.DirectorySearcher]$labDomainOU

  
$search.Filter= "(&(name=$labUnit)(objectCategory=organizationalunit))"

  
$result = $search.FindOne()

  
if ($result -eq $null) {

      
$objOU = $labDomainOU.Create("OrganizationalUnit","ou=" + $labUnit)

      
$objOU.SetInfo()

       Write-Host $labUnit "created"

    }

   else

    {

       Write-Host $labUnit "exists"

    }

}

It should look like this if you used the same OUs as I did.

image

In the next part we will create the users.

 

2 Comments
read more
posted 17/12/09

Create Lab AD – part 3

By Fredrik Wall

We have:

  1. Real names
  2. User Names (samAccountNames) with employee numbers

In this part we will do stuff for the Lab OUs and we will also pick Country
for the users.

We will not create anything until we have what we need in information.

$labOUs = "Finance","IT","Marketing","Operations","Service","Customer Support"

$mylabOUs = $labOUs | Get-Random

The output will be one of the OUs in the $labOUs.

Finance

IT

Marketing

Need more OU names, just add yours.

Now It’s time to do the same way to make some countries for the users.

$userCountry = "Sweden", "Norway", "Denmark", "Finland"
$myUserCountry = $userCountry | Get-Random
1 Comment
read more