Create and Configure Users for Synthetic Transactions

Submitted by Antenehe Temteme, Microsoft

This script will perform the following actions:

1. Create Active Directory users

2. Enable those users for Lync Server 2010

3. Create health monitoring configuration for use with synthetic transactions when they are run from System Center Operations Manager (SCOM)

Copy the following script, paste it into Notepad (or your favorite script editor), and save it as CreatandConfigureSTUsers.ps1. (You can choose a different name, just be sure to use that name when calling the script; see the Usage section below.)

Script

Usage

Parameters

Examples

Output

Script

param(

    [string] $domainFqdn = $(Read-Host -prompt "Plaese Enter Domain FQDN"),

    [string] $poolFqdn = $(Read-Host -prompt "Plaese Enter Pool FQDN"),

    [string] $userPrefix = $(Read-Host -prompt "Plaese Enter User Prefix"),

    [string] $OUname,

    [switch] $EnableEnterpriseVoice = $false,

    [string] $User1telURI,

    [string] $User2telURI

     

    )

Import-Module Lync

##############################################################################################################################

# This function takes Pool FQDN and returns the Pool's short name

##############################################################################################################################

function GetPoolShortName([string]$poolFQDN)

{

   if(-not([String]::IsNullOrEmpty($poolFqdn)) -and $poolFqdn.Contains(".") )

   {

      [int] $index=$poolFqdn.IndexOf('.')

  $poolName =$poolFqdn.SubString(0,$index)

      $poolName

   }

   else

   {

      $(throw "Pool FQDN can not be Empty or Pool FQDN is not in a valid format")

   }

}

############################################################################################################################

# This function accepts the Identity of user and registrar pool and enables the user for Lync Server

############################################################################################################################

function EnableUserforLS([string]$Identity,[string]$RegistrarPoolFQDN)

{

      try

      {

            Enable-CsUser -Identity $Identity -RegistrarPool $poolFqdn -SipAddressType UserPrincipalName

      }

      catch

      {

            $( throw "Exception encountered when trying to enable user for Lync Service " -f $_.Exception.ToString())

      }

     

}

############################################################################################################################

# This function accepts pool FQDN, first and second user sip uris and creates health monitoring configuration

############################################################################################################################

function CreateHealthMonitoringConfiguration([string]$poolFQDN,[string]$firstUserSipURI,[string]$secondUserSIPURI)

{

      try

      {

            New-CsHealthMonitoringConfiguration $poolFqdn -FirstTestUserSipUri $firstUserSipURI -SecondTestUserSipUri $secondUserSIPURI

      }

      catch

      {

            $(throw "Exception encountered when trying to create health monitoring configuration " -f $_.Exception.ToString())

      }

}

############################################################################################################################

# This function accepts pool FQDN and verifies whether the pool is capable of hosting users or not

############################################################################################################################

function CheckPoolValidity($poolFQDN)

{

   if([String]::IsNullOrEmpty($poolFQDN))

   {

      $(throw "Pool FQDN can not be Empty.")

   }

   else

   {

      $topo=Get-CsTopology

      foreach($cluster in $topo.Clusters)

      {

           

            [string] $pool=$cluster.Fqdn

            if($pool.ToLower() -eq $poolFqdn.ToLower())

            {

                  $registrarService = $cluster.InstalledServices[[Microsoft.Rtc.Management.Core.RoleName]::Registrar]

                  return (($registrarService -ne $null) -and ($registrarService.IsDirector -eq $false))

            }

      }

   }

}

################################################################################################################################

# Takes OUDN and User prefix and creates Disabled AD users, Enables the users for Lync Server for a pool and creates health

# monitoring configuration

################################################################################################################################

function CreateADUserandEnableForLS([string]$OuDN , [string]$userName,[string]$lineURI)

{

    $ldpPath= "LDAP://" + $OuDN

    $container= [ADSI]$ldpPath

    $container.psbase.Get_children()

   

    $usercommonName= "CN=" + $userName

      $newUser = $container.Create("User",$usercommonName )

      $newUser.Put("sAMAccountName", $userName)

      $newUser.Put("name", $userName)

      $dsecription= "ST Test Account " + $userName + "for " + $poolName

      $newUser.put("description", $dsecription )

      $newUser.put("sAMAccountName", $userName)

      $newUser.put("givenName", $userName)

      $newUser.put("userPrincipalName", $userName + "@" + $domainFqdn )

      $newUser.SetInfo()

    $newUser.psbase.InvokeSet("AccountDisabled", $false)

      $newUser.SetInfo()

    $newUser.SetPassword("P@55w0rd")

      $Identity=$usercommonName + "," + $OuDN

      EnableUserforLS $Identity $poolFQDN

   

     

}

###############################################################################################################################

# Takes pool FQDN and checks if health monitoring configuration exists for the pool or not

###############################################################################################################################

function HealthMonitoringConfigurationNotExists([string]$poolFqdn)

{

      $hms=Get-CsHealthMonitoringConfiguration

      $hmnotFound=$true;

      foreach($hm in $hms)

      {

            if($hm.Identity -eq $poolFqdn)

            {

                  $hmnotFound=$false

                  return

            }

      }

      $hmnotFound

}

################################################################################################################################

# Takes domain Fqdn and returns distinguished name of domain

################################################################################################################################

function GetDomainDN($domainFQDN)

