
<#This Script create room mailbox and set primary email 
(It Can also add the Created Room to a distribution List if the request want it restricted to a certain group of users)..
It takes an import CSV file named "C:\Scripts\input\Rooms.csv"

Created By Alberto Igbinedion
Thanks to Adam Beardwood
.....#>
####################################################################### Set Exchange Powershell Environment
$password = Read-Host "Enter password" -AsSecureString

####################################################################### Check that Exchange Snap-in is added if not add it
$snaps = Get-PSSnapin
foreach($snap in $snaps){if($snap.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"){$exflag = 1}}
if($exflag -ne 1){Add-PSSnapin -name Microsoft.Exchange.Management.PowerShell.E2010}

###################################/######################################### Imports Data From Spreadsheet
#$data = Import-Csv C:\Scripts\RoomsSMLTest.csv
#$data = Import-Csv C:\Scripts\RoomsYR.csv
$data = Import-Csv C:\Scripts\input\Rooms.csv

################################################################### Defines the variables with the Trim statement to remove White Spaces (.Trim())
    foreach ($row in $data) {
    $Alias = $row.Alias.Trim()
    $DisplayName = $row.DisplayName.Trim()
	$FirstName = $row.FirstName.Trim()
	$LastName = $row.LastName.Trim()

####################################################################### Load Exchange snap-in
#Add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
#Creates New mailboxes with the properties defined below          
    New-Mailbox -Domaincontroller AEC01A-DC03 -Alias "mbx_$Alias" -database AEC01-DB3 -UserPrincipalName "mbx_$($Alias)@aeroenginecontrols.com" -samaccountname "mbx_$Alias" -Name $DisplayName -FirstName $FirstName -LastName $LastName -OrganizationalUnit "aecuk.aec.local/Engine Control Systems/Resource Mailboxes" -Room
	
###################################################################### Set a delay so that mailbox creation is completed before adding to a distribution group	
	Do{
	try{ get-mailbox -Domaincontroller AEC01A-DC03 "mbx_$Alias" -erroraction stop 
	$mbx=$true} catch {} 
	} while ($mbx -ne $true)

######################################################################## Add Created mailbox to Distribution Group	
	#$Email = (Get-Mailbox mbx_$Alias).EmailAddresses
	#Add-DistributionGroupMember -Domaincontroller AEC01A-DC03 -identity "ShaftmoorLaneMeetingRooms" -Member "mbx_$Alias" 
	#Add-DistributionGroupMember -Domaincontroller AEC01A-DC03 -identity "YorkRoadMeetingRooms" -Member "mbx_$Alias"
	#Add-DistributionGroupMember -Domaincontroller AEC01A-DC03 -identity "DerbyMeetingRooms-BassettBlock" -Member "mbx_$Alias"
	
######################################################################## Set created mailbox email address policy to disabled	
	Set-mailbox -Domaincontroller AEC01A-DC03 -identity "mbx_$Alias" -EmailAddressPolicyEnabled $false
	#$Email.add("$($Alias)@controlsdata.com") 
	
################################################################## Set created mailbox Variables to allow primary SMTP to be created by using the splitting string method	
	$OldAddress = (Get-Mailbox -Domaincontroller AEC01A-DC03 -identity "mbx_$Alias").primarysmtpaddress.tostring().split("@")[0] 

################################################################### Sets Created mailbox as primary address 
	Set-Mailbox -Domaincontroller AEC01A-DC03 -identity "mbx_$Alias" -PrimarySmtpAddress "$($OldAddress)@controlsdata.com"


}


# Good To Go