在这里的几个人的帮助下,我能够整理下面的脚本。我试图从我位于目录中的* .xml文件中将XML数据解析到事件查看器中。出于安全原因,我需要在创建事件日志之前提取一些信息。最初我正在替换文件中的关键字,但我使用的软件有问题,所以我不得不创建一个LastRun.txt文件来经常检查文件。如果XML文件仍然存在于文件夹中,我不想对XML文件保持警惕。
我尝试运行时遇到以下错误: 它报告:表达式仅允许作为管道的第一个元素。在行:20 char:132
以"开头' "这里:' \ b [A-Za-z0-9 ._% - ] + @ [A-Za-z0-9 .-] +。[A-Za-z] {2,4} \ b& #39;,' $ 3- $ 2- $ 1'})
#get last time the powershell script was run
$LastRunStamp = Get-Content c:\test\lastRunStamp.txt
#write current timestamp to file
Get-Date > c:\test\lastRunStamp.txt
foreach ($file in (Get-ChildItem c:\test\*.xml))
{
#if the file was modified since the last script run
if($file.LastWriteTime -ge $LastRunStamp)
{
#instatiate XML document object
$xdoc = new-object System.Xml.XmlDocument
#load up the XML contents into the object
$xdoc.load($file)
#check the value of the priority XML tag if it contains Major then write to event log
if ($xdoc.SelectNode("//priorityname") -eq 'Major') {
$content = ([IO.File]::ReadAllText($file.FullName) | {$_ -replace '.abc.com', ' '} | {$_ -replace '\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b', '$3-$2-$1'})
Write-EventLog -LogName Application -Source 'Verint Alert' `
-EntryType Warning -EventId 1 `
-Message "Triggered Alarm $content";
}
}
}
答案 0 :(得分:0)
假设代码中没有其他问题,你的问题当然是这一行
$content = ([IO.File]::ReadAllText($file.FullName) | {$_ -replace '.abc.com', ' '} | {$_ -replace '\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b', '$3-$2-$1'})
您有一个管道,但没有cmdlet来提供信息,因此错误。因此,使用当前代码,您可以执行此操作。
$content = ([IO.File]::ReadAllText($file.FullName) |
ForEach-Object {$_ -replace '.abc.com', ' '} |
ForEach-Object {$_ -replace '\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b', '$3-$2-$1'})
我只是在新行中加入,以便在此处阅读。
<强>交替强>
-replace
用作数组运算符,您可以将它们链接起来。所以这也是一个选择。
$content = [IO.File]::ReadAllText($file.FullName) -replace '.abc.com', ' ' -replace '\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b', '$3-$2-$1'
答案 1 :(得分:0)
这是那些可能偶然发现的人的决议:
$ LastRunStamp =(Get-Item c:\ test \ lastRunStamp.txt).LastWriteTime.DateTime
获取日期&gt; C:\测试\ lastRunStamp.txt
foreach($文件中的(Get-ChildItem c:\ test * .xml)) { #calculate文件修改时间与上次运行脚本之间的时差 $ span = new-timespan -start $ file.LastWriteTime.DateTime -end $ LastRunStamp
#if the file was modified since the last time the script run value will be less than 0
if($span.TotalSeconds -le 0)
{
#instantiate XML document object
$xdoc = new-object System.Xml.XmlDocument
#load up the XML contents into the object
$xdoc.load($file)
#check the value of the priority XML tag if it contains Major then write to event log
if ($xdoc.SelectSingleNode("//priorityname").innertext -eq 'Major')
{
#get the content of XML
$content = [string]([IO.File]::ReadAllText($file.FullName))
#mask the FQDN's
$content = $content.replace(".ngd.com",".sensored").replace(".ad.local",".sensored FQDN")
#regex pattern to detect IP Addresses
$pattern = "\b((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b"
#use regex to mask IP addresses
$contentScrubbed = [regex]::replace($content, $pattern, "sensored IP Address")
Write-EventLog –LogName Application –Source “Application” `
–EntryType Information –EventID 1 `
-Message ("Triggered Alarm" + $contentScrubbed)
}
}
}