Pages

Thursday, March 28, 2013

Lync PowerShell Script to Mass Enable Users

* Updated 1/22/15 - I added the option to add dial plan and voice policy as well to the script
* Updated 6/20/13 - I forgot to add the dash in set-csuser on the bottom script so the enable-csuser cmdlet would fail.

Of course there is always a need to mass enable users for Lync once you have Lync deployed in your environment. The easiest way to mass enable users for Lync is via a PowerShell script and a CSV file with the user information. 

To enable PC-to-PC users only
Here is a script I created to mass enable users:

if ($args[0] -eq $null)
    {
    $userNameFile = read-host "Enter the full path of the .csv file with the user information."
    $userNameFile  = $usernamefile -replace '"',""} 
else 
    {$usernamefile = $args[0]}
if ($userNameFile -ne "") 
    {$csv=import-csv $userNameFile} 
else 
    {"Could not find a valid .csv with the user information."
    exit}
foreach($c in $csv)
# enable for lync
{
"Enabling " + $c.Identity + " for Lync 2010"
Enable-csuser -identity $c.Identity -registrarpool $c.RegistrarPool –sipaddresstype $c.SipAddressType -sipdomain $c.SipDomain
}

This script will enable the users with basic PC-to-PC configuration. This script allows for organizations with multiple SIP domains and multiple pools with the columns in the .csv file for RegistrarPool and SipDomain. The .csv file will look like this:
Identity RegistrarPool SipAddressType SipDomain
John Doe PoolFQDN.Domain.com EmailAddress SIPDomain.com


To enable Enterprise Voice users
To enable the users for Enterprise Voice, we will make the following addition (highlighted in yellow and green) to the above script. The green highlighted section is optional if you have extensions set up in your LineURI’s like tel+12815551234;ext=1234. You can omit this section and remove the Extensions column from the .csv file if you are not using extensions in your LineURI’s:

if ($args[0] -eq $null)
    {
    $userNameFile = read-host "Enter the full path of the .csv file with the user information."
    $userNameFile  = $usernamefile -replace '"',""} 
else 
    {$usernamefile = $args[0]}
if ($userNameFile -ne "") 
    {$csv=import-csv $userNameFile} 
else 
    {"Could not find a valid .csv with the user information."
    exit}
foreach($c in $csv)
# enable for lync
{
"Enabling " + $c.Identity + " for Lync"
$lineuri = "tel:+1" + $c.PhoneNumber + ";ext=" + $c.Extension
Enable-csuser -identity $c.Identity -registrarpool $c.RegistrarPool -sipaddresstype $c.SipAddressType -sipdomain $c.SipDomain 

# Pause for 30 seconds for AD Replication
write-host -foregroundcolor Green "Pausing for 30 seconds for AD Replication"

Start-Sleep -s 30

Set-CsUser -Identity $c.Identity -enterprisevoiceenabled $True -lineuri $lineuri
Grant-CsDialPlan -Identity $c.Identity -PolicyName $c.DialPlan
Grant-CsVoicePolicy -Identity $c.Identity -PolicyName $c.VoicePolicy
}

As you can see, the script turns the 10 digit phone number into E.164 format and adds the extension (if needed) and then sets it as the LineURI for the user. The .csv file will have the following columns added for this script:


PhoneNumber Extension DialPlan VoicePolicy
2815551234 1234 HoustonDialPlanHoustonVoicePolicy




8 comments:

  1. Hi, great script really helps a lot. How can I get the results exported to a file to review accounts which were not enabled.

    ReplyDelete
    Replies
    1. Dewalt,
      You will have to test this but try adding the following to the very beginning of the script:

      $transcriptname = "ScriptResults_" + (Get-Date -Format s).Replace(":","-") +".txt"
      Start-Transcript $transcriptname
      $ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Path -Parent

      Then add the following to the very end of the script:

      Stop-Transcript

      Basically all this will do is write what you see on the screen to a text file and allow you to parse thru it to see who did not get enabled.

      Delete
  2. I had to make a slight edit to the EV part of the script as -enterprisevoiceenabled is not a parameter of enable-csuser, but rather set-csuser:

    Enable-csuser -identity $c.Identity -registrarpool $c.RegistrarPool -sipaddresstype $c.SipAddressType -sipdomain $c.SipDomain
    Set-CsUser -identity $c.Identity -enterprisevoiceenabled $True -lineuri $lineuri

    ReplyDelete
    Replies
    1. Great catch Alan. I fixed the post to show the right way to do it. I had originally put it in that way but was trying to be slick and streamline it and didn't test it.

      Delete
  3. Hello, great script. We have a couple of dial and voice plans. How can they be included in the script?

    ReplyDelete
    Replies
    1. This is already included in the script. Just put the proper dial plan name and voice policy in the CSV.

      Delete
  4. I can't get this to work.. Enable-CsUser : Cannot bind argument to parameter 'Identity' because it is null.

    Is my .csv wrong maybe?

    ReplyDelete
    Replies
    1. Check the header of the column to make sure it is Identity and then verify the entries you have under identity are valid.

      Delete