Saturday, 28 December 2013

Outlook VB Script - Detach attachments, Embed hyper link and Move email

The following VB script on outlook will move the email to a subfolder, detach the attachments, save them on the hard drive and embed the link on your receiving email. Tried and tested with MS Outlook 2007/2010 and worked perfectly fine but amend as necessary and use it at your own risk.
  • Task 1: Create an MS Access database and a table as described on database section
  • Task 2: copy and paste the code into Outlook Visual Basic Editor (Alt+F11), under Microsoft Office Outlook Objects, ThisOutlookSession.
  • Task 3: change the ConnectStr path to reflect the location of your Access Database and ensure the user has the necessary right on that folder.


Assumption
Inbox subfolder in Outlook has to be created beforehand.
Network share folders where the attachments are saved must be created and assigned necessary permissions beforehand.
Network share folders name (FsFolder) on the database table must end with “\”
e.g. if sharename is \\FileServer\Users\Someone, sharename must be entered as \\FileServer\Users\Someone\ in the FsFolder field.

Script Logic

Database: EmailContacts.accdb
Table: Email
ID
Auto Number
EmailAddress
Text
SubFolder
Text
FsFolder
Text



Option Explicit
Dim SFolder As String
Dim SaveLocation As String

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
'Check the sender email address and move the mail to corresponding folder
Dim InboundEmails
Dim Email
Dim i As Integer
Dim DestFolder As Outlook.MAPIFolder
Dim olAttachments As Outlook.Attachments
Dim olAttachFile As Outlook.Attachment
Dim AttachCount As Integer
Dim intIndex As Integer
Dim FilePath As String
Dim FileLink As String

SFolder = "Service"
InboundEmails = Split(EntryIDCollection, ",")

'loop all incoming email items
For i = 0 To UBound(InboundEmails)
Set Email = Application.Session.GetItemFromID(InboundEmails(i))
If CheckAddress(Email.SenderEmailAddress) Then
    ‘MsgBox "You Just have recieved a mail from " & Email.SenderEmailAddress & "." & Chr(13) & Chr(10) & "and he said : " & Email.Subject   
    AttachCount = Email.Attachments.Count
    If AttachCount > 0 Then
        ‘MsgBox "This email has " & AttachCount & " attachments."
        For intIndex = AttachCount To 1 Step -1
            Set olAttachFile = Email.Attachments.Item(intIndex)
            Dim ReNameFile As String
            ReNameFile = Time
            ReNameFile = Replace(ReNameFile, Chr(58), Chr(46))
            ReNameFile = ReNameFile & "." & olAttachFile.FileName
            FilePath = SaveLocation & ReNameFile
            olAttachFile.SaveAsFile FilePath           
            FileLink = FileLink & Chr(34) & FilePath & Chr(34) & Chr(32) & vbCrLf
            olAttachFile.Delete
        Next
        Email.Body = Email.Body & vbCrLf & vbCrLf & "Removed Attachments" & vbCrLf & vbCrLf & FileLink
        Email.Save
    End If
    Set DestFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders(SFolder)
    Email.Move DestFolder
End If
Next
Set Email = Nothing
Set InboundEmails = Nothing

End Sub


Function CheckAddress(ByVal eAddress As String) As Boolean

Dim Connect As New ADODB.Connection
Dim Cmd As New ADODB.Command
Dim RecSet As New ADODB.Recordset
Dim ConnectStr As String

ConnectStr = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _
            "DBQ=C:\Documents and Settings\N Awn\My Documents\EmailContacts.accdb;" & _
            "DefaultDir=C:\Documents and Settings\N Awn\My Documents;" & _
            "UID=admin;"

On Error Resume Next
Connect.ConnectionString = ConnectStr
Connect.Open
'Set RecSet = Connect.Execute("SELECT EmailAddress FROM Email")
Cmd.ActiveConnection = Connect
Cmd.CommandText = "SELECT EmailAddress, SubFolder, FsFolder FROM Email"
Set RecSet = Cmd.Execute

