Search code examples
powershellsplunk

Need Powershell script to point client machines to new splunk deployment server


Basically, need the script to loop through a list of client servers and

  1. Stop splunk
  2. Set deploy-poll newDepServer:port
  3. Start splunk

Caveat - the commands im trying to run are splunk commands specifically. so, they must run in the splunkuniversalforwader\bin location

I can get the "start" and "stop" to work (I assume because those are windows commands FOR Splunk service ....

However, the set deploy-poll xxx.xxx:port is getting rejected "not a valid command"

$computername = "test_server"

$products = Get-WmiObject -Class win32_product -ComputerName $computername -filter 'Name like "% forwarder%"' | select Caption, InstallLocation

foreach ($product in $products)
{
    $installpath = $product.InstallLocation
}

$installpath += "bin\"

Invoke-Command -ComputerName $computername -ScriptBlock { 
    Set-Location "C:\program files\splunkuniversalforwarder\bin"
    & ".\splunk.exe" "stop"
    & ".\splunk.exe" "set deploy-poll xxx.xxx.xxx:xxx"
    & ".\splunk.exe" "start"
} -ArgumentList $installpath

Solution

  • Your problem is this line:

    & ".\splunk.exe" "set deploy-poll xxx.xxx.xxx:xxx"
    

    "set deploy-poll xxx.xxx.xxx:xxx" is being treated as one argument to splunk.exe instead of 3 separate ones. It should work if you change the line to the following:

    & ".\splunk.exe" set deploy-poll xxx.xxx.xxx:xxx