{

      if(-not([String]::IsNullOrEmpty($domainFQDN)))

      {

            $splittedList=$domainFQDN.split('.')

            for($i = 0;$i -le $splittedList.Length-1; $i++)

            {

                  $domainDN = $domainDN + "DC=" + $splittedList[$i]

                  if($i -ne $splittedList.Length-1)

                  {

   $domainDN= $domainDN + ",";

                  }

            }

            $domainDN

      }

      else

      {

  $(throw "Domain FQDN can not be Empty.")

      }

}

if([String]::IsNullOrEmpty($domainFQDN))

{

    $(throw "Domain FQDN can not be null or empty")

}

$domainDN=GetDomainDN $domainFqdn;

if([String]::IsNullOrEmpty($ouName))

{

      $ouName="Users"

      $OUDN= "CN=" + $OUName + "," + $domainDN

}

else

{

      $OUDN= "OU=" + $OUName + "," + $domainDN

}

$poolValid=CheckPoolValidity $poolFqdn

if($poolValid -eq $true)

{

     

      if((HealthMonitoringConfigurationNotExists $poolFqdn) -eq $true )

      {

        $poolShortName =GetPoolShortName $poolFqdn

            $user1=$UserPrefix + "0." + $poolShortName

            $user2= $UserPrefix + "1." + $poolShortName

            $user1SipURI="sip:" + $user1 + "@" + $domainFqdn

            $user2SipURI="sip:" + $user2 + "@" + $domainFqdn

        [string]$User1Identity = "CN=" + $user1 + "," + $OUDN

        [string]$User2Identity = "CN=" + $user2 + "," + $OUDN

       

            CreateADUserandEnableForLS $OUDN $user1

            CreateADUserandEnableForLS $OUDN $user2

        sleep 30

        if($EnableEnterpriseVoice -eq $true)

        {

            if([String]::IsNullOrEmpty($User1telURI))

            {

                $User1telURI=$(Read-Host -prompt "Plaese Enter User1 Telephone URI ")

            }

            if([String]::IsNullOrEmpty($User2telURI))

        {

                $User2telURI=$(Read-Host -prompt "Plaese Enter User2 Telephone URI ")

            }

            try

            {

                Set-CsUser -Identity $User1Identity -EnterpriseVoiceEnabled $true -LineURI $User1telURI

                Set-CsUser -Identity $User2Identity -EnterpriseVoiceEnabled $true -LineURI $User2telURI

            }

            catch

      {

            $(throw "Exception encountered when trying to enable user for enterprise voice" -f $_.Exception.ToString())

      }

        }

            CreateHealthMonitoringConfiguration $poolFqdn $user1SipURI $user2SipURI

      }

      else

      {

        $Message = "No action required as health monitoring configuration already exists for: " + $poolFqdn

        write-host $Message -ForegroundColor Green -BackgroundColor Black

      }

}

else

{

      $(throw "Pool is not a valid Registrar pool.")

}

Usage

CreateandConfigureSTUsers.ps1 -domainFQDN <DomainFQDN> -poolFQDN <PoolFQDN> -userPrefix <UserPrefix> -OUname <OUName> - EnableEnterpriseVoice -User1telURI <User1TelURI> -User2telURI <User2TelURI>

Parameters

domainFQDN: Domain where users are to be created. This parameter is required. If not specified Admin will be prompted for a value.

poolFQDN: Lync Server registrar pool where users will be hosted. This script will succeed only if health monitoring has not been configured on the specified pool. This parameter is required. If not specified Admin will be prompted for a value.

userPrefix: Prefix to be used when creating users. This parameter is required. If not specified Admin will be prompted for a value. The script creates two users per registrar pool. The prefix is used to create a user name in the format <userprefix><usernumber>.<poolname>@<domain>. For example, a userPrefix value of TEST on the domain fabrikam.com with the registrar pool redmond1.fabrikam.com would create two test users with the following SIP URIs: sip:TEST0.redmond1@fabrikam.com and sip:TEST1.redmond1@fabrikam.com.

OUname: Organizational unit under which users will be created. The OU should already exist in Active Directory. This parameter is optional; if no value is specified, the user OU will be used.

EnableEnterpriseVoice: A switch parameter that, when specified, will enable users for enterprise voice. This parameter is optional. If this switch is specified then the User1telURI and User2telURI parameters are required.

User1telURI and User2telURI: Telephone URI for user 1 and user 2. The tel URIs should be in the following format: +14250001000. Values for these parameters are required if the EnableEnterpriseVoice switch is specified.

Examples

Example 1

PS C:\> C:\Scripts\CreateandConfigureSTUsers.ps1 –domainFQDN fabrikam.com –poolFQDN Redmond1.fabrikam.com –userPrefix TEST –OUname Sales

If a gateway is detected, a warning will be displayed that Enterprise Voice should be enabled. In that case, run the script as shown in Example 1.

Example 2

PS C:\> C:\Scripts\CreateandConfigureSTUsers.ps1 –domainFQDN fabrikam.com –poolFQDN Redmond1.fabrikam.com –userPrefix TEST –OUname Sales –EnableEnterpriseVoice –User1telURI +14255551212 –User2telURI +12065551234

Output

Identity : redmond1.fabrikam.com

FirstTestUserSipUri : sip:TEST0.redmond1@fabrikam.com

FirstTestSamAccountName :

SecondTestUserSipUri : sip:TEST1.redmond1@fabrikam.com

SecondTestSamAccountName :

TargetFqdn : redmond1.fabrikam.com