Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何插入断开或键序列信息powershell作业_Powershell_Powershell 2.0_Powershell 3.0_Netmon - Fatal编程技术网

如何插入断开或键序列信息powershell作业

如何插入断开或键序列信息powershell作业,powershell,powershell-2.0,powershell-3.0,netmon,Powershell,Powershell 2.0,Powershell 3.0,Netmon,我正在尝试在一系列机器上运行netmon,我能够启动netmon,这看起来很好。但是,当我发出停止作业*时,netmon似乎没有正确关闭,这导致数据包捕获无效。Netmon希望出现“ctrl+C”,当我停止作业时,有没有办法发出此消息?Netmon可以使用/stopwhen/keypress选项使用其他按键序列 $server = get-content C:\Server.txt $capturedevice = "CAPTUREMACHINE" foreach ($server in $

我正在尝试在一系列机器上运行netmon,我能够启动netmon,这看起来很好。但是,当我发出
停止作业*
时,netmon似乎没有正确关闭,这导致数据包捕获无效。Netmon希望出现“ctrl+C”,当我停止作业时,有没有办法发出此消息?Netmon可以使用/stopwhen/keypress选项使用其他按键序列

$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 }

我没有现成的解决方案给你,但这里有一些值得思考的东西

您有两个问题需要解决:

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

  • 您可以尝试使用
    启动流程

    $nmcap = Start-Process -FilePath "Nmcap" -ArgumentList  "/capture /network * /file C:\temp\"$servername".chn:400MB"
    
    $nmcap.Close() #or $nmcap.CloseMainWindow()
    $nmcap.WaitForExit()
    
  • 或者,如果使用
    $nmcap.Close()
    $nmcap.CloseMainWindow()
    上述代码无法工作,则您可以尝试将Ctrl+C发送到
    nmcap

    $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()
    }
    
  • 如果一切都失败了,您可以尝试采用C#解决方案:
  • 要优雅地关闭
    Nmcap
    进程,您必须在PowerShell尝试停止此作业时检测作业内部

  • 基于文件的方法:
  • 基于事件的方法:。但是你必须把代码颠倒过来,然后向工作发送信号,我还没有试过,所以我不知道这是否可行

  • 谢谢,我来试一试。