Powershell - 使用 ExecutionPath 和 CommandLine 监控新进程

Powershell - monitoring new processes with ExecutionPath and CommandLine

我想知道你是否可以帮助我编写脚本(根据标题)

所以我一直在研究下面的代码:

Register-CimIndicationEvent -ClassName Win32_ProcessStartTrace -SourceIdentifier "ProcessStarted"

我需要Get-Event returns的输出:

Get-Event | select timegenerated, @{N='ProcessName'; E = {$_.sourceeventargs.newevent.processname}}
TimeGenerated         ProcessName           
-------------         -----------           
21-Feb-20 12:58:29 PM UpdateTrustedSites.exe
21-Feb-20 12:58:31 PM backgroundTaskHost.exe
21-Feb-20 12:58:33 PM pwrgate.exe           
21-Feb-20 12:58:33 PM chrome.exe   

但我不知道如何将其与 win32_Process (Get-WMIObject win32_Process).CommandLine and .ExecutablePath)

如有任何帮助,我们将不胜感激。

Register-CimIndicationEvent

The Register-CimIndicationEvent cmdlet subscribes to indications using an indication class name or a query expression. Use the SourceIdentifier parameter give a name to the subscription

而您使用的 SourceIdentifier 参数是 Win32_ProcessStartTrace

Win32_ProcessStartTrace 它只有以下属性您可以访问

[AMENDMENT]
class Win32_ProcessStartTrace : Win32_ProcessTrace
{
  uint8  SECURITY_DESCRIPTOR[];
  uint64 TIME_CREATED;
  uint32 ProcessID;
  uint32 ParentProcessID;
  uint8  Sid[];
  string ProcessName;
  uint32 SessionID;
};

也就是说,如果要查找进程的命令和路径,则必须分别为每个进程查找进程信息。

foreach($event in Get-Event) {
    $TimeGen = $event.timegenerated
    $ProcessName = $event.sourceeventargs.newevent.processname
    $Process = Get-WmiObject Win32_Process -Filter "Name LIKE '$ProcessName'" | select -First 1
    $ProcessCMD = ($Process | select CommandLine).CommandLine
    $processPath = ($Process | select ExecutablePath).ExecutablePath
    $out = [pscustomobject]@{
        Time=$TimeGen
        Name=$ProcessName
        Path=$processPath
        Command=$ProcessCMD
    }
    $out
}

您可以使用数组或哈希表组合 $out 参数,并根据需要进一步查询。