0

我正在尝试在一系列机器上运行 netmon,我能够启动 netmon,这似乎很好。但是,当我发出 aStop-Job *时,似乎 netmon 没有正确关闭,这导致数据包捕获无用。Netmon 期待 "ctrl + C" ,当我停止工作时有没有办法发出这个?当使用 /keypress 选项时,Netmon 可以使用 /stop 使用其他键序列

$server = get-content C:\Server.txt 
$capturedevice = "CAPTUREMACHINE"

foreach ($server in $server)
{ $scriptBlockContent = { param ($servername)
Nmcap /capture /network * /file C:\temp\"$servername".chn:400MB } 
Invoke-Command -ComputerName $server -Scriptblock $scriptBlockContent -ArgumentList $server -AsJob }

foreach ($capturedevice in $capturedevice){ $scriptBlockContent = 
{ param ($capturedevicename) Nmcap /capture /network * /file D:\temp\"$capturedevicename".chn:400MB } 
Invoke-Command -ComputerName $capturedevice -Scriptblock $scriptBlockContent -ArgumentList $capturedevice -AsJob }
4

1 回答 1

0

我没有现成的解决方案给你,但这里有一些值得深思的地方。

您有 2 个问题需要处理:

  1. 您需要优雅地关闭Nmcap进程,以便它可以保存捕获文件。

    1. 您可以尝试使用Start-Process

      $nmcap = Start-Process -FilePath "Nmcap" -ArgumentList  "/capture /network * /file C:\temp\"$servername".chn:400MB"
      
      $nmcap.Close() #or $nmcap.CloseMainWindow()
      $nmcap.WaitForExit()
      
    2. $nmcap.Close()或者,如果上面的代码使用or不起作用$nmcap.CloseMainWindow(),那么您将尝试发送Ctrl+CNmcap

      $Wasp = Add-Type -MemberDefinition @'
      [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      public static extern bool SetForegroundWindow(IntPtr hWnd);
      '@ -Passthru
      $null = Add-Type -Assembly System.Windows.Forms
      
      if($Wasp::SetForegroundWindow($nmcap.MainWindowHandle)) {
          [System.Windows.Forms.SendKeys]::SendWait("^C")
          $nmcap.WaitForExit()
      }
      
    3. 如果一切都失败了,您可以尝试采用 C# 解决方案:使用 .Net 中的 Ctrl-C 事件以编程方式停止命令行应用程序 – 一个工作演示
  2. 要优雅地关闭Nmcap进程,您必须在 PowerShell 尝试停止此作业时检测到作业内部。

    1. 基于文件的方法:如何在 PowerShell 中优雅地停止异步作业
    2. 基于事件的方法:从另一个 PowerShell 会话监视 PowerShell 会话中的作业。但是您必须反转代码并向工作发送信号,我没有尝试过,所以我不知道是否可能。
于 2015-11-12T12:11:33.113 回答