Active Directory Star Wars Users

Active Directory Star Wars Users

Hello,

When you build Active Directory labs, you need some users in it. Usually, we go with :

Before :

2..50 | % {New-ADUser -Name "User-$_" 'OU=Users,OU=People,DC=D2K12R2,DC=local' }
2..50 | % {New-ADGroup -GroupScope Universal -Name "U_Test$_" -Path 'OU=Groups,OU=People,DC=D2K12R2,DC=local' }
2..50 | % {New-ADGroup -GroupScope Global -Name "G_Test$_" -Path 'OU=Groups,OU=People,DC=D2K12R2,DC=local' }
2..50 | % {New-ADGroup -GroupScope DomainLocal -Name "D_Test$_" -Path 'OU=Groups,OU=People,DC=D2K12R2,DC=local' }

It works well, but it’s not fun !

The API

Today, I found a very cool WebAPI that can serve us. The API is http://swapi.co/ for “Star Wars API”. This web site holds a ton of data about Star Wars world : People, Species, Films, Starships, etc.. All of this data is exposed through a WepAPI that we can query from PowerShell :

Invoke-WebRequest -Uri http://swapi.co/api/people/1/
Active Directory Star Wars Users - WepAPI with PowerShell

Active Directory Star Wars Users – WepAPI with PowerShell

We need to go a bit deeper, indeed, this isn’t usable for now. If we look at at the “Content”, we can see some JSON datas :

Invoke-WebRequest -Uri http://swapi.co/api/people/1/ | Select-Object -ExpandProperty Content
| ConvertFrom-Json
Active Directory Star Wars Users - WepAPI with PowerShell and JSON

Active Directory Star Wars Users – WepAPI with PowerShell and JSON

And here we go, we have a well formatted object that we can use for Active Directory users creation.

If we look at the documentation about this WebAPI we can see all the kind of data we can query :

Invoke-WebRequest -Uri http://swapi.co/api/ | Select-Object -ExpandProperty Content | Convert
From-Json
Active Directory Star Wars Users - WepAPI Root

Active Directory Star Wars Users – WepAPI Root

Active Directory Star Wars Users

With some imagination, you can build all king of query to populate a CSV to use for Active Directory users creation. Hereunder a quick example of how to implement it :

$ApiUrl = 'http://swapi.co/api'
$UserList = New-Object -TypeName System.Collections.ArrayList
$GroupList = New-Object -TypeName System.Collections.ArrayList
$SiteList = New-Object -TypeName System.Collections.ArrayList
$AccountPassword = 'P@$$w0rd1'
$UPNSuffix = '@D2K12R2.local'
$MailDomain = 'itfordummies.net'
$ParentOU = 'OU=Production,DC=D2K12R2,DC=local'

