Solved

VB.NET - Running Exchange Management Shell commands

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
In this video we show how to create a User Mailbox 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 >> Mailb…
In this video we show how to create a mailbox database 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 Servers >> Data…

759 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

18 Experts available now in Live!

Get 1:1 Help Now