Skip to content

Instantly share code, notes, and snippets.

@halr9000
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save halr9000/cc8cb372fafe73a9ec8d to your computer and use it in GitHub Desktop.

Select an option

Save halr9000/cc8cb372fafe73a9ec8d to your computer and use it in GitHub Desktop.
Simple PowerShell scripted input for Splunk that can invoke Splunk commands
# Get script path (works on v2+ of PowerShell) http://stackoverflow.com/a/5466355. v3+ has $PSScriptRoot variable, but win2k3 boxes might not have v3.
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Write-Verbose "Script Path: $scriptPath"
# find $SPLUNK_HOME starting from the bin path for an app, e.g. $env:programfiles\Splunk\etc\apps\appname\bin\test.ps1
# could just use string concatenation, but using the Resolve-Path and Join-Path cmdlets here will generate useful errors if they fail
$SPLUNK_HOME = Resolve-Path ( Join-Path -Path $scriptPath -ChildPath "..\..\..\.." ) # if my relative path is right?
Write-Verbose "Splunk Home: $splunk_home"
$SPLUNK_BIN = Join-Path -Path $SPLUNK_HOME -ChildPath "bin\splunk.exe" # join-path figures out starting & trailing path separators the right way
# arguments to splunk.exe
# each sub-command and argument must be enclosed in an array, otherwise all items would be enclosed in one set of quotes upon execution, e.g. splunk "help foo" and of course "help foo" isn't a valid command
$splunkArgs = @("help", "clone-prep-clear-config")
# using invoke operator, otherwise it would just print the string
Write-Verbose "Running command: $SPLUNK_BIN $splunkArgs"
& "$SPLUNK_BIN" $splunkArgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment