Pages

Thursday, March 28, 2013

PowerShell Script to Create a User, Mailbox Enable the Account, and Enable the Account for Lync

I have created a handy script that will create a user account, mailbox enable the account, enable the account for Lync, enable the Lync account for Enterprise Voice, and finally UM enable the account from a local computer without having to log onto the Exchange or Lync servers. This script basically creates the user account creating the Display Name as FirstName Last Name, UPN as FirstInitialLastName@InternalDomain, Alias as FirstInitial.LastName, SIP URI as the same as the e-mail address, and sets the LineURI in the format of tel:+13335551234;ext=1234.

This script is pieced together from other scripts I had so it may not be the most efficient. I will do some more testing and update the script once I get it optimized as well as creating one that will allow you to use a .csv file to mass enable users.

# Static Entries
$ExchangeServer = "ExchangeServer.domain.com"
$LyncServer = "LyncServer.domain.com"
$Registrar = "LyncServer.domain.com"
$dialplan = "Registrar:LyncServer.domain.com"
$intdomain = "domain.com"
$sipdomain = "SIPDomain.com"
$company = "Company Name"
$mbdb = "Mailbox Database Name"
$umpolicy = "Unified Messaging Policy Name"

# Import session information
$user = Get-Credential
$ExchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://$ExchangeServer/powershell -Credential $user
Import-PSSession $ExchSession
$LyncSession = New-PSSession -ConnectionURI https://$LyncServer/ocspowershell -Credential $user
Import-PSSession $LyncSession

# Data Entry section
$fname = Read-Host "First Name"
$finit = Read-Host "First Initial"
$lname = Read-Host "Last Name"
$password = Read-Host "Password"
$ou = Read-Host "AD OU account will be created in. In the format of OU=OUname,DC=Domain,DC=com"
$desc = Read-Host "Description"
$dept = Read-Host "Department"
$title = Read-Host "Title"
$manager = Read-Host "Manager"
$phone = Read-Host "Phone Number"
$ext = Read-Host "4 Digit Extension"

# Create user and enable mailbox
"Creating and enabling " + $fname + " " + $lname + " for AD and Exchange 2010"
$pwd = convertto-securestring $password -asplaintext -force
$name = $fname + " " + $lname
$upn = $finit + $lname + "@" + $intdomain
$alias = $finit + "." + $lname
$sam = $finit + $lname
New-Mailbox -name $name -userprincipalname $upn -Alias $alias -OrganizationalUnit $ou -SamAccountName $sam -FirstName $fname -Initials '' -LastName $lname -Password $pwd -ResetPasswordOnNextLogon $true -Database $mbdb 

# Pause for 30 seconds for AD 
write-host "Pausing for 30 seconds for AD Changes"
Start-Sleep -s 30 

# Set user properties
"Configuring AD settings for " + $fname + " " + $lname
Get-Mailbox -identity $name | Set-User -Company $company -Department $dept -Title $title -Manager $manager -Phone $phone 

# Pause 10 for AD changes
write-host "Pausing 10 Seconds for AD Changes"
Start-Sleep -s 10


# Enable for lync and configure settings
"Enabling " + $fname + " " + $lname + " for Lync 2010"
$lineuri = "tel:+1" + $phone + ";ext=" + $ext
Get-mailbox -identity $name | Enable-csuser -registrarpool $Registrar -sipaddresstype EmailAddress -sipdomain $sipdomain -enterprisevoiceenabled $True -lineuri $lineuri

# Pause 10 for Lync changes
write-host "Pausing 10 Seconds for Lync Changes"
Start-Sleep -s 10

# Enable For Unified Messaging
"Configuring " + $fname + " " + $lname + " for Unified Messaging"
$sipuri = $fname + "." + $lname + "@" + $sipdomain
Get-Mailbox -identity $name | Enable-UMMailbox -PinExpired $true -UMMailboxPolicy $umpolicy -Extensions $ext -SIPResourceIdentifier $sipuri

I will walk you thru what this script does...

The first commented area (# Static Entries) will be the entries that will be consistent thru the account creation and configuration process. The $ExchangeServer and $LyncServer variables are the servers that you are going to be connecting to via the PowerShell remote connections. The $intdomain and $sipdomain variables are in case your internal domain is different than your SIP domain.

The second commented area (# Import session information) is where it allows you to run this script from your computer instead of logging into the Exchange or Lync servers.

The third commented area (# Data Entry section) is where you will manually type in the requested information. Be careful with your entries because you will have to go back and manually fix anything that may be misspelled after the script has run.

The fourth commented area (# Create user and enable mailbox) is where it will create the user account and enable the mailbox.

The pauses in the script are to allow ample time for AD to replicate prior to making the additional changes. Depending on how large your AD environment is, you may need to add more time to the pauses to allow for AD replication to occur.

If you have any questions or need the script customized, let me know.

I will update the post later with a script that will use a .csv file to mass enable users and allow you to assign them to different MBX databases, Lync pools, Policies, etc.

Use the script at your own risk. There might be typos in the script so test first before running it in production.



4 comments:

  1. Have you tested this script yet?

    ReplyDelete
  2. How do you enable remote mailboxes for multiple users in AD? Do you have a Powershell script which gets the input in .CSV format and setup remote mailboxes ?
    Note : we have a hybrid deployment, where user accounts are setup in on-premise AD and corresponding mailboxes in Office 365

    ReplyDelete
    Replies
    1. Do you want to just enable remote mailboxes for multiple users in AD? This script creates the users and enables them for everything else. I can publish a script that will enable users mailboxes only if that is what you need.

      Delete