다음을 통해 공유


Create a New User in Active Directory/Exchange 2013/Lync 2013

I created a script from multiple technet sources and websites that will allow an individual to work quicker and login to less servers and consoles.

This script Imports the Lync and Active Directory modules. It creates the user account in AD, then creates the users mailbox on the Datbase specified, it then goes and adds the user to the group you specify and finally enables the lync account for the user.

It also sets the users mobile and telephone number attributes in Active directory.

Requirements:

  • Exchange 2013 Management Tools
  • User Account that has access to AD, Exchange and Lync.
  • (If you dont want to install this you can enable the 2 lines of code to create a PSSession to Exchange)

The Read-Host command didn't work too well running it from the Exchange Management Shell or Windows PowerShell so I resorted to using the InputBoxDialog. I import an assembly to allow this function to work.

The powershell script below:

 

# Set variables and server names

$ExchangeServer="Exchange Server Name"

$LyncServer="Lync Server Name"

$Registrar="Lync pool"

$idomain="Domain.local"

$DC="FQDN Domain Controller"

$OU="Your OU"

$user=Get-Credential

# Import Exchange Session

#$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI https://$ExchangeServer/PowerShell -AllowRedirection -Authentication Basic -Credential $user

#Import-PSSession $ExchangeSession

#Import Lync Session

$LyncSession=New-PSSession-ConnectionURIhttps://$LyncServer/ocspowershell-Credential$user

Import-PSSession$LyncSession

#Import Active Directory

$Session=New-PSsession-Computername$DC-Credential$user

Invoke-Command-Command {Import-ModuleActiveDirectory}-Session$Session

Import-PSSession-Session$Session-ModuleActiveDirectory-PrefixRM-AllowClobber

# Show input box popup and return the value entered by the user.

functionRead-InputBoxDialog([string]$Message,[string]$WindowTitle,[string]$DefaultText)

{

Add-Type-AssemblyNameMicrosoft.VisualBasic

return[Microsoft.VisualBasic.Interaction]::InputBox($Message,$WindowTitle,$DefaultText)

}

$fname=Read-InputBoxDialog-Message"Please enter the Users First Name'"-WindowTitle"First Name"

$lname=Read-InputBoxDialog-Message"Please enter the Users Last Name'"-WindowTitle"Last Name"

$username=Read-InputBoxDialog-Message"Please enter the UserName'"-WindowTitle"User Name"

$pwd=Read-InputBoxDialog-Message"Please enter the Users Password'"-WindowTitle"Password"

$desc=Read-InputBoxDialog-Message"Please enter a Description'"-WindowTitle"Description"

$dept=Read-InputBoxDialog-Message"Please enter the Department Name'"-WindowTitle"Department Name"

$title=Read-InputBoxDialog-Message"Please enter the Users Job Title'"-WindowTitle"Job Title"

$mbdb=Read-InputBoxDialog-Message"Please enter the Mailbox Database'"-WindowTitle"Mailbox Database Name"

$group=Read-InputBoxDialog-Message"Please enter the Active Directory Group'"-WindowTitle"Active Directory Group"

$officephone=Read-InputBoxDialog-Message"Please enter the Phone Number'"-WindowTitle"Telephone Number"

$mobile=Read-InputBoxDialog-Message"Please enter the Users Mobile Phone Number'"-WindowTitle"Mobile Phone"

$manager=Read-InputBoxDialog-Message"Please enter the Users direct Manager'"-WindowTitle"Manager"

$company=Read-InputBoxDialog-Message"Please enter the Company Name'"-WindowTitle"Company Name"

$password=

$name=$fname+" "+$lname

$upn=$username+"@"+$idomain

$alias=$username

$sam=$username

#Create user mailbox

New-Mailbox-name$name-userprincipalname$upn-Alias$alias-OrganizationalUnit$OU-SamAccountName$sam-FirstName$fname-LastName$lname-Password$password-Database$mbdb

write-host"Wait 30 seconds for Active Directory Changes"

Start-Sleep-s30

#Add the user to the group specified

add-RMAdGroupMember-Identity$group-Member$sam

Set-RMADUser-identity$sam-PasswordNeverExpires$False-Description$desc-Department$dept-Title$title

#Add telephone number to the user account

Set-ADUser-Identity$sam-OfficePhone$officephone

Set-ADUser-Identity$sam-Mobile$mobile

Set-ADUser-Identity$sam-Manager$manager

Set-ADUser-Identity$sam-Company$company

write-host"Wait 10 Seconds for Active Directory Changes"

Start-Sleep-s10

#Enable Lync account for user

Get-mailbox-identity$name|Enable-csuser-registrarpool$Registrar-sipaddresstypeSamAccountName-SipDomain"Domain.local"