#"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
#"!!!!!!! THIS IS NOT A MICROSOFT SUPPORTED SCRIPT. !!!!!!!!"
#"!!!!!!! TEST IN A LAB FOR DESIRED OUTCOME !!!!!!!!"
#"!!!!!!! !!!!!!!!!!"
#"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# Changes required before running script:
# Change Locations are mentioned in BOLD. If you have any other OU for HOSTING apart from Customers then you would need to search the OU and then perform the neccesary changes searching keyword companies.
# - Change the DC name in Step No. 1.
# - Change the domain and OU structure to match existing organization in step 6.
# - change the database location to the desired database in the new-mailbox cmdlet. You could also leave this set to one database and then move the mailboxes to the desired mailbox store once they are created.
# - change domain and OU information in step 11
# Once the changes have been made, comment out this line and the ones above using "#" at the beginning of the line.
# Checks to see if samAccountName is a dupe and creates appropriate one
function GetAvailableAccountName([string] $inputName)
{
$returnValue = $null
$SamAccountName = $inputName
$SamAccountIndex = 0
do
{
$tempUser = $null;
# Special case as we don't want to add the index to the first query
if($samAccountIndex -eq 0)
{
$potentialName = $samAccountName
}
else
{
$potentialName = [string]::Format("{0}{1}", $samAccountName, $samAccountIndex)
}
$samUser = Get-User -Filter {SamAccountName -eq $potentialName} -ErrorAction "SilentlyContinue"
$mbxUser = Get-Mailbox -Filter {Alias -eq $potentialName} -ErrorAction "SilentlyContinue"
$isUnique = ($samUser -eq $null) -and ($mbxUser -eq $null)
if(!($isUnique))
{
# Increment the index
$samAccountIndex++;
}
}while(!($isUnique))
if($samAccountIndex -eq 0)
{
$returnValue = $samAccountName
}
else
{
$returnValue = [string]::Format("{0}{1}", $samAccountName, $samAccountIndex)
}
$returnValue
}
#check for "Companies" OU
#----------------------------
#----------------------------
$eaSave = $ErrorActionPreference
$ErrorActionPreference = "stop"
#$ErrorActionPreference = "SilentlyContinue"
"1 of 14"
$DC = "dc01"
"Using DC - $DC to create the mailbox"
$context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext([System.DirectoryServices.ActiveDirectory.DirectoryContextType]::DirectoryServer, $DC)
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($context)
$root = $domain.psbase.GetDirectoryEntry()
# check if the 'Companies' ou exists
$orgs = $null
$orgs = $root.psbase.Children.psbase.Find("OU=Companies")
if ($orgs -eq $null)
{
" "
write-host "Cannot find an OU named 'Companies'"; exit
" "
}
#-----------------------------
#-----------------------------
" "
"2 of 14"
"Enter User Company Name:"
$UserCompany = [Console]::ReadLine().Trim()
" "
#check for this companies OU
#--------------------------------
#--------------------------------
# find the org itself
$newOrg = $null
$newOrg = $orgs.psbase.Children.psbase.Find("ou="+$userCompany)
if ($newOrg -eq $null)
{
" "
write-host "Cannot find an OU named" $userCompany
" "
exit
}
#---------------------------------
#---------------------------------
"3 of 14"
"Enter User First Name:"
$UserFirst = [Console]::ReadLine()
$UserFirst = $UserFirst.Trim()
" "
"4 of 14"
"Enter User Last Name:"
$UserLast = [Console]::ReadLine()
$UserLast = $UserLast.Trim()
" "
"5 of 14"
do
{
"Enter User UPN:"
$UserUPN = [Console]::ReadLine()
$userUPN = $UserUPN.Trim()
$tmpUser = Get-User -Filter {UserPrincipalName -eq $userUPN}
if($tmpUser -ne $null)
{
write-host "The upn already exists in the directory! Please enter a unique UPN"
}
}
while($tmpUser -ne $null)" "
"6 of 14, parsing UPN name"
" "
$useralias = $userUPN.split("@")[0]
$userdomain = $userUPN.split("@")[1]
$userFull = $UserFirst + " " + $UserLast
$UserOU = "CONTOSO.COM/COMPANIES/" + $UserCompany
$SamAccountName = $userAlias
"7 of 14, creating mailbox"
New-Mailbox -Name $UserFull -Alias $UserAlias -OrganizationalUnit $UserOU -UserPrincipalName $UserUPN -SamAccountName $SamAccountName -FirstName $UserFirst -LastName $UserLast -ResetPasswordOnNextLogon $false -Database "Mailbox Database" -domaincontroller $DC
"8 of 14, setting customattribute1 to $userCompany"
set-mailbox "$userAlias" -customattribute1 "$userCompany" -offlineaddressbook "$userCompany OAB" -domaincontroller $DC -EmailAddressPolicyEnabled $false -emailAddresses ("SMTP:" + $userAlias + "@" + $userDomain) -windowsEmailAddress ($useralias + "@" + $userdomain)
"9 of 14, adding $userAlias to $usercompany security group"
add-distributiongroupmember "$userCompany SG" -member "$userAlias" -domaincontroller $DC
"10 of 14, updating Address List"
update-addresslist "$usercompany AL"
#=============================
# Mailbox Confirmation Section
#=============================
$a1 = get-mailbox $UserAlias
$a2 = $a1.name
$a3 = $a1.addresslistmembership
$a4 = $a1.OfflineAddressBook
$a5 = $a1.userprincipalname
$a6 = $a1.organizationalunit
$a7 = $a1.customattribute1
$a8 = $a1.distinguishedname
$a10 = $a1.emailaddresses
"11 of 14, Setting msExchQueryBaseDN"
#To Bind:
$user = ([ADSI]"LDAP://$a8").psbase;
#To Modify:
$user.Properties["msExchQueryBaseDN"].Value = "ou=$a7,ou=Companies,dc=contoso,dc=com";
$user.CommitChanges();
$a9 = $user.Properties["msExchQueryBaseDN"]
$a9b =$user.Properties["memberof"]
"12 of 14, Display Attributes"
" "
"Address List Membership: $a3"
"Alias: $userAlias"
"CustomAttribute1: $a7"
"DN: $a8"
"Email Addresses: $a10"
"Memberof: $a9b"
"mxExchQueryBaseDN: $a9"
"Offline Address Book: $a4"
"UPN: $a5"
"OU: $a6"
"User Name: $a2"
" "
"13 of 14, output to HTM file"
set-content -path c:\userconfirmation.htm -value "<html>
<title>User Confirmation: $userFull</title>
<head></head>
<body>
<h1>User Confirmation: <font color=green>$userfull</font></h1>
<table>
<tr><td><font size=2>The script will query the user just created and return a list of attributes that are needed to determine if the object has been provisioned corrrectly. Values will be listed in <font color=green>GREEN</font>. If the value is not found, it will not be listed. </td></tr>
</table>
<br>
<table>
<tr><td>Address List Membership:</td><td><font color=green>$a3</font></td></tr>
<tr><td>Alias:</td><td><font color=green>$useralias</font></td></tr>
<tr><td>CustomAttribute1:</td><td><font color=green>$a7</font></td></tr>
<tr><td>DistinguishedName:</td><td><font color=green>$a8</font></td></tr>
<tr><td>Email Addresses:</td><td><font color=green>$a10</font></td></tr>
<tr><td>MemberOf:</td><td><font color=green>$a9b</font></td></tr>
<tr><td>msExchQueryBaseDN:</td><td><font color=green>$a9</font></td></tr>
<tr><td>Offline Address Book:</td><td><font color=green>$a4</font></td></tr>
<tr><td>OU:</td><td><font color=green>$a6</font></td></tr>
<tr><td>UPN:</td><td><font color=green>$a5</font></td></tr>
</table>
</body>
</html>"
"14 of 14, open HTM file"
invoke-item c:\userconfirmation.htm