Solved

VB.NET - Running Exchange Management Shell commands

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now