Dim Sender As String
Do While Not RecSet.EOF
    Sender = Trim(RecSet("EmailAddress"))
    If eAddress = Sender Then
        CheckAddress = True
        SFolder = Trim(RecSet("SubFolder"))
        SaveLocation = Trim(RecSet("FsFolder"))
        RecSet.Close
        Set RecSet = Nothing
        Connect.Close
        Connect.ConnectionString = ""
        Exit Function
    Else
    CheckAddress = False
    End If
    RecSet.MoveNext
Loop

RecSet.Close
Set RecSet = Nothing
Connect.Close
Connect.ConnectionString = ""

End Function
'coded By Naw Awn 24/02/2013. Amended on 31/03/2013



MS Exchange 2010 Calendar Management PowerShell

Exchange 2010 Calendar Management PowerShell

View permissions on a calendar

Get-MailboxFolderPermission –identity “user one:\Calendar”

For those who’d like to type less, instead of using –identity “user one:\Calendar”, you could omit the identity switch and use alias. Assuming user.one is an alias for “user one”, you could use:

Get-MailboxFolderPermission user.one:\Calendar

Give permissions to a new user

Add-MailboxFolderPermission –identity “user one:\Calendar” –User “user two” –AccessRights Editor

Change permissions on an existing user

Set-MailboxFolderPermission –identity “user one:\Calendar” –User “user two” –AccessRights Owner

For some reason, when someone who uses MS Office on a Mac wants to give delegate permission to a user, I have to give the Owner right other than anything else. A delegate normally gets the Editor right which is what a delegate needs but it messes up the original owner’s calendar when the delegate makes changes to the calendar items.

Remove permissions

Remove-MailboxFolderPermission user.one:\Calendar –User “user two” –Confirm:$false

Without the confirm:$false switch, you will have to type y to confirm removing.

Get items count on a calendar

Search-Mailbox –identity “user one” –SearchQuery kind:meetings -EstimateResultOnly

Delete all calendar items

Search-Mailbox –identity “user one” –SearchQuery kind:meetings -DeleteContent

Enable calendar repair assistance for a mailbox

Set-Mailbox –identity “user one” –CalendarRepairDisabled $false

Calendar Repair Assistance is managed on the server level but not on the individual mailbox basis. This implies that you have Server Management right. CRA does not run automatically by default. Therefore you will have to set it up.

Add-RoleGroupMember “Server Management” –Member “Admin User”

To configure CRA...

Set-MailboxServer –identity Exchange01 –CalendarRepairIntervalEndWindow 14
Set-MailboxServer –identity Exchange01 –CalendarRepairWorkCycle 7.00:00:00 –CalendarRepairWorkcycleCheckpoint 2.00:00:00
Set-MailboxServer –identity Exchange01 –CalendarRepairMissingItemFixdisabled $false

The above PowerShell cmdlets will trigger CRA to repair calendar on the mailbox server 14 days from the date it was set. Every 7 days the CRA will scan all mailboxes and flag to repair.   Every 2 days CRA will process those which are flagged to be repaired. To view what has been repaired you must use calendar repair logging feature. It is enabled by default. The default path for the log is inside the default exchange installation path\v14\Logging\Calendar Repair Assistant.

All in one go...
Set-MailboxServer –identity Exchange01 –CalendarRepairIntervalEndWindow 14 –CalendarRepairWorkCycle 7.00:00:00 –CalendarRepairWorkcycleCheckpoint 2.00:00:00 –CalendarRepairMissingItemFixdisabled $false

To configure CRA Logging...

Set-MailboxServer –identity Exchange01 –CalendarRepairLogEnabled $true
Set-MailboxServer –identity Exchange01 –CalendarRepairLogPath “D:\CRA LogFiles”
Set-MailboxServer –identity Exchange01 –CalendarRepairLogFileAgeLimit 14
Set-MailboxServer –identity Exchange01 –CalendarRepairLogDirectorySizeLimit 1GB

