You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从PowerShell的Get-EventLog中提取进程ID(Process ID)

从Security日志事件4663中提取进程ID

你当前的代码仅获取了事件的完整Message内容,未对其中的Process ID做解析。可以通过正则捕获组直接从Message中提取目标值,以下是两种可行的解决方案:

方案一:管道内直接解析

$directory = "E:\BpLnfgDsc2.txt"
$processId = Get-EventLog -LogName Security -InstanceId 4663 -Newest 1 | 
    Where-Object { $_.Message -match "Object Name:\s*$([regex]::Escape($directory))" } |
    ForEach-Object {
        # 用正则捕获Process ID后的非空白值
        if ($_.Message -match 'Process ID:\s+(\S+)') {
            $matches[1]
        }
    }
# 输出你需要的格式化结果
Write-Output "**_$processId_**"

方案二:使用Select-String解析

$directory = "E:\BpLnfgDsc2.txt"
$eventMessage = Get-EventLog -LogName Security -InstanceId 4663 -Newest 1 | 
    Where-Object { $_.Message -match "Object Name:\s*$([regex]::Escape($directory))" } |
    Select-Object -ExpandProperty Message
# 匹配并提取Process ID
$processId = ($eventMessage | Select-String -Pattern 'Process ID:\s+(\S+)').Matches.Groups[1].Value
# 输出格式化结果
Write-Output "**_$processId_**"

关键说明

  • 用[regex]::Escape($directory)处理文件路径中的特殊字符(比如反斜杠),避免正则匹配失效
  • 正则表达式Process ID:\s+(\S+)中的(\S+)是捕获组,专门提取Process ID后面的非空白内容(即你需要的0xd34)

内容的提问来源于stack exchange,提问作者Faik SEVİM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:06:23