Submitted by Scott Stubberfield and Nick Smith, Microsoft
Windows PowerShell makes it easy for you to enable a new user for Microsoft Lync Server 2010, and makes it just as easy for you to move a single user account from one Registrar pool to another. But what if you need to perform these user management tasks on a whole bunch of users, some of whom need to be enabled for Lync Server and some of whom need to have their accounts moved to a different Registrar pool? What do you do then?
Scott Stubberfield and Nick Smith have come up with one solution: you simply put all this information in a text file and then use a PowerShell script to read that text file and then take the appropriate action. The script they put together reads a comma-separated values file (a file containing user information), loops through the collection of users listed on that file and then does one of two things:
· If the MoveOrEnable field for a user is marked as Move, the script uses the Move-CsUser cmdlet to move the user account from its current Registrar pool to a new Registrar pool (the Target field).
· If the MoveOrEnable field for a user is marked as Enable, the script uses the Enable-CsUser cmdlet to enable the user account for Lync Server. The script assigns the user to the Registrar pool indicated by the Target field, and gives the user the SIP address listed in the SipUri field.
For example, suppose the first user listed in the .CSV file has these property values:
Because MoveOrEnable is set to Enable, the script will enable the user for Lync Server, and will do so using the following command:
Enable-CsUser –Identity “firstname.lastname@example.org” –RegistrarPool “cspool.p10.ca” –SipAddress “sip:email@example.com”
Nice, huh? And the best part is this: the script will take care of everything whether you have one user listed in the .CSV file or 100,000 users listed in the .CSV file.
Here’s what the code looks like:
param( [string] $importfile = $(Read-Host -prompt `
“Please enter a file name”))
$importedusers = Import-CSV $importfile
$transcriptname = “MoveorEnableUsers” + `
(Get-Date -format s).Replace(“:”,”-“) +”.txt”
foreach ($importeduser in $importedusers)
if ($importeduser.MoveorEnable -eq “Move”)
Move-CsUser $importeduser.SipUri -target `
Enable-CsUser $importeduser.UPN -SipAddress `
$importeduser.SipUri -RegistrarPool `
To make use of this script, copy the code to Notepad (or any other text editor), and then save the file using a .PS1 file extension (for example, C:ScriptsMoveOrEnableUsers.ps1). In addition, you must create a .CSV file similar to this, and save that file to your hard drive as well:
To run the script from within the Lync Server Management Shell, just type the full path to the .PS1 file and then press ENTER:
When the script starts, it will prompt you to enter the location of the .CSV file. Type in the file path (e.g., C:ScriptsUsers.csv) and press ENTER; in turn, the script will read the data from the .CSV and, as requested, either move or enable each user listed in the file.
As an added bonus, the script also maintains a detailed log of everything it does. That log will be stored in the current working directory, and will have a file name based on the current date and time. For example: