Solved

VB.NET - Running Exchange Management Shell commands

Posted on 2016-08-02
2
120 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
In this video we show how to create an email address policy 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 Mail Flow…

726 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