Sunday, December 09, 2012

ServerCore + Create Test Users using Facebook

So Recently my VHD got corrupted and had to make the VHD Boot again work. Thankfully I had all the PowerShell one-liners I used to get my Lab up in less time.

First of all I will add 100 test users by following this awesome Hey Scripting Guys Blog Post.

To make things a bit clear I am on a workstation with RSAT for Active Directory installed and my DC is a servercore machine.

Just to give a hint when I need to run a cmdlet to get any information from the AD, I need to specify the name of the DC with -Server  parameter from where I want to get the information but a new connection will be opened and closed for each cmdlet invocation.

For Example :
Get-ADForest -Server

Better way explained in Active Directory PowerShell Blog is to map a PSDrive for better performance. Since I have only one DC in my test environment , when I Import the ActiveDirectory Module it will automatically map a PSDrive on my remote workstation.

Now I don't want to add users like TestUser01, TestUser02... So I began to think from where can I get the data to create few test users for my Domain....and the answer to it is what better than FaceBook :P

Below are the steps I carried out:

Create a new Organizational Unit
I used the following cmdlet to create an OU named 'IT' under
   New-ADOrganizationalUnit -Name IT -Path 'dc=dexter,dc=com' -ProtectedFromAccidentalDeletion:$false

Get the relevant data from Facebook

To get the some basic info for the User I used already available PowerShell Module for Facebook( credits to the Developer). On a how to use this module see my earlier post.

Set up a New FB Connection and be ready to retrieve data from Facebook 

 You can see that I have stored all my FB friends in $friends, but it doesn't return much information. What can be done here..there is an another cmdlet Get-FBObjectData which returns objects with greater deal of information. So just expand the property ID and pipe it and use as below

Better save this valuable data as a facebook.xml which I will use later.....

Create the AD Users 

Now I have all the information I could gather from Facebook into an XML file for later use. Now I will do some manipulation with the Objects I get from ...Below is the Script block which I used.

Above is the Script block that I used later from the PowerShell ISE.
First import the XML data into variable $facebookdata then do some Object manipulation in order to be consistent with the AD User attributes. Finally pipe it into the New-ADUser cmdlet with the -path parameter specifying the OU in which the Users need to be created.

Note that there is a -whatif parameter added at last to just check if we are headed in the right direction. Execute the Script block to get something like below ouput

If you read it properly it tells our success story :)
Go ahead and remove the -whatif parameter and execute the Script block again

When I run the script block I get two errors :
Below one is because some of my friends have same name and the other one is because of some formatting issues in the name of some friends.

Finally verify the User creation:

That's all for today.
Thank you!