First thing Amy has to do every Monday is to create mailboxes for all new hires that week. The Human Resources (HR) department sends a comma-separated value file (CSV) to Amy every Monday morning that has the names and user names of the new hires. A typical CSV file from HR looks like this:
Name,User Name
David Hamilton,DHamilton
Ezio Alboni,EAlboni
Rajesh M. Patel,RPatel
Kevin Liu,KLiu
When creating the new mailboxes, Amy needs to accomplish all of the following tasks:
-
Add the user principal name (UPN) extension contoso.com to each user name.
-
Assign pass@word1 as the initial password.
-
Configure the user to change the password at first logon.
-
Specify the mailbox database and the organizational unit (OU) in which to place the new mailboxes.
When this recurring task was first assigned to her, she decided to use the Exchange Management Shell to accomplish it in a rapid manner. She started out by running the following New-Mailbox command, which creates a test user called Test User 1.
New-Mailbox -Name "Test User 1" -UserPrincipalName "TUser1@contoso.com" -OrganizationalUnit "contoso.com/Users" -Database "Server01\Mailbox Database" -ResetPasswordOnNextLogon $true
The command worked, but prompted her to enter the password for the new user. The New-Mailbox cmdlet only accepts secure strings for the password parameter, so she couldn't use the value of the default password as clear text in the command. To get around this, she used the ConvertTo-SecureString cmdlet to create a secure string from the default password and store it in the user-defined variable $Temp. She could then pass the variable to the password parameter of the New-Mailbox cmdlet.
$Temp = ConvertTo-SecureString "pass@word1" -asPlainText -force
New-Mailbox -Name "Test User 1" -UserPrincipalName "TUser1@contoso.com" -OrganizationalUnit "contoso.com/Users" -Database "Server01\Mailbox Database" -Password $Temp -ResetPasswordOnNextLogon $true
The next step was to read the user information from the CSV file and automate the provisioning. After reviewing the bulk management examples in the Microsoft Exchange Team blog Exchange Server 2007 recipient management one-liners, she modified her own script to accomplish this task.
Note: |
|---|
|
The content of each blog and its URL are subject to change without notice.
|
$Temp = ConvertTo-SecureString "pass@word1" -asPlainText -Force
Import-CSV "C:\NewUsers.csv" | ForEach-Object -Process {New-Mailbox -Name $_.Name -UserPrincipalName "$($_.UserName)@contoso.com" -OrganizationalUnit "contoso.com/Users" -Database "Server01\Mailbox Database" -Password $Temp -ResetPasswordOnNextLogon $true} Amy was able to set up this script by copying the syntax from the example in the blog article. However, she didn't quite understand what the $_ syntax did. After doing a little research, she found the topic Shell Variables. The article states that $_ is an Exchange Management Shell variable that contains the current pipeline object. Therefore, every time the script block specified with the Process parameter is executed in her script, the $_ variable contains the data from the next row of the CSV file.
Another part that was not clear to Amy was the syntax used when setting the UPN. It was different because the value of the variable was not directly assigned to a parameter of the New-Mailbox cmdlet, but rather it was used to modify a string that was assigned to the parameter. Amy reviewed the topic Syntax, which discusses the command syntax for the Exchange Management Shell and found out that the $() syntax is used to substitute the output of a command as an argument in a script. In the Exchange Management Shell, if you input only a variable, the output is the value of that variable. Therefore, when you place a variable inside a $() block, that block is substituted with the value of the variable. In her script, the variable $_.UserName contains the user name. By placing this variable inside a $() syntax, Amy was able to insert the value of the variable to the string that is assigned to the UserPrincipalName parameter.
After learning more about the command syntax in the Exchange Management Shell, Amy went one step further and moved the first line of her script to the Begin parameter of the ForEach-Object cmdlet and turned this into her very own bulk recipient management one-liner. Every Monday, she has simply been plugging the latest CSV file from HR to the following script.
Import-CSV "C:\NewUsers.csv" | ForEach-Object -Begin {$Temp = ConvertTo-SecureString "pass@word1" -asPlainText -Force} -Process {New-Mailbox -Name $_.Name -UserPrincipalName "$($_.UserName)@contoso.com" -OrganizationalUnit "contoso.com/Users" -Database "Server01\Mailbox Database" -Password $Temp -ResetPasswordOnNextLogon $true} However, today's CSV file looks a little different. The HR department added titles and departments of the new employees to the CSV file.
Name,User Name,Title,Department
Deepak Kumar,DKumar,Marketing Analyst,Marketing
Ray Chow,RChow,Sales Associate,Sales
David Simpson,DSimpson,Sales Associate,Sales
Isabel Martins,IMartins,Administrative Assistant,Accounting
Amy's script as it stands won't work with the new CSV file because the Title and Department fields can only be updated by using the Set-User cmdlet. At first thought, she considers writing another script to read the same file and then use the Get-User cmdlet and pipeline its output to Set-User cmdlet. However, because she can also pipeline the output of the New-Mailbox cmdlet to the Set-User cmdlet, she ends up simply modifying her existing one-liner.
Import-CSV "C:\NewUsers.csv" | ForEach-Object -Begin {$Temp = ConvertTo-SecureString "pass@word1" -asPlainText -force} -Process {New-Mailbox -Name $_.Name -UserPrincipalName "$($_.UserName)@contoso.com" -OrganizationalUnit "contoso.com/Users" -Database "Server01\Mailbox Database" -Password $Temp -ResetPasswordOnNextLogon $true | Set-User -Title $_.Title -Department $_.Department} To make sure that her syntax is correct, Amy consults the Using the Exchange Management Shell and Mailbox and Recipient Cmdlets topics in Microsoft Exchange Server 2007. After running her new one-liner, she browses Scripting with Windows PowerShell: Script Center to learn more about scripting in Microsoft Windows PowerShell.