Showing posts with label exchange. Show all posts
Showing posts with label exchange. Show all posts

Wednesday, June 25, 2014

Everyday Powershell - Part 24 - Send Email With Inline Image

Ever wanted to include inline images with an email you are sending out from a script?

We came across the requirement during our Exchange 2013 Migration.

This was ridiculously easy to achieve in Powershell 4.
Send-MailMessage -to someuser@someserver.com -From someotheruser@someserver.com -SmtpServer somemailserver -Subject "Test" -BodyAsHtml "TEST <br /><img src="attachement.png" />" -Attachments "C:\Scripts\attachement.png"

This is how we used to do it!
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$smtpServer = "somemailserver"
$att = new-object Net.Mail.Attachment("c:\scripts\attachment.png")
$att.ContentId = "att"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$body = "Some Text<br />
<img src="cid:att" />"

$msg.From = "someuser@manly.nsw.gov.au"
$msg.To.Add("someotheruser@manly.nsw.gov.au")
$msg.Subject = "Some Subject" + (get-date -format yyyyMMdd)
$msg.Body = $body
$msg.IsBodyHTML = $true
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

UPDATE - The HTML included in the script bugged out... Will fix shortly.

UPDATE 2 - reader sivaramsharma asked if send-mailmessage could handle multiple attachments, if this interests you too head over to Part 31 where we give a useful example.

Wednesday, May 21, 2014

Everyday Powershell - Part 22 - Determine which mailboxes a user has fullaccess to

Had cause this week to figure out to which mailboxes a particular user had been assigned Full Access  permission.

Seemed a bit daunting, and would have taken us ages to audit just using the GUI. But it was actually very quick to get going in Powershell.
Add-PSSnapin *exchange*

$user = "TESTUSER"

$mailboxes = get-mailbox

foreach ($mailbox in $mailboxes)
{
    $mailboxpermission = Get-Mailboxpermission $mailbox
    foreach ($permission in $mailboxpermission)
    {
        $permission | where {$_.Accessrights -Contains "FullAccess" -and $_.user -like "*$user*"} | select Identity
    }
}

This was a cracking use of Powershell right in the nick of time and made us seem really professional to our big boss. Really can't recommend enough getting some powershell tools into your frequently used toolbelt.

Once you start using it, it's amazing the things you start to dream up. Things you wouldn't have even thought to do before learning the power of the shell!

Wednesday, April 16, 2014

Everyday Powershell - Part 19 - Migrate Transport Rules from Exchange 2007 to 2013

Today's script is a quick and dirty fix for a project we're in the middle of. Not really a script either, but it's the thought that counts.


#on a computer with exchange 2007 management tools
Add-PSSnapin *exchange*
export-transportrulecollection c:\scripts\exch2007transportrules.xml

#on a computer with exchange 2013 management tools
Add-PSSnapin *exchange*
[Byte[]]$Data = Get-Content -Path '\\someserver\c$\Scripts\exch2007transportrules.xml' -Encoding Byte -ReadCount 0
Import-TransportRuleCollection -FileData $Data
Export transport rules from Exchange 2007 and import them to 2013. 

To think our System Admin was going to copy these over by hand!

Full credit to Microsoft for this one it is all from http://support.microsoft.com/kb/2846555