Solved

VB.NET - Running Exchange Management Shell commands

Posted on 2016-08-02
2
100 Views
Last Modified: 2016-08-08
Hi All

I am struggling with finding out how I can use a VB.NET app to run an Exchange Management Shell command.
Specifically this will be used to aid in message tracking.

Typically I would remote onto the Exchange server (2010), open Exchange Management Shell and type something similar to the following:

get-messagetrackinglog -start "6/1/2016 6:00am" -end "8/1/2016 11:00am" -sender "someone@somewhere.co.uk" | select sender, {$_.Recipients}, timestamp, messagesubject, totalbytes | export-csv c:\temp\report.csv

Open in new window

What I will do is have date/time pickers, text boxes, etc for the end user to populate and that will be used to send the command to Exchange

I have searched around on the internet but all mildly useful code is in C# and I only know VB.NET.

Can someone help me to get this off the ground please?
0
Comment
Question by:fruitloopy
2 Comments
 
LVL 19

Accepted Solution

by:
Peter Hutchison earned 500 total points
ID: 41740227
I have written vb.net code to runExchange commands. If there something specific you need let me know.
To

Anyway something to get you started:
https://msdn.microsoft.com/en-us/library/bb332449.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-4
0
 
LVL 2

Author Comment

by:fruitloopy
ID: 41741896
Hi Peter

Thanks for that link, I'll have a go at that as soon as I can.
In the meantime I found some old code I have written and forgotten about that exports a users mailbox to a PST file but does this by first creating a Powershell file and running that.
Sub step_three()
        '#### STEP THREE: CREATE THE PST FILE ####
        Dim newfile As String = "C:\Temp\PSTScript_" & txtUser.Text & ".ps1"
        'File.Create(newfile)

        Dim write As New StreamWriter(newfile, False)
        write.WriteLine("$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://serv9/PowerShell/")
        write.WriteLine("Import-PSSession $Session")
        write.WriteLine("New-MailboxExportRequest -Mailbox " & txtUser.Text & " -filepath \\serv24\Archive_Users\" & txtUser.Text & "\" & txtUser.Text & ".pst")
        write.WriteLine("Remove-PSSession $Session")
        write.Close()
        Cursor = Cursors.WaitCursor
        Application.DoEvents()
        Try
            'Create the runspace.
            Using R As System.Management.Automation.Runspaces.Runspace = _
            System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()

                'Create the pipeline
                Using P As System.Management.Automation.Runspaces.Pipeline = R.CreatePipeline()

                    'Open the runspace.
                    R.Open()

                    'Create each command (in this case just one)...
                    Dim Cmd As New System.Management.Automation.Runspaces.Command(newfile, True)

                    '...and add it to the pipeline.
                    P.Commands.Add(Cmd)

                    'Execute the commands and get the response.
                    Dim Result As System.Collections.ObjectModel.Collection(Of  _
                    System.Management.Automation.PSObject) = P.Invoke()

                    'Close the runspace.
                    R.Close()


                End Using
            End Using
            System.Threading.Thread.Sleep(10000)
            If File.Exists("\\serv24\Archive_Users\" & txtUser.Text & "\" & txtUser.Text & ".pst") Then
                tick3.Visible = True
                step_four()
            Else
                Dim r = MessageBox.Show("The command to archive " & txtUser.Text & " has been sent but may still be queued at the Exchange server" _
                                          & vbCrLf & "Do you want to wait another 10 seconds?", "PST not yet created", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If r = Windows.Forms.DialogResult.Yes Then
                    System.Threading.Thread.Sleep(10000)
                    If File.Exists("\\serv24\Archive_Users\" & txtUser.Text & "\" & txtUser.Text & ".pst") Then
                        lblPSTArchived.Text = "Users PST file is being created"
                        tick3.Visible = True
                    End If
                ElseIf r = Windows.Forms.DialogResult.No Then
                    step_four()

                End If
            End If
            step_four()
            Cursor = Cursors.Default
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window


I'll try the code in the link you sent me first as it looks cleaner and more direct.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question