【发布时间】:2019-08-07 15:13:13
【问题描述】:
我有问题;我想自动化一些东西:
我有一个 *.log 文件,其中包含记录的数据行; 我需要知道如何在 powershell 中创建一个脚本来搜索某一行,如果该行等于 0,则在 windows 事件查看器中创建一个事件。有人可以帮忙吗? 我的日志如下所示:
数据 = 1
测试 = 5
com = 5
连接数 = 1
当 *.log 文件中的 connection = 0 时,它应该在事件查看器中创建一个事件。 如果可能作为次要任务,我想每天运行两次这个脚本; 15:00 和 00 点。
谢谢!
试图用powershell找到= 0
Select-String -Path C:\Users\user\test.log -Pattern '= 0'
IF (Pattern '= 0')
{
Write-EventLog –LogName Application –Source “My Script” –EntryType
Information –EventID 30000
}
收到错误:
Pattern : The term 'Pattern' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or
if a path was included, verify that the path is correct and try again.
At line:2 char:5
+ IF (Pattern '= 0')
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (Pattern:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
【问题讨论】:
-
我设法使脚本的第一部分工作,但现在,它不执行在 EventLog 中写入事件的第二部分:
-
PS C:\Windows\system32> $SEL = Select-String -Path C:\Users\user\test.log - 模式 "test =0" if ($SEL -ne $null) { Write-EventLog -LogName Eroare -Source scripts -Message “s-a gasit 0 in log” - EventId 15 -EntryType information } else { echo Not Contains String }
-
上述方法有效,但会引发以下错误:Write-EventLog : 计算机“localhost”上不存在源名称“scripts”。在 line:5 char:5 + Write-EventLog -LogName Eroare -Source scripts -Message “s-a gasi ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [ Write-EventLog], InvalidOperationException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteEventLogCommand
-
PS C:\Windows\system32> New-EventLog -LogName Application -Source "scripts" 出现以下错误:Write-EventLog : The Log name "Eroare" does not exist in the computer"本地主机”。在 line:5 char:5 + Write-EventLog -LogName Eroare -Source scripts -Message “s-a gasi ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [ Write-EventLog], InvalidOperationException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteEventLogCommand
标签: powershell-4.0