我是PowerShell的新手并且已经学到了很多东西。但还有更多要去。所以我的代码并不是最严格的。
我创建了一个事件日志搜索工具。它允许我通过ID,错误级别,关键字等进行搜索。除了关键字和提供者名称之外,它在大多数情况下都有效。
目前,在尝试在日志中搜索关键字或关键字集时,脚本会提示错误消息:
Get-WinEvent:指定的图像文件不包含资源部分 在C:\ Users \ Rob \ Google Drive \ Powershell \ Get-logs.ps1:65 char:9 + Get-WinEvent -FilterHashtable @ {Logname = $ Log} -ComputerName $ Computer | ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [Get-WinEvent],EventLogException + FullyQualifiedErrorId:指定的图像文件不包含资源部分,Microsoft.PowerShell.Commands.GetWinEventCommand
问题是,我不理解ps1.65 char:9位。然后该脚本继续并从日志中获取旧的无关数据。
以下是我遇到问题的两个区域的代码。最后的完整代码。
关键字搜索:
elseif ($Kwrd -gt "a"){
foreach ($Kwrd in $Kwrd)
{
Get-WinEvent -FilterHashtable @{logname=$Log} -ComputerName $Computer | where-object { $_.Message -like "*$Kwrd*" } | Sort-Object TimeGenerated -Descending | Select-Object -First $Maxnum | Format-List
}
}
提供商名称搜索:
elseif ($Prov.Length -gt 1){
Get-WinEvent -FilterHashtable @{Logname=$Log} -ComputerName $Computer | Where-Object {($_.ProviderName -like "*$Prov*")} | Sort-Object TimeGenerated -Descending|Select-Object -First $Maxnum | Format-List
}
因此,例如,如果我想在应用程序日志中搜索提供程序名称System Restore,(我最近运行的Revo应用程序中有一些),这就是脚本所做的事情。
Enter Computer or EXIT to quit: office
Enter log set to retrieve: application
Enter Instance ID or leave blank:
Enter number of logs to retrieve: 10
Enter error level or leave blank:
Search logs by keyword or leave blank:
Search by Provider or leave blank: System Restore
Get-WinEvent : The specified image file did not contain a resource section
At C:\Users\Rob\Google Drive\Powershell\Get-logs.ps1:65 char:9
+ Get-WinEvent -FilterHashtable @{Logname=$Log} -ComputerName $Computer | ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-WinEvent], EventLogException
+ FullyQualifiedErrorId : The specified image file did not contain a resource section,Microsoft.PowerShell.Commands.GetWinEventCommand
PS C:\Users\Rob>
关键字搜索也是同样的问题。相同的错误消息。唯一的区别是行号从65变为61,因为代码在第61行。
它并不完美,但我随时都在学习。这是完整的脚本。我是否有任何想法如何从日志中获取信息而没有错误?
Clear-Host
while (1 -ne 2){
$Computer = $Null
$IDNum = $Null
$Lvl = $Null
$Kwrd = $Null
$Prov = $Null
Write-Host ''
$Computer = Read-Host "Enter Computer or EXIT to quit"
if ($Computer -eq "EXIT") {exit;}
$Log = Read-Host "Enter log set to retrieve"
$IDNum = Read-Host "Enter Instance ID or leave blank"
$IDNum = $IDNum.Split(',')
$MaxNum = $MaxNum = Read-Host "Enter number of logs to retrieve"
$Lvl = Read-Host "Enter error level or leave blank"
$Lvl = $Lvl.Split(',')
$Kwrd = Read-Host "Search logs by keyword or leave blank"
$Kwrd = $Kwrd.Split(',')
$Prov = Read-Host "Search by Provider or leave blank"
if ($IDNum.Length -gt 1){
foreach ($IDNum in $IDNum)
{
Get-WinEvent -FilterHashTable @{LogName=$Log; ID=$IDNum} -ComputerName $Computer | Where-Object { ($_.ID -eq "*$IDNum*")} |Sort-Object TimeGenerated -Descending | Select-Object -First $Maxnum| Format-List
}
}
elseif ($Lvl -gt 1 ){
foreach ($Lvl in $Lvl)
{
Get-WinEvent -FilterHashTable @{LogName=$Log;Level=$lvl} -ComputerName $Computer -MaxEvents $MaxNum |Select-Object -First $MaxNum | Sort-Object TimeGenerated -Descending | Format-List
}
}
elseif ($Kwrd -gt "a"){
foreach ($Kwrd in $Kwrd)
{
Get-WinEvent -FilterHashtable @{logname=$Log} -ComputerName $Computer | where-object { $_.Message -like "*$Kwrd*" } | Sort-Object TimeGenerated -Descending | Select-Object -First $Maxnum | Format-List
}
}
elseif ($Prov.Length -gt 1){
Get-WinEvent -FilterHashtable @{Logname=$Log} -ComputerName $Computer | Where-Object {($_.ProviderName -like "*$Prov*")} | Sort-Object TimeGenerated -Descending|Select-Object -First $Maxnum | Format-List
}
else {
Get-WinEvent -LogName $Log -ComputerName $Computer | Sort-Object TimeGenerated -Descending| Select-Object -First $MaxNum | Format-List
}
} else{
Clear-Host
$log = $IDNum = $MaxNum = $Lvl = $Kwrd = $Prov = $Null
continue
Write-Host ''
Write-Host ''
}
感谢。
答案 0 :(得分:0)
注意:我以管理员身份运行ISE。
几点意见
1)我无法重新创建错误(您的脚本在我的计算机上正常运行)但它的行为就好像它试图打开/读取图像文件一样(非常奇怪)。
2)我尝试在没有参数的情况下运行get-winevent
,并且出现了很多get-winevent : The data is invalid
错误。当我研究这个错误时,我了解到get-winevent
似乎是一个错误/有问题/挑剔的cmdlet
。所以,我建议您尝试使用get-eventlog
3)你在一个循环中调用Get-WinEvent
,这使得程序运行所需的时间更长。我建议您执行一次Get-EventLog
(请参阅上面的注释#2)并将输出传递给out-gridview
。例如:
Get-EventLog -LogName application | out-gridview -Title "App log events"
然后,使用out-gridview
过滤器仅显示您想要查看的输出。
上述命令的示例输出: