# **************************************************************************
# This is a sample script for creating new server and mail enable process
#
# Author: Kutbettin Ekici
# **************************************************************************
# **************************************************************************
# This is first function which we have to use for new user data collection
function dataenter
{
Write-Host " "
Write-Host "New user (Required) information ... " -ForeGroundColor DarkGreen
Write-Host " "
# Required basic information for a sample user will collect by that way...
$FirstName = Read-Host "First Name"
$LastName = Read-Host "Last Name"
# Password need to collect as a secure string
$passWord = Read-Host "Password" -AsSecureString
# Global variable declaration in a function like that...
# By default variable scoping was local...
$global:Name = Read-Host "Logon name (it needs a unique value)"
$global:Displayname = $FirstName + " " + $LastName
$HomeMDB = Read-Host "Database path (HomeMDB) [For example; servername\StorageGroupName\DatabaseName] "
$alias = Read-Host "Alias name (it needs a unique value)"
$domain = Read-Host "What is the domain name for new user [For example; contoso.local] "
$UPN = $Name+"@"+$domain
Write-Host " "
Write-Host "Optional information for user... " -ForeGroundColor DarkGreen
Write-Host " "
# Optional data for new user
$OU = Read-Host "Organizational Unit location for new user [For example; /Contoso/Bim/Users/ ]"
if ($OU -eq "")
{
# Following line is not necessary but addressing static path will be fine…
$OU = $domain + "/" + "USERS"
}
# normally variable scope in a function by default is local, we can set it global with the following sample line...
# set-variable OU value -scope global
$SendSize = Read-Host "Mail send limit [For example; 1230KB, 2MB ]"
if ($SendSize -eq "")
{
$SendSize = "0KB"
}
$ReceiveSize = Read-Host "Mail receive limit [For example; 10000KB, 2MB ]"
if ($ReceiveSize -eq "")
{
$ReceiveSize = "0KB"
}
$global:Description = Read-Host "User Description [For example; Customer Account Manager]"
$global:title = Read-Host "Title : [For example; Account Manager]"
# Calling createUserMBX function
createUserMBX
}
function createUserMBX
{
Write-Host " "
Write-Host " User Information will be… : " -ForeGroundColor DarkGreen
Write-Host "Name : " $DisplayName -ForeGround Blue
Write-Host "Alias : " $Alias -ForeGround Blue
Write-Host "OU : " $OU -ForeGround Blue
Write-Host "UPN : " $UPN -ForeGround Blue
Write-Host "Logon Name : " $Name -ForeGround Blue
Write-Host "DB address : "$HomeMDB -ForeGround Blue
Write-Host "Send Limit : " $SendSize -ForeGround Blue
Write-Host "Receive Limit : " $ReceiveSize -ForeGround Blue
Write-Host " "
# ************************************************************************
$SecondQuestion = Read-Host "Do you want to create this user with the above information ? Yes (Y) / No: (N)"
if ($SecondQuestion -eq 'Y' )
{
# exception handling lines....
trap {
Write-Host " An error occured, please check the following detailed information and take an action for resolution. " -ForeGroundColor RED -BackGroundColor WHITE
Write-Host " "
break
}
# we are creating user by calling RUS API with the “new-mailbox” command...
New-Mailbox -Name $Name -Alias $Alias -OrganizationalUnit $OU -UserPrincipalName $UPN -SamAccountName $Name -FirstName $FirstName -LastName $LastName -Database $HomeMDB -ResetPasswordOnNextLogon $false -Password $passWord;
if (($SendSize -eq '0KB') -and ($ReceiveSize -eq '0KB'))
{
}
else
{
if ($SendSize -ne '0KB')
{
Set-Mailbox -identity $Alias -MaxSendSize $SendSize
}
if ($ReceiveSize -ne '0KB')
{
Set-Mailbox -identity $Alias -MaxReceiveSize $ReceiveSize
}
}
# This is a sample line for additional settings
# OWA enabled by default With the following line we can disable OWA for new users
# Set-CASMailbox -identity $Alias -OWAEnabled $false
#We are calling new user Ldap path for optional data entry
}
else
{
Write-Host " An error occurred in user creation phase...! " -ForeGround Blue
break
}
}
# **************************************************************************
function get-dn ($Name)
{
Write-Host " "
write-host "Searching" $DisplayName "named new user in the active directory..."
Write-Host " "
trap {
Write-Host " Problems occurred related AD connection or user creation. " -ForeGroundColor RED -BackGroundColor WHITE
Write-Host " Please check error message and take correct action. " -ForeGroundColor RED -BackGroundColor WHITE
Write-Host " "
break
}
# We are starting a powershell ADSI session...
$root = [ADSI]
# By using .NET directory service we are describe a searcher object for ADSI interface
$searcher = new-object DirectoryServices.DirectorySearcher($root)
#Setting up a name filter for user class
$searcher.filter = "(&(objectClass=user)(name= $Name))"
#Listing all possible results... (It's a unique value...)
$user = $searcher.findall()
if ($user.count -gt 1)
{
$count = 0
foreach($i in $user)
{
write-host $count ": " $i.path
$count = $count + 1
}
$selection = Read-Host "Please select item: "
# returning Ldap path back to the requested function...
return $user[$selection].path
}
else
{
return $user[0].path
}
}
function lastUserSetup
{
$global:LDAPpath = get-dn $Name
# another variable scope declaration way is: set-variable var -scope global
trap {
Write-Host " Probably script didn't complete update user: $DisplayName for it's additional attributes. " -ForeGroundColor RED -BackGroundColor WHITE
Write-Host " Please check the error message and take required action. " -ForeGroundColor RED -BackGroundColor WHITE
Write-Host " "
break
}
$NewUser = New-Object System.DirectoryServices.DirectoryEntry $LDAPpath
if ($Displayname -ne "")
{$NewUser.Put('displayName',$DisplayName)}
if ($Description -ne "")
{$NewUser.Put('description',$Description)}
if ($title -ne "")
{$NewUser.Put('title',$title)}
if (($Displayname -eq "") -and ($Description -eq "") -and ($title -eq ""))
{
Write-Host "There isn't any update ..."
}
else
{
Write-Host " "
#We are writing all attributes with the following line
$NewUser.SetInfo()}
Write-Host "Update operation completed successfully."
Write-Host " "
}
function results
{
$NewUser = New-Object System.DirectoryServices.DirectoryEntry $LDAPpath | Select-Object *
Write-Host "************************************************************************** " -ForeGroundColor DarkGREEN
Write-Host "********* User created or updated with the following information ********* " -ForeGroundColor DarkGREEN
Write-Host "E-mail Address : "
Write-Host $newUser.mail -ForeGroundColor DarkGreen
Write-Host " "
Write-Host "Server Path : "
write-host $newUser.msExchHomeServerName -ForeGroundColor DarkGreen
Write-Host " "
Write-Host "Database Path : "
write-host $newUser.homeMDB -ForeGroundColor DarkGreen
Write-Host " "
Write-Host "Display Name : "
write-Host $newUser.DisplayName -ForeGroundColor DarkGreen
Write-Host " "
Write-Host "Logon Name : "
write-host $newUser.sAMAccountName -ForeGroundColor DarkGreen
Write-Host " "
Write-Host "Title : "
Write-Host $newUser.title -ForeGroundColor DarkGreen
Write-Host " "
Write-Host "Description : "
write-host $newUser.description -ForeGroundColor DarkGreen
Write-Host " "
Write-Host " ************************************************************************** " -ForeGroundColor DarkGREEN
}
# This is the start point of powershell script
# Just giving a sample for Read-Host and if statement....
$FirstQuestion = Read-Host " What do you need ? For a new user plase press:Y / For updating a user please press: U "
# if statement sample...
if ($FirstQuestion -eq 'Y' )
{
# We are calling dataenter function by the following line
dataenter
lastUserSetup
results
break
}
else
{
Write-Host " Update function is not implemented yet. " -ForeGroundColor GREEN
break
}