Enjoy Sharing

Managing Systems with Windows PowerShell (Part 1)

Here’s the session that I did in TechEd SEA 2007 some three weeks back which was held in Kuala Lumpur Convention Center, Malaysia.
 
Managing Systems with Windows PowerShell – SYS236
 
In this track, I talked about how to get started with PowerShell and how this will help network administrators (yes, you AD and Exchange admins) to better manage your systems more easily. This session was not meant for showing everything about PowerShell, but rather how to get yourself started to discover PowerSell, thus, learning to fish! Here are some scripts I used for my demos.
 
Command Line Utilities
You can pipe multiple commands together as you would normally do with writting a bach file in the command prompt.
 
"ipconfig /all >C:tshoot.txt; route print >>C:tshoot.txt; netdiag /q >>C:tshoot.txt; net statistics workstation >>C:tshoot.txt"
 
This command does some basic troubleshooting steps on your computer and output’ing the results on to a text file. The ">" directs the results of the command into a text file called "tshoot.txt" as most already know. The ">>" appends to the end of the file without overwritting existing contents in that file. The ";" acts as the pipe in PowerShell seperating and executing each command sequentially.
 
 
Creating OUs
$aryText = Get-Content -Path "c:ScriptsMultiValued.txt"

$strCLass = "User"
$intUsers = 9
$strName = "cn=tempUser"
$objADSI = [ADSI]"LDAP://ou=myTestOU,dc=nwtraders,dc=msft"
for ($i=1; $i -le $intUsers; $i++)
{
$objUser = $objADSI.create($strCLass, $StrName+$i)
$objUser.setInfo()
$objUser.put("streetAddress", $aryText[0])
$objUser.put("postOfficeBox", $aryText[1])
$objUser.put("l", $aryText[2])
$objUser.put("st", $aryText[3])
$objUser.put("postalCode" , $aryText[4])
$objUser.put("c", $aryText[5])
$objUser.put("co", $aryText[6])
$objUser.put("countryCode", $aryText[7])
$objUser.Put("facsimileTelephoneNumber", $aryText[8])
$objUser.Put("info", $aryText[9])
$objUser.setInfo()
}
 
  • The first line declares a variable which is an array and using the Get-Content to hold the content gathered from the text file.
  • Then define a class into a variable called $strClass.
  • Depending on the number of users you want to create, define a variable to hold the number of users to create.
  • Here we’ll use a prefix to the user we are going to create; $strName
  • Create a variable called $objADSI which will be user to hold the object that is returned by the [ADSI] accelerator.
  • The rest of the code is encapsulated in a for loop. First using the ‘create’ method to on the user object and then each line after that makes changes to the properties of the object.
  • Make sure to execute the ‘setInfo()’ to write the changes to Active Directory
 
 User Properties
User Properties 2
 
Advertisements

2 responses

  1. Dmitry

    Have you tried using AD cmdlets instead? Provisioning from CSV is then just one line of PowerShell code, and you don’t have to learn the ADSI methods or AD schema attribute names:Import-CSV c:\users.csv | ForEach-Object { New-QADUser -Name $_.Name -ParentContainer nwtraders.msft/myTestOU -City $_.City -Company $_.Company -Department $_.Department}I am not listing all possible attributes but I guess this should be enough to get the idea. PowerShell is great when used with domain-specific cmdlets (AD, Exchange, etc.). When you stick back to ADSI it is not much different from VBScript on a different syntax. My humble opinion, of course. 😉

    October 2, 2007 at 5:18 pm

  2. Noel

    Indeed TechEd SEA 2007 was a great showcase of technologies. Wei King did 2 sessions and both had quite a bit of work poured into them. Having worked with Wei King for the past few years, he is progressively becoming a real leader in the area of system management. In the area of scripting, he has agreed to take a lead and this session was his first full attempt. I want to applaud him on his effort. Keep going and keep learning. 🙂

    October 4, 2007 at 1:29 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s