Powershell在事件日志中搜索注册表内容

时间:2012-07-17 06:28:47

标签: powershell

我正在寻找一些代码来搜索日志文件的注册表内容。具体做法是:

Get-ItemProperty -path hklm:\system\currentcontrolset\enum\usbstor\*\* | select PSChildName

会列出序列号。对于每个序列号,我需要能够搜索文本日志文件以查看项目何时出现。

以下是我现在使用的内容:

Get-ItemProperty -path hklm:\system\currentcontrolset\enum\usbstor\*\* | select PSChildName | foreach-object {Get-Content C:\Windows\inf\setupapi.dev.log | select-string '$_.PSChildName' -context 1}

但搜索PSChildName无效,我缺少什么?

1 个答案:

答案 0 :(得分:0)

我认为它的原因是没有通过$ _. PSChildName发送任何东西。所以你可以使用2个解决方案中的一个

解决方案1 ​​

Get-ItemProperty -path hklm:\system\currentcontrolset\enum\usbstor\*\* | select PSChildName | foreach-object {$P = $_.PSChildName ; Get-Content  C:\Windows\inf\setupapi.dev.log | select-string $P -SimpleMatch -context 1} 

解决方案2

$KeyListArray = @()
    Foreach($key in Get-ItemProperty -path hklm:\system\currentcontrolset\enum\usbstor\*\* | select PSChildName)
    {$KeyListArray +($key)}


foreach($PsChild in $KeyListArray)
{Get-Content C:\Windows\inf\setupapi.dev.log | select-string -Pattern "$PsChild" -context 1}