Creating User Accounts: Step-by-Step Exercises


In this exercise, we will explore the use of Windows PowerShell to create several users whose names are contained in a text file.

  1. Open Notepad or some other Windows PowerShell script editor.

  2. Locate the image from book UserNames.txt file, and ensure you have access to the path to this file. The file contains a listing of users’ first and last names. A sample from this file is shown here:

     Chuck,Adams Alice,Jones Bob,Dentworth

  3. On the first line of your script, create a variable called $aryUsers to hold the array of text that is returned by using the Get-Content cmdlet to read a text file that contains various user first names and last names. Make sure you edit the string that gets supplied to the path argument of the Get-Content cmdlet as required for your computer. This line of code is shown here:

     $aryUsers = Get-Content -path "c:\ch9\UserNames.txt"

  4. On the next line of your script, declare a variable called $password that will contain the password to use for all your users. For this example, the password is Password01. This line of code is shown here:

     $password = "Password01"

  5. On the next line of your script, declare a variable called $strOU to hold the organizational unit to place the newly created users. For this example, place the users in the MyTestOU, which was created in Chapter 7, “Working with Active Directory.” This line of code is shown here:

     $strOU = "myTestOU"

  6. On the next line, declare a variable called $strDomain. This variable will be used to hold the domain name of the organization. This will become part of the user’s e-mail address. For this example, use NwTraders.msft, as shown here:

     $strDomain = "nwtraders.msft"

  7. Now declare a variable called $strDatabase. This variable will hold the name of the database where the users’ mailboxes will reside. On our system, the database is called Mailbox Database. This line of code is shown here:

     $strDatabase = "Mailbox Database"

  8. Use the ConvertTo-SecureString cmdlet to convert the string contained in the variable $password into a secure string that can be used for the password argument of the New-Mailbox cmdlet. To convert a string to a secure string, you need to specify the asplaintext argument for the string contained in the $password variable, and use the force argument to force the conversion. Reuse the $password variable to hold the newly created secure string. This line of code is shown here:

     $password = ConvertTo-SecureString $password -asplaintext -force

  9. Use the ForEach statement to walk through the array of text that was created by using the Get-Content cmdlet to read the text file. Use the variable $i as an individual counter. The variable that holds the array of text from the Get-Content cmdlet is $aryUsers. This line of code is shown here:

     foreach ($i in $aryUsers)

  10. Open and close the code block by using the opening and closing curly brackets, as shown here. You will need space for at least 9 or 10 lines of code, but that can always be added later.

     { }

  11. On the first line inside your code block, use the variable $newAry to hold a new array you will create out of one line of text from the $aryUsers variable by using the Split method. When you call the Split method, supply a comma to it because the default value of the Split method is a blank space. The variable $i holds the current line of text from the $aryUsers variable. This line of code is shown here:

     $newAry = $i.split(',')

  12. The first name is held in the first column in our text file. After this line of text is turned into an array, the first column is addressed as element 0. To retrieve it, we use the name of the new array and enclose the element number in square brackets. Hold the first name in the variable $strFname, as shown here:

     $strFname = $newAry[0]

  13. The last name is in the second column of the text file and is addressed as element 1 in the new array contained in the $newAry variable. Retrieve the value stored in $newAry[1] and store it in the variable $strLname, as shown here:

     $strLname = $newAry[1]

  14. Now we need to use the New-Mailbox cmdlet and supply the values for each of the parameters we have hard-coded in the script, and from the first and last name values stored in the text file. The goal is to not have any of the arguments of the New-Mailbox cmdlet be hard-coded. This will greatly facilitate changing the script to run in different domains and organization units (OUs), and with additional parameters.

  15. On a new line, call the New-Mailbox cmdlet. For the alias argument, create the user’s alias by concatenating the first name contained in the $strFname variable with the last name contained in the $strLname variable. The database that will hold the user’s mailbox is the one supplied in the $strDatabase variable. Because the command will stretch for several lines, use the line continuation command (the grave accent character [`]) at the end of the line. This line of code is shown here:

     New-Mailbox -Alias $strFname$strLname -Database $strDatabase

  16. The next line of our New-Mailbox command creates the user name attribute. To do this, concatenate the first name and last name. The organizational unit name is stored in the $strOU variable. Continue the command to the next line. This line of code is shown here:

     -Name $strFname$strLname -OrganizationalUnit $strOU

  17. The next line is easy. The value for the firstname argument is stored in $strFname, and the value for the lastname argument is stored in the $strLname variable. Use line continuation to continue the command to the next line. This code is shown here:

     FirstName $strFname -LastName $strLname

  18. The displayname for these users will be the first name and the last name concatenated. To do this, use the first name stored in $strFname and the last name stored in $strLname. Continue the command to the next line, as shown here:

     -DisplayName $strFname$strLname `

  19. The userprincipalname value is composed of the first name concatenated with the last name separated from the domain name stored in the $strDomain variable by the commercial at symbol (@). It looks like an e-mail address but is not the same thing. Our code to create this is shown here:

     -UserPrincipalName $strFname$strLname"@"$strDomain `

  20. The value for the password argument is stored in the $password variable. This is the last parameter we need to supply for this command.

     -password $password

  21. Save your script as yournameimage from book CreateUsersFromTxt.ps1. Run your script. If it does not produce the desired results, then compare it with image from book CreateUsersFromTxt.ps1 script.

  22. This concludes this step-by-step exercise.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net