通过Powershell运行远程命令
我试图通过脚本在一系列远程系统上调用离散CLI命令,但我无法获得任何PowerShell命令来接受它们。我将在下面提供一些伪代码,而不是试图解释问题的细节 请注意,这只是一个简单的例子。使用停止服务命令不是一个选项。这些是通过CLI和Splunk程序使用的显式命令,我需要按此顺序运行这些命令 简而言之,我就是不知道如何告诉PowerShell在远程机器上逐字运行CLI命令通过Powershell运行远程命令,powershell,Powershell,我试图通过脚本在一系列远程系统上调用离散CLI命令,但我无法获得任何PowerShell命令来接受它们。我将在下面提供一些伪代码,而不是试图解释问题的细节 请注意,这只是一个简单的例子。使用停止服务命令不是一个选项。这些是通过CLI和Splunk程序使用的显式命令,我需要按此顺序运行这些命令 简而言之,我就是不知道如何告诉PowerShell在远程机器上逐字运行CLI命令 foreach ($server in $list) cd C:\Program Files\SplunkUniv
foreach ($server in $list)
cd C:\Program Files\SplunkUniversalForwarder\bin
splunk stop
splunk clone-prep-clear-config
splunk start
有很多方法可以做到这一点。使用WMI c/o Powershell: 您也可以使用Windows远程处理,但我从这里开始。您可以尝试
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
}
}
注意:可能需要从命令中删除-wait
和/或-nonewindow
,具体取决于进程的行为
还有输出重定向参数,请查看下面的文档以了解更多信息
我今天早上就这么做了。这是我想到的主要部分
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
}
我的完整代码如下:
#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
}
建议:用您尝试过的内容(以及结果)更新您的问题。