We have a program that controls door access on our premises. Whenever a person opens the door with their tag, an event is registered in the programs database. These events can be read by enabling a HTTP integration, which makes it possible to view them in the localhost web browser.
We want to export the events viewed from the HTTP URL to Splunk. To do that, I have been writing a PowerShell script that uses Invoke-RestMethod
to fetch the data from the URL to a file on C:\Scripts
, which Splunk then monitors.
Here's the PowerShell script I have so far:
$getRestMethodParams = @{
Uri = 'http://localhost:5004/eventexport?end_date=keep'
Method = 'Get'
Credential = $Creds
OutFile = 'C:\Scripts\SplunkOutput.xml'
}
Invoke-RestMethod @getRestMethodParams
The URI used will keep the connection open with a heartbeat with end_date=keep
so we are monitoring the events in real-time. The script will also output the results into the file 'C:\Scripts\SplunkOutput.xml'
. So far, so good.
However, the code will also always keep the file in an open/used state (because of the heartbeat parameter), which prevents Splunk from reading from the file until I terminate the script, which we don't want to do (well, we will have to at some point to prevent the file from growing too big, but that will be done later on).
A colleague suggested I tried to use [System.IO.File]
to manipulate the file streams, but I only got so far. This is the code I used:
$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
Unfortunately, that gave me the output as:
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
I also tried with (from 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
Which gave me:
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
Any and all tips would be greatly appreciated on how to approach this issue! Thanks.