Myself and Brett played with some coding last week and I wanted to share with you what we done.
Brett’s original script is to deploy an Exchange 2007 server. It basically runs setup, installs exchange and then imports data from a number of CSVs and creates StorageGroups, Databases and address lists.
So I was also thinking that it would be cool to create some users from a script. Oh and to why not create some OU’s to put them in. Finally why not send an email to activate the mailbox too!
So, to create the OU’s we do this:
<NEW-manyOUs.ps1>$OUs = Import-csv ou.csv$objDomain = [ADSI]"LDAP://localhost:389/dc=flaphead,dc=local"foreach ($ou in $OUs){$objOU = $objDomain.Create("organizationalUnit", "ou=" + $ou.name)$objOU.SetInfo()}</NEW-manyOUs.ps1>
<ou.csv>NameOU1OU2OU3</ou.csv>
NB: Change the bits in bold to suit tyour AD
Okay now for the users
<New-ManyUsers># Need a secure password, so ask for one like Pa55word$password = Read-Host "Enter Password.." -AsSecureString
# Import the users from users.csv$AllUsers = Import-Csv users.csv
# Set the default ReceiveConnector to allow Anonymous emailsGet-ReceiveConnector | where {$_.Identity -like "*Default*"} | Set-ReceiveConnector -identity {$_.Identity} -PermissionGroups anonymoususers
foreach ($user in $AllUsers){ # Create a new mailbox enabled user using the information fromm the csv file new-mailbox -database $user.database -password $password -UserPrincipalName $user.Email -Name $user.name -OrganizationalUnit $user.OU .\send-mail.ps1 -To $user.Email -From administrator@flaphead.local -Subject "Welcome to Exchange 2007" -Server exchange2007}</New-ManyUsers>
<users.csv>name,ou,database,emailuser-24,flaphead.local/ou1,Exchange2007\First Storage Group\Mailbox Database,user-24@flaphead.localuser-25,flaphead.local/ou2,Exchange2007\Second Storage Group\Mailbox Database,user-25@flaphead.local</users.csv>
So finally, how to send an email. It took me a while to work this one out, and plenty of google time .. I mean live.com ;-)
<send-mail.ps1>#Send-Mail.ps1#Created by Paul Flaherty (blogs.flaphead.com)
$server =""$to = ""$from = ""$body = "This message as sent to use using send-mail.ps1 at " + "{0:G}" -F (GET-DATE)$subject = "Sent using send-mail.ps1"$sendmail = $True$verbose = $false$message = New-Object System.Net.Mail.MailMessage
#Make the assumption that the arguments are in pairs
for($i = 0; $i -le $args.Length; $i++){ switch ($args[$i]) { "-server" { $server = $args[$i+1] $i++ } "-to" { $to = $args[$i+1] $message.TO.add($to) $i++ } "-from" { $from = $args[$i+1] $message.from = $from $i++ } "-subject" { $subject = $args[$i+1] $message.subject = $subject $i++ }
"-body" { $body = $args[$i+1] $message.Body = $body $i++ }
"-attachment" { $attachment = $attachment + $args[$i+1] + "; " $message.Attachments.Add($args[$i+1]) $i++ }
"-verbose" { $verbose = $true }
}
IF ($server.length -eq 0) {$sendmail = $false}IF ($to.length -eq 0) {$sendmail = $false}IF ($from.length -eq 0) {$sendmail = $false}
IF ($verbose){"No Args: " + $args.length"Servername: " + $server"To: " + $to"From: " + $from"subject: " + $subject"body: " + $body"Attachment: " + $attachment}
IF ($sendmail){
$smtpclient = [System.Net.Mail.SmtpClient]("")$smtpclient.Host = $server$smtpclient.Send($message) }ELSE{
'Syntax:send-mail.ps1 -server exchange2007 -from administrator@flaphead.local -TO user-11@flaphead.local -subject "Welcome" -body "Welcome to your new mailbox on Exchange 2007" -verbose
-server <Server name>-from <User mail is from>-to <Mail recipient>-subject <Message Subject>-body <Message Body>-attachment <Filelocation of attachment>-verbose
NB: Make sure your Exchange 2007 default recieve connectors set to allow anonymoususer access
Get-ReceiveConnector | where {$_.Identity -like "*Default*"} | Set-ReceiveConnector -identity {$_.Identity} -PermissionGroups anonymoususers'}</send-mail.ps1>
enjoy!