• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

VB.NET - Running Exchange Management Shell commands

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
fruitloopy
Asked:
fruitloopy
1 Solution
 
Peter HutchisonSenior Network Systems SpecialistCommented:
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
 
fruitloopyAuthor Commented:
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now