从 PowerShell 中的 Invoke-RestMethod 同时读取和写入同一个文件
Read and write to same file simultaneously from Invoke-RestMethod in PowerShell
我们有一个程序可以控制我们场所的门禁。每当有人用他们的标签打开门时,程序数据库中就会注册一个事件。可以通过启用 HTTP 集成来读取这些事件,这使得可以在本地主机 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'
中。到目前为止,还不错。
但是,代码也会始终将文件保持在 open/used 状态(因为心跳参数),这会阻止 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
我也试过(来自):
$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
任何和所有关于如何解决这个问题的提示都将不胜感激!谢谢。
除了在 inputs.conf
中使用 Monitor
,在 Windows,您还可以尝试使用 MonitorNoHandle
,记录在 https://docs.splunk.com/Documentation/Splunk/8.0.2/Data/Monitorfilesanddirectorieswithinputs.conf#MonitorNoHandle_syntax
MonitorNoHandle
不使用 Windows 文件句柄来读取文件,因此可用于保持打开的文件。
我们有一个程序可以控制我们场所的门禁。每当有人用他们的标签打开门时,程序数据库中就会注册一个事件。可以通过启用 HTTP 集成来读取这些事件,这使得可以在本地主机 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'
中。到目前为止,还不错。
但是,代码也会始终将文件保持在 open/used 状态(因为心跳参数),这会阻止 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
我也试过(来自
$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
任何和所有关于如何解决这个问题的提示都将不胜感激!谢谢。
除了在 inputs.conf
中使用 Monitor
,在 Windows,您还可以尝试使用 MonitorNoHandle
,记录在 https://docs.splunk.com/Documentation/Splunk/8.0.2/Data/Monitorfilesanddirectorieswithinputs.conf#MonitorNoHandle_syntax
MonitorNoHandle
不使用 Windows 文件句柄来读取文件,因此可用于保持打开的文件。