Update to Create-LabUsers - Enable for SfB

This is a further update to my previous postings ()on changes I've made to Aarons script for creating lab users.

One of my requirement was enable users for Skype for Business. This is reasonably straightforward.
In order to connect remotely to the SfB powershell, we need credentials.
Once connected, we can enable the users with a default sip address of their email account (so some checks need to be in place)

Eagled eye readers will notice i've also included a stopwatch to monitor the execution times. Very useful when tweaking the scripts to streamline the proces.
Top to bottom, the switches required are:
#Skype4Business parameters
[switch]$Skypeenable,
[string]$SfBFEServer,
[string]$SfBPool,

  • SfBFEServer is the remote machine we will connect to to execute the commands.
  • SfBPool is the pool where the users will be homed on. If not specified, the script will try to be smart and do a DNS lookup for the _sipinternaltls._tcp record

The functions required are:
Function EnableUsersForSkype
{
 param (
  [string]$UserOUPath = $OUPath,
  [string]$Pool=$SfBPool,
  [string]$SipDomain=$UPNSuffix
 )
 

 ConnectToSfB
 if ($SipDomain -like '')
 {
  $SipDomain=(Get-CSSipDomain | where-object {$_.IsDefault -eq $true}).name
 }
    #Get users on OUPath with email accounts, and not enabled for S4B 
    $User=$null
 $UserList=Get-ADUser -Server $DomainController -Filter * -SearchBase $UserOUPath -properties mail, msRTCSIP-DeploymentLocator,msRTCSIP-UserEnabled | where {$_.'msRTCSIP-UserEnabled' -eq $null}
    $UserCount=$UserList.count
    $i=1
 foreach ($User in $userlist)
 {
  $userEmail=$User.mail
        if ($userEmail -like "*")
        {
   if ($User.'msRTCSIP-DeploymentLocator' -eq "SRV:")
   {
   #User is already enabled for S4B
   }
   elseif ($User.'msRTCSIP-UserEnabled' -eq $null)
   {
    #Enable user for S4B
    try
    {
                    Write-Log -ConsoleOutput -Message "Enabling user [$($i)/$($UserCount)], $($user.mail) for SfB" -LogFile $Logfile -LogLevel INFO
     Enable-CsUser -identity $user.userprincipalname -RegistrarPool $Pool -SipAddressType EmailAddress -SipDomain $SIPDomain -confirm:$false -DomainController $DomainController -ErrorAction Stop
     $user.'msRTCSIP-UserEnabled'=$true
     $User.'msRTCSIP-DeploymentLocator'="SRV:"
    }
    catch
                {
                    $ExceptionMessage = "Cannot enable user [$($i)/$($UserCount)], $($user.mail) for SfB, error $($_.Exception.Message)"
                    Write-Log -ConsoleOutput -Message $($ExceptionMessage) -LogFile $Logfile -LogLevel ERROR

                }
   }
  else
  {
            #User has no email account, so dont enable  
  }
        }
        $i++
 }

}

function ConnectToSfB
{
 # Connect to Skype4Business server
 try
 {
  $SessionInfo = Get-PSSession
  if ($SessionInfo.ConfigurationName -match "Microsoft.Powershell" -and $SessionInfo.ComputerName -match $SfBFEServer)
  {
   Write-Log -ConsoleOutput -Message "You are already connected to a SfB Powershell instance." -LogLevel INFO -LogFile $Logfile
  }
  else
  {
   Write-Log -ConsoleOutput -Message "Connecting to $($SfBFEServer)..." -LogFile $Logfile -LogLevel INFO
   $SkypeSession = New-PSSession -ConnectionURI "https://${SfBFEServer}/OcsPowershell" -credential $SkypeCredentials
   Import-PsSession $SkypeSession -CommandName Enable-CsUser, Get-CsUser, get-cssipdomain -AllowClobber -verbose:$false | Out-Null
  }
 }
 catch { Write-Log -Message "Cannot connect to Powershell Server $($SfBFEServer)." -LogFile $Logfile -LogLevel ERROR -ConsoleOutput; Break }
} # End Function ConnectToSfB

The checking and function calls are (just after the automatic domain controller detection around line 1840):
#If skype actions to be performed, get skype admin credentials
if ($SkypeEnable){
    $SkypeCredentials=$Host.ui.PromptForCredential("Skype Credentials","Please enter credentials to enable Skype users",$env:USERDOMAIN + "\" + $env:USERNAME,"SkypeForBusiness")
    if (!($SfBFEServer.contains(".")))
    {
        # requires FQDN so append DNS domain
        $SfBFEServer = $SfBFEServer,$Domain -join "."
    }
    Write-Log -ConsoleOutput -Message "Skype for Business: Using Front end server $($SfBFEServer)" -LogFile $Logfile -LogLevel INFO

    if (!($SfBPool))
    {
    # No Pool passed. lookup from dns (lyncdiscover record)
        $DNSRecord= "_sipinternaltls._tcp."+$Domain
        $SfBPool=(Resolve-DNSName $DNSRecord -type SRV).nametarget
        Write-Log -ConsoleOutput -Message "Skype for Business: No pool specified, resolving through DNS" -LogFile $Logfile -LogLevel INFO
    }
    if (!($SfBPool.contains(".")))
    {
        # requires FQDN so append DNS domain
        $SfBPool = $SfBPool,$Domain -join "."
    } 
    Write-Log -ConsoleOutput -Message "Skype for Business: Using Pool $($SfBPool)" -LogFile $Logfile -LogLevel INFO


}
$stopwatch=[system.diagnostics.stopwatch]::startNew()
$SectionSW=[system.diagnostics.stopwatch]::startNew()
Around line 1815 after the groups are created and mail enabled
If ($SkypeEnable -and $SfBFEServer)
{
    $SectionSW.Restart()
    EnableUsersForSkype -UserOUPath $OUPath -Pool $SfBPool -SipDomain $UPNSuffix
    $SectionSW.Stop()
    Write-Log -LogFile $Logfile -Message "Lab checkpoint:`tSkype enabled`t$($SectionSW.elapsed.minutes)m:$($SectionSW.elapsed.seconds)s:$($SectionSW.elapsed.Milliseconds)ms" -LogLevel SUCCESS -ConsoleOutput
}
And finally at the end, close the stopwatch and display the time it took:
$stopwatch.stop()
Write-Log -LogFile $Logfile -Message "Lab took $($stopwatch.elapsed.minutes)m:$($stopwatch.elapsed.seconds)s:$($stopwatch.elapsed.Milliseconds)ms to build" -LogLevel SUCCESS -ConsoleOutput

And finally an example of the command line:
.EXAMPLE
.\Create-LabUsers.ps1 -Count 100 -Company "PGR2, Inc." -OUPath "OU=PGR2 Users,DC=homelab,DC=domain,DC=com" -CreateGroups -InflateMailboxes -CreateMailboxes -ExchangeServer exch16-01 -SkypeFEServer sfb15-fe01

Create 100 users, create groups, enable all users in OU for email, add content to mailboxes and enable for Skype for Business


Comments

Popular posts from this blog

Skype Online and MCOValidationError

SCCM 2012 R2 - Offline servicing error

Polycom provisioning - and Zoom!