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

Comments

  • Anonymous
    January 01, 2003
    Hey Michael, Sorry we didn't get to this sooner, but it looks like you found your answer here: powershell.com/.../6783.aspx. As mentioned in that post, just set the -Confirm parameter to $False on the call to Move-CsUser.

  • Anonymous
    January 01, 2003
    Hey atif, Can you give us some more details on what didn't work and what types of error messages you got? You can email us at cspshell@microsoft.com if that would be easier.

  • Anonymous
    January 01, 2003
    Thanks Alec! Glad you got it working well for you.

  • Anonymous
    March 24, 2011
    Thanks so much for this great info.  I had some issues with the code and wanted to share with you my solution.  My version removes the if statement for MOVING users and only gives the option to ENABLE users. This should work. 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 ($importedusers in $importedusers)    {         Enable-CsUser $importedusers.UPN -SipAddress $importedusers.SipUri -RegistrarPool $importedusers.Target -Verbose    } Stop-Transcript

  • Anonymous
    June 17, 2011
    from where i can get this script??? can you please help in getting this script. as i have round about 6000 users :(

  • Anonymous
    June 17, 2011
    i tried to copy paste the script but it didn't work for me.. can any one help?

  • Anonymous
    June 29, 2011
    . When I run it it runs fine and imports the spreadsheet as designed and is successful in moving the users. The issue I have is, during the process it asks if you want to move the user and the choices are Y – yes, A – yes to all, N – no, and L – no to all. If I choose yes it works and moves to the next user and asks again. If I choose A for yes to all it moves the user and instead of moving on and doing the same to all the users it acts like I said yes and asks me again at each and every user. Does anyone there who is familiar with powershell know why the yes to all does not work as it should??