Google

Welcome to Carpe Diem: Flaphead@Home Sign in | Join | Help

Carpe Diem: Flaphead.com

Seize the Day

News


  • Add to Technorati Favorites <script type="text/javascript" src="http://technorati.com/embed/3ni3q36ikc.js"> </script>
    This information is provided "AS IS" with no warranties, and confers no rights. Also some of the information contains my views and thoughts.
    <script src="http://widgets.technorati.com/t.js" type="text/javascript" charset="UTF-8"></script>

    Add Me! - Search Engine Optimization

    I heart FeedBurner

More PowerShell

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>
Name
OU1
OU2
OU3
</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 emails
Get-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,email
user-24,flaphead.local/ou1,Exchange2007\First Storage Group\Mailbox Database,user-24@flaphead.local
user-25,flaphead.local/ou2,Exchange2007\Second Storage Group\Mailbox Database,user-25@flaphead.local
</users.csv>

NB: Change the bits in bold to suit tyour AD

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!

Posted: 26 November 2006 14:10 by Paul Flaherty
Filed under: ,

Comments

Brettjo :: Microsoft Exchange Messaging said:

As I write this post I am currently waiting for my unattended install script of Exchange to complete,
# November 27, 2006 14:14
New Comments to this post are disabled