#People
1..$(((Invoke-WebRequest -Uri $ApiUrl/people).Content | ConvertFrom-Json).count) | % {
    (Invoke-WebRequest -Uri $ApiUrl/people/$_).Content | ConvertFrom-Json | Select-Object Height,Mass,Hair_Color,Skin_Color,Eye_Color,Birth_Year,Gender,
        @{Label='Name';Expression={$_.Name -replace 'é','é'}},
        @{Label='Homeworld';Expression={(Invoke-WebRequest -Uri $_.homeworld).Content | ConvertFrom-Json}},
        @{Label='Films';Expression={($_.Films | % {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}},
        @{Label='species';Expression={($_.species | % {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}},
        @{Label='vehicles';Expression={($_.vehicles | % {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}},
        @{Label='starships';Expression={($_.starships | % {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}},
        @{Label='url';Expression={(Invoke-WebRequest -Uri $_.url).Content | ConvertFrom-Json}}
} | % {
    $UserList.Add((New-Object -TypeName PSObject -Property @{
        Description         = "$($_.Gender), $($_.Height)cm for $($_.Mass)kg. $($_.'Hair_Color') hair, $($_.'Skin_Color') skin, $($_.'Eye_Color') eyes. Born in $($_.'Birth_Year')."
        Office              = $_.Homeworld.Name
        MemberOf            = ($_.Films.title -join ','),($_.species.Name -join ',') -join ',' #,($_.vehicles.Name -join ','),($_.starships.Name -join ',')
        Name                = $_.Name
        DisplayName         = $_.Name
        GivenName           = ($_.Name -split ' ')[0]
        Surname             = ($_.Name -split ' ')[-1]
        SamAccountName      = ("$(($_.Name -split ' ')[0]).$(($_.Name -split ' ')[-1])" -replace ' ','')[0..19] -join ''
        EmailAddress        = "$(($_.Name -split ' ')[0]).$(($_.Name -split ' ')[-1])@$MailDomain"
        UserPrincipalName   = "$(($_.Name -split ' ')[0]).$(($_.Name -split ' ')[-1])$UPNSuffix"
    })) | Out-Null
}
#Films
1..$(((Invoke-WebRequest -Uri $ApiUrl/films).Content | ConvertFrom-Json).count) | % {
    (Invoke-WebRequest -Uri $ApiUrl/films/$_).Content | ConvertFrom-Json
} | % {
    $GroupList.Add((New-Object -TypeName PSObject -Property @{
        Name = $_.title
        Info = $_.opening_crawl -join ''
        Description = "Produced by $($_.producer), diected by $($_.director) released on $($_.release_date)"
    })) | Out-Null
}
#Species
1..$(((Invoke-WebRequest -Uri $ApiUrl/species).Content | ConvertFrom-Json).count) | % {
    (Invoke-WebRequest -Uri $ApiUrl/species/$_).Content | ConvertFrom-Json
} | % {
    $GroupList.Add((New-Object -TypeName PSObject -Property @{
        Name = $_.Name
        Description = "average_height : $($_.average_height), skin_colors : $($_.skin_colors), hair_colors : $($_.hair_colors), eye_colors : $($_.eye_colors), average_lifespan : $($_.average_lifespan)"
        Info = $_.language
    })) | Out-Null
}
#planets
1..$(((Invoke-WebRequest -Uri $ApiUrl/planets).Content | ConvertFrom-Json).count) | % {
    (Invoke-WebRequest -Uri $ApiUrl/planets/$_).Content | ConvertFrom-Json
} | % {
    $SiteList.Add((New-Object -TypeName PSObject -Property @{
        Name = $_.Name -replace ' ',''
        Description = "diameter : $($_.diameter), climate : $($_.climate), gravity : $($_.gravity), terrain : $($_.terrain), surface_water : $($_.surface_water), population : $($_.population), rotation_period : $($_.rotation_period), orbital_period : $($_.orbital_period)."
    })) | Out-Null
}

$UserList | Export-Csv -NoTypeInformation -Delimiter ';' Users.csv -Encoding UTF7
$GroupList | Export-Csv -NoTypeInformation -Delimiter ';' Groups.csv -Encoding UTF7
$SiteList | Export-Csv -NoTypeInformation -Delimiter ';' Sites.csv -Encoding UTF7

New-ADOrganizationalUnit -Name 'Star Wars' -Path $ParentOU
New-ADOrganizationalUnit -Name Users -Path "OU=Star Wars,$ParentOU"
New-ADOrganizationalUnit -Name Groups -Path "OU=Star Wars,$ParentOU"

Import-Csv -Path Groups.csv -Delimiter ';' -Encoding UTF7 | % { $_ | New-ADGroup -Path "OU=Groups,OU=Star Wars,$ParentOU" -GroupCategory Security -GroupScope Global -OtherAttributes @{Info = $_.info}}
Import-Csv -Path Users.csv -Delimiter ';' -Encoding UTF7 | % { 
    $NewUser = $_ | New-ADUser -Path "OU=Users,OU=Star Wars,$ParentOU" -AccountPassword (ConvertTo-SecureString -AsPlainText -Force -String $AccountPassword) -Enable $true -PassThru
    $_.MemberOf -split ',' | % {Add-ADGroupMember -Identity $_ -Members $NewUser}
    $NewUser = $null
}
Import-Csv -Path Sites.csv -Delimiter ';' -Encoding UTF7 | % { $_ | New-ADReplicationSite}

Here under the created CSV, you may want to remove my lab references in it, or create your own with those PowerShell lines.

The CSV’s

Groups Users Sites

The result:

Once you imported the CSV :

Active Directory Star Wars Users - OU

Active Directory Star Wars Users – OU

Active Directory Star Wars Users - Groups

Active Directory Star Wars Users – Groups

Active Directory Star Wars Users - Users

Active Directory Star Wars Users – Users

Active Directory Star Wars Users - Sites

Active Directory Star Wars Users – Sites

You got a nice bunch of Active Directory users way more fun than  your usual ones, and the groups are full of member based on the movies they are in, and their specie.

One thought on “Active Directory Star Wars Users

Leave a Reply

Your email address will not be published. Required fields are marked *