远程PowerShell Exchange:使用Windows窗体输入并传递到远程Exchange会话

时间:2018-03-01 19:32:19

标签: winforms powershell exchange-server powershell-remoting

第一篇文章,对任何格式问题道歉,无法找到插入PowerShell代码的直接方法,并且JavaScript语言插入内容看起来最接近。

我创建了一个工具,可以将远程PowerShell连接到环境中的各种Exchange服务器。此工具使用用于GUI的Windows窗体,并具有用户选择和输入的各种选项。我有GUI控件所有正常工作,并能够毫无问题地连接到远程Exchange服务器。 Exchange环境是2010年,2013年的本地和O365。

我正在尝试利用更多的用户输入来使远程会话尽可能不干涉,并且我遇到了在成功连接后将自定义函数或命令传递到远程会话的问题。

所需行为:将文本框的内容用作搜索传输服务器上的邮件跟踪日志的命令的一部分。在Connect-Exchange函数中,我有一个执行搜索的Message-Subject函数。

我已经四处搜索并尝试了各种方法,但似乎无法使它们中的任何一种以所需的方式工作。我正在寻找建立连接和建立会话,然后运行命令。我不确定这是否可能,所以我创建了一个函数,希望在会话建立后自动运行,然后让用户只需键入函数调用。 我认为问题在于导线-PSSession和Invoke-Command的第169-171行。

最终,我希望GUI能够捕获MessageTracking搜索的所有属性,并且命令会自动运行而无需任何进一步的用户输入。

可以这样做吗?

另外,我设置它的方式可能不是最实用的,所以如果有更好的方法,我愿意接受建议。

由于



##
Connect Remote Exchange PowerShell


## Function Input Box-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -

