Move or Enable Multiple User Accounts

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:

SipUri

sip:seeuser11@p10.ca

MoveOrEnable

Enable

Target

cspool.p10.ca

UPN

seeuser11@p10.local

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 "seeuser11@p10.local" –RegistrarPool "cspool.p10.ca" –SipAddress "sip:seeuser11@p10.ca"

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"

Start-Transcript $transcriptname

foreach ($importeduser in $importedusers)

    {

        if ($importeduser.MoveorEnable -eq "Move")

            {

                Move-CsUser $importeduser.SipUri -target `
$importeduser.Target -verbose

            }

        else

    {

        Enable-CsUser $importeduser.UPN -SipAddress `
$importeduser.SipUri -RegistrarPool `
$importeduser.Target -Verbose

    }

    }

Stop-Transcript

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:\Scripts\MoveOrEnableUsers.ps1). In addition, you must create a .CSV file similar to this, and save that file to your hard drive as well:

SipUri,MoveorEnable,Target,UPN

sip:seeuser11@p10.ca,Enable,"cspool.p10.ca",seeuser11@p10.local

sip:seeuser12@p10.ca,Move,"cspool.p10.ca",seeuser12@p10.local

sip:seeuser13@p10.ca,Enable,"cspool.p10.ca",seeuser13@p10.local

sip:seeuser14@p10.ca,Enable,"cspool.p10.ca",seeuser14@p10.local

sip:seeuser15@p10.ca,Enable,"cspool.p10.ca",seeuser15@p10.local

sip:seeuser16@p10.ca,Enable,"cspool.p10.ca",seeuser16@p10.local

sip:seeuser17@p10.ca,Enable,"cspool.p10.ca",seeuser17@p10.local

sip:seeuser18@p10.ca,Move,"cspool.p10.ca",seeuser18@p10.local

sip:seeuser19@p10.ca,Enable,"cspool.p10.ca",seeuser19@p10.local

To run the script from within the Lync Server Management Shell, just type the full path to the .PS1 file and then press ENTER:

C:\Scripts\MoveOrEnableUsers.ps1

When the script starts, it will prompt you to enter the location of the .CSV file. Type in the file path (e.g., C:\Scripts\Users.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:

MoveorEnableUsers2010-06-10T09-33-03.txt