我们有一个程序来控制场所的门禁。每当有人打开带有标签的门时,就会在程序数据库中注册一个事件。可以通过启用HTTP集成来读取这些事件,从而可以在localhost Web浏览器中查看它们。
我们想将从HTTP URL查看的事件导出到Splunk。为此,我一直在编写一个PowerShell脚本,该脚本使用Invoke-RestMethod
将URL中的数据提取到C:\Scripts
上的文件中,Splunk随后对其进行监视。
这里是我到目前为止拥有的PowerShell脚本:
$getRestMethodParams = @{
Uri = 'http://localhost:5004/eventexport?end_date=keep'
Method = 'Get'
Credential = $Creds
OutFile = 'C:\Scripts\SplunkOutput.xml'
}
Invoke-RestMethod @getRestMethodParams
使用的URI将通过end_date=keep
使心跳保持打开状态,因此我们正在实时监视事件。该脚本还将结果输出到文件'C:\Scripts\SplunkOutput.xml'
中。到目前为止,一切都很好。
但是,代码也会始终将文件保持在打开/使用状态(由于心跳参数),这会阻止Splunk从文件中读取文件,直到我终止脚本为止(我们不想这么做)( ,我们将不得不在某个时候防止文件变得太大,但这将在以后进行。]
[一位同事建议我尝试使用[System.IO.File]
来操纵文件流,但到现在为止。这是我使用的代码:
$file = [System.IO.File]::Open('C:\Scripts\SplunkOutput.xml')
$getRestMethodParams = @{
Uri = 'http://localhost:5004/eventexport?end_date=keep'
Method = 'Get'
Credential = $Creds
OutFile = $file
}
Invoke-RestMethod @getRestMethodParams
不幸的是,这给了我以下输出:
Cannot find an overload for "Open" and the argument count: "1".
At C:\Scripts\SplunkPoller1.ps1:12 char:1
+ $file = [System.IO.File]::Open('C:\Scripts\SplunkOutput.xml')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
我也尝试过(来自PowerShell Closing FileStream):
$inFile = 'C:\Scripts\SplunkOutput.xml'
$inFS = New-Object FileStream($inFile, [FileMode]::Open)
$getRestMethodParams = @{
Uri = 'http://localhost:5004/eventexport?end_date=keep'
Method = 'Get'
Credential = $Creds
OutFile = $inFS
}
Invoke-RestMethod @getRestMethodParams
哪个给了我:
Unable to find type [FileMode].
At C:\Scripts\SplunkPoller1.ps1:11 char:40
+ $inFS = New-Object FileStream($inFile, [FileMode]::Open)
+ ~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (FileMode:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
关于解决此问题的所有技巧,将不胜感激!谢谢。
在Windows上,您也可以尝试使用Monitor
,在inputs.conf
中记录,而不是在MonitorNoHandle
中使用https://docs.splunk.com/Documentation/Splunk/8.0.2/Data/Monitorfilesanddirectorieswithinputs.conf#MonitorNoHandle_syntax,>
[MonitorNoHandle
不使用Windows文件句柄读取文件,因此可以用于保持打开状态的文件。