1

I'm trying to invoke discrete CLI commands on a series of remote systems via a script, and I can't get any PowerShell commands to accept them. Rather than try to explain the specifics of the issue, I'll provide some pseudocode of what I'm trying to do below.

Please note that this is just a simple example. Using the stop-service command is not an option. These are explicit commands used via CLI with via the Splunk program that I need to run in this order.

In short, I just can not figure out how to tell PowerShell to run a CLI command verbatim on a remote machine.

foreach ($server in $list)
     cd C:\Program Files\SplunkUniversalForwarder\bin
     splunk stop
     splunk clone-prep-clear-config
     splunk start
1
  • 1
    Recommended: Update your question with what you have tried (and with what results). Commented Apr 19, 2018 at 20:41

3 Answers 3

1

Bunch of ways you can do this. Using WMI c/o Powershell:

Starting,Stopping and Restarting Remote Services with PowerShell

You can also use Windows remoting, but I'd start here.

Sign up to request clarification or add additional context in comments.

Comments

0

You could try...

Foreach($server in $list)
{
    Invoke-command -computername $server -scripblock {
        $splunkpath = 'c:\program files\splunkuniversalforwarder\bin\splunk.exe'
        Start-process -filepath $splunkpath -argumentlist 'stop' -wait -nonewwindow
        Start-process -filepath $splunkpath -argumentlist 'clone-prep-clear-config' -wait -nonewwindow
        Start-process -filepath $splunkpath -argumentlist 'start' -wait -nonewwindow
    }
}

Note: you may need to remove the -wait and/or -nonewwindow from the commands depending on how your process behaves.

There are also output redirection parameters checkout the docs below for more.

Invoke-command

Start-process

Comments

0

I literally just did this this morning. This is the main part I came up with.

foreach($server in $servers){
Write-Host "From " -nonewline; Write-Host "$server" -ForegroundColor Yellow
Invoke-Command -ComputerName $server -ScriptBlock { C:\SplunkUniversalForwarder\bin\splunk.exe stop } -Credential $cred
Invoke-Command -ComputerName $server -ScriptBlock { C:\SplunkUniversalForwarder\bin\splunk.exe clone-prep-clear-config } -Credential $cred
Invoke-Command -ComputerName $server -ScriptBlock { C:\SplunkUniversalForwarder\bin\splunk.exe start } -Credential $cred

}

my full code is below:


#Author: Christopher Boillot
#Clear config of Splunk Forwarder



[CmdletBinding()]
Param ([Parameter(Mandatory=$False,Position=0)]
         [String[]]$servers = (Get-Content C:\ClearConfig.txt))


Set-Location $PSScriptRoot

#User login
$User = "user.txt" 
$FileExists = Test-Path $User 
If ($FileExists -eq $False) {
Write-Host "Enter your user name. This will be saved as  $User"
read-host | out-file $User
}
$Pass = "securepass.txt" 
$FileExists = Test-Path $Pass 
If ($FileExists -eq $False) {
Write-Host "Enter your password. This will be saved as an encrypted sting as $Pass"
read-host -assecurestring | convertfrom-securestring | out-file $Pass
}

$username = cat $User
$password = cat $Pass | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password


#go through each server in list
foreach($server in $servers){



Write-Host "From " -nonewline; Write-Host "$server" -ForegroundColor Yellow
Invoke-Command -ComputerName $server -ScriptBlock { C:\SplunkUniversalForwarder\bin\splunk.exe stop } -Credential $cred
Invoke-Command -ComputerName $server -ScriptBlock { C:\SplunkUniversalForwarder\bin\splunk.exe clone-prep-clear-config } -Credential $cred
Invoke-Command -ComputerName $server -ScriptBlock { C:\SplunkUniversalForwarder\bin\splunk.exe start } -Credential $cred

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.