function Connect - Exchange {

  Add - Type - AssemblyName System.Windows.Forms
  Add - Type - AssemblyName System.Drawing



  ## Form Code-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --


  ##Form details

  $form = New - Object System.Windows.Forms.Form
  $form.Text = "Connect to Remote Exchange"
  $form.DataBindings.DefaultDataSourceUpdateMode = 0
  $form.Size = New - Object System.Drawing.Size(500, 330)
  $form.StartPosition = "CenterScreen"
  $form.TopMost = $True
  $form.AutoSize = $True
  $form.FormBorderStyle = 'Fixed3D'
  $form.AutoSizeMode = "GrowAndShrink"


  ##
  Radio Buttons-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --


  ##Group Box
  for Radio Buttons

  $GroupBox = New - Object System.Windows.Forms.GroupBox
  $GroupBox.Location = New - Object System.Drawing.Point(40, 30)
  $GroupBox.size = New - Object System.Drawing.Size(400, 180)
  $GroupBox.Text = 'Select the desired domain and press "OK" to connect:'

  ##
  Radio Button 1

  $radioButton1 = New - Object System.Windows.Forms.RadioButton
  $radioButton1.Text = "Exchange 2010"
  $radioButton1.Location = New - Object System.Drawing.Point(20, 40)
  $radioButton1.Size = New - Object System.Drawing.Size(350, 20)
  $radioButton1.DataBindings.DefaultDataSourceUpdateMode = 0
  $radioButton1.UseVisualStyleBackColor = $True
  $radioButton1.TabIndex = 0

  ## Radio Button 2

  $radioButton2 = New - Object System.Windows.Forms.RadioButton
  $radioButton2.Text = "Exchange 2010"
  $radioButton2.Location = New - Object System.Drawing.Point(20, 70)
  $radioButton2.Size = New - Object System.Drawing.Size(350, 20)
  $radioButton2.DataBindings.DefaultDataSourceUpdateMode = 0
  $radioButton2.UseVisualStyleBackColor = $True
  $radioButton2.TabIndex = 1

  ## Radio Button 3

  $radioButton3 = New - Object System.Windows.Forms.RadioButton
  $radioButton3.Text = "Exhcange 2013"
  $radioButton3.Location = New - Object System.Drawing.Point(20, 100)
  $radioButton3.Size = New - Object System.Drawing.Size(350, 20)
  $radioButton3.DataBindings.DefaultDataSourceUpdateMode = 0
  $radioButton3.UseVisualStyleBackColor = $True
  $radioButton3.TabIndex = 2

  ## Radio Button 4

  $radioButton4 = New - Object System.Windows.Forms.RadioButton
  $radioButton4.Text = "Office 365"
  $radioButton4.Location = New - Object System.Drawing.Point(20, 130)
  $radioButton4.Size = New - Object System.Drawing.Size(350, 20)
  $radioButton4.DataBindings.DefaultDataSourceUpdateMode = 0
  $radioButton4.UseVisualStyleBackColor = $True
  $radioButton4.TabIndex = 3

  ## Text Box

  $textBox = New - Object System.Windows.Forms.TextBox
  $textBox.Location = New - Object System.Drawing.Point(515, 40)
  $textBox.Size = New - Object System.Drawing.Size(300)
  $textBox.WordWrap = $True
  $textLabel1 = New - Object System.Windows.Forms.Label
  $textLabel1.Location = New - Object System.Drawing.Point(300, 40)
  $textLabel1.AutoSize = $True
  $textLabel1.Text = "Enter Message Subject without Quotes:"

  ##
  OK / Cancel Buttons-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --


    ##OK Button

  $okButton = New - Object System.Windows.Forms.Button
  $okButton.Location = New - Object System.Drawing.Point(130, 230)
  $okButton.Size = New - Object System.Drawing.Size(100, 40)
  $okButton.Text = "OK"
  $okButton.TabIndex = 4
  $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

  ## Cancel Button

  $cancelButton = New - Object System.Windows.Forms.Button
  $cancelButton.Location = New - Object System.Drawing.Point(255, 230)
  $cancelButton.Size = New - Object System.Drawing.Size(100, 40)
  $cancelButton.Text = "Cancel"
  $cancelButton.TabIndex = 5
  $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel


  ## Actions / Misc.-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    ##Get Text Box Input

  $messageSubject = $textBox.Text

  ## Search Transport Log Function

  function Message - Subject {
    Set - Variable $messageSubject = $textBox.Text - Scope 1
    $i = 0
    $transportServers = get - transportserver
    foreach($transportserver in $transportServers) {
      $trackingLogs = Get - MessageTrackingLog - MessageSubject $messageSubject - resultsize unlimited - Start "Feb 27 2018 00:00:00" - EventId Deliver | Select - Object {
        $_.Recipients
      }
      $i++
      Write - Progress - Activity "Checking Transport Logs" - status "Server: $i of $($transportServers.Count) " - PercentComplete(($i / $transportServers.Count) * 100)
    }

    $trackingLogs | Export - Csv "C:\test.csv" - NoTypeInformation
  }

  ##
  Add Form and GroupBox Radio Button Controls

  $form.Controls.AddRange(@($GroupBox, $textBox, $textLabel1, $okButton, $cancelButton))
  $GroupBox.Controls.AddRange(@($radioButton1, $radioButton2, $radioButton3, $radioButton4))


  ## Form Accept and Cancel Options Assigned to OK / Cancel Buttons

  $form.AcceptButton = $okButton
  $form.CancelButton = $cancelButton

  ## Activate and Show Form

  $form.Add_Shown({
    $form.Activate()
  })

  $result = $form.ShowDialog()

  ## Credential input

  ## Connection Commands


  ## Script block actions-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -


  if ($result - eq "OK") {##
    Check radio button status
    if ($radioButton1.Checked) {
      $response1 = [System.Windows.MessageBox]::Show("Connect to", "Confirmation", 4)
      if ($response1 - eq "Yes") {
        $creds1 = Get - Credential - Message 'Enter  Exchange credentials:'
        $session1 = New - PSSession - ConfigurationName Microsoft.Exchange - ConnectionUri "http://" - Credential $creds1 - Authentication Kerberos
        Import - PSSession $session1
        Invoke - Command - Session $session1 - ScriptBlock $ {
          function: Message - Subject
        }
        $host.ui.RawUI.WindowTitle = $creds1.UserName + " (Exchange 2010) "
        $host.ui.RawUI.BackgroundColor = "Black"
        $host.UI.RawUI.ForegroundColor = "Red"
        Clear - Host
      } else {
        $form.close()
      }
    }
    if ($radioButton2.Checked) {
      $response2 = [System.Windows.MessageBox]::Show("Connect to", "Confirmation", 4)
      if ($response2 - eq "Yes") {
        $creds2 = Get - Credential - Message 'Enter  Exchange credentails:'
        $session2 = New - PSSession - ConfigurationName Microsoft.Exchange - ConnectionUri "http://fqdn.domain/PowerShell" - Credential  $creds2 - Authentication Kerberos
        Import - PSSession $session2
        $host.ui.RawUI.WindowTitle = $creds2.UserName + " (Exchange 2010) "
        $host.ui.RawUI.BackgroundColor = "Black"
        $host.UI.RawUI.ForegroundColor = "Red"
        Clear - Host
      } else {
        $form.Close()
      }
    }
    if ($radioButton3.Checked) {
      $response3 = [System.Windows.MessageBox]::Show("Connect to", "Confirmation", 4)
      if ($response3 - eq "Yes") {
        $creds3 = Get - Credential - Message 'Enter  Exchange credentails:'
        $session3 = New - PSSession - ConfigurationName Microsoft.Exchange - ConnectionUri "http://fqdn.domain/PowerShell" - Credential $creds3 - Authentication Kerberos
        Import - PSSession $session3
        $host.ui.RawUI.WindowTitle = $creds3.UserName + " (Exchange 2013) "
        $host.ui.RawUI.BackgroundColor = "Black"
        $host.UI.RawUI.ForegroundColor = "Red"
        Clear - Host
      } else {
        $form.Close()
      }
    }
    if ($radioButton4.Checked) {
      $response4 = [System.Windows.MessageBox]::Show("Connect to ", "Confirmation", 4)
      if ($response4 - eq "Yes") {
        $rcreds3 = Get - Credential - Message 'Enter  Exchange credentails:'
        $session4 = New - PSSession - ConfigurationName Microsoft.Exchange - ConnectionUri "https://ps.outlook.com/powershell-liveid/" - Credential $creds4 - Authentication Basic - AllowRedirection
        Import - PSSession $session4 - DisableNameChecking

        $session4 = New - PSSession - ConfigurationName Microsoft.Exchange - ConnectionUri "https://ps.compliance.protection.outlook.com/powershell-liveid/" - Credential $creds4 - Authentication Basic - AllowRedirection
        Import - PSSession $session4 - AllowClobber - DisableNameChecking
        $host.ui.RawUI.WindowTitle = $creds4.UserName + " (Exchange Online + Compliance Center) "
        $host.ui.RawUI.BackgroundColor = "Black"
        $host.UI.RawUI.ForegroundColor = "Red"#
        Clear - Host
      } else {
        $form.close()
      }
    }
  } else {
    if ($result - eq "Cancel") {
      [System.Windows.MessageBox]::Show("Cancel and exit?", "Confirmation", 4)
    }
  }


}##
End Connect - Exchange Function


## Call the Function-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --


Connect - Exchange




0 个答案:

没有答案