To troubleshoot individual Calendar issues...

Get-CalendarDiagnosticLog –identity “user one” –StartDate “12/12/2013” –EndDate “19/12/2013”
Get-CalendarDiagnosticLog –identity “user one” –Subject “Well Being Meeting”
Get-CalendarDiagnosticLog –identity “user one” –Subject “Well Being Meeting” | Get-CalendarDiagnosticAnalysis

By default, CalendarVersionStoreDisabled is set to $False. Hence, Calendar diagnostic logging is enabled.

Note: if you use subject as your Get-CalendarDiagnosticLog criteria, you must provide the exact string of the meeting subject.

Thursday, 12 December 2013

MS Exchange 2010 Mailbox Management PowerShell

Exchange 2010 Mailbox Management PowerShell



Disable

Disable-Mailbox -identity "user one"

Disable Archive

Disable-Mailbox -identity "user one" -Archive

Disable Remote Archive

Disable-Mailbox -identity "user one" -RemoteArchive

Remove

Remove-Mailbox -identity "user one"
Remove-Mailbox -identity "user one" -Permanent

Enable Unified Messaging...

To enable UM service on a mailbox, this has to be first set up.
PowerShell Command to use: Enable-UMMailbox

Manage Mobile Phone...

To get the details...
Get-ActiveSyncDeviceStatistics -Mailbox user.one | fl identity

To cancel the partnership...
Remove-ActiveSyncDevice -identity user.one_iphone

To perform a remote wipe...
Clear-ActiveSyncDevice -identity user.one_iphone

Clear-MobileDevice -identity user.one_iphone -NotificationEmailAddresses "admin@mydomain.com"

New Local Move Request...

New-MoveRequest -identity "user one" -TargetDatabase MBdb02
New-MoveRequest -identity "user two" -TargetDatabase MBdb02 -PrimaryOnly
New-MoveRequest -identity "user three" -ArchiveTargetDatabase MBdb03 -ArchiveOnly
New-MoveRequest -identity "user four" -TargetDatabase MBdb02 -ArchiveTargetDatabase MBdb03

New Remote Move Request...

$MyCred = Get-Credential
New-MoveRequest -identity user.one@mydomain.com -Remote -TargetDatabase MSMBdb02 -RemoteHostName "hostedcas01.mydomain.com" -RemoteCredential $MyCred -TargetDeliveryDomain "hostedmail.onmicrosoft.com"

Manage Send As Permission

Add-ADPermission –identity "user one" –user "user two" –ExtendedRights “Send As”

Manage Full Access Permission

Add-MailboxPermission -identity "user one" -User "user two" -AccessRights FullAccess

If you don't want to map the mailbox on the "user two" outlook, set the automapping value to false. By default it is set to true. Any mailbox you give full access permission will appear as another inbox under user own inbox in Outlook 2010 or later version.

Add-MailboxPermission -identity "user one" -User "user two" -AccessRights FullAccess -InheritanceType All -Automapping $false


Send Mail

When you click on ‘Send Mail’ button on the pop up Menu, the exchange server will look for the email client software such as Microsoft Outlook 2010 on the server. If there is one installed you will be able to use it. If not, use the following command to send an email.

Send-MailMessage -From "user.one@mydomain.com" -To "user.two@mydomain.com" -Cc "group.one@mydomain.com" -Bcc "user.three@mydomain.com" -Subject "Mailbox Statistics" -Attachments "C:\temp\Report.csv" -Priority High -Body "This is the Exchange Server Mailbox usage report I have promised."


Properties

Get-Mailbox and Get-mailboxStatistics can be used to get the necessary information about the mailbox in question. For example,

Get-Mailbox -identity "user one" | fl
Get-MailboxStatistics -identity "user one" | fl