首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Powershell运行远程命令

通过Powershell运行远程命令
EN

Stack Overflow用户
提问于 2018-04-20 02:47:24
回答 3查看 731关注 0票数 1

我试图通过脚本在一系列远程系统上调用离散的命令行界面命令,但是我无法获得任何接受它们的PowerShell命令。我不会试图解释这个问题的细节,而是在下面提供一些我试图做的伪代码。

请注意,这只是一个简单的示例。不能使用stop-service命令。这些是通过CLI通过Splunk程序使用的显式命令,我需要按此顺序运行这些命令。

简而言之,我只是想不出如何告诉PowerShell在远程机器上逐字运行CLI命令。

代码语言:javascript
复制
foreach ($server in $list)
     cd C:\Program Files\SplunkUniversalForwarder\bin
     splunk stop
     splunk clone-prep-clear-config
     splunk start
EN

回答 3

Stack Overflow用户

发布于 2018-04-20 02:53:48

有很多方法可以做到这一点。使用WMI c/o Powershell:

Starting,Stopping and Restarting Remote Services with PowerShell

您也可以使用Windows remoting,但我将从这里开始。

票数 1
EN

Stack Overflow用户

发布于 2018-04-20 07:49:13

你可以试试..。

代码语言:javascript
复制
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和/或-nonewwindow

还有输出重定向参数,请查看下面的文档以了解更多信息。

Invoke-command

Start-process

票数 0
EN

Stack Overflow用户

发布于 2019-03-07 02:11:47

我今天早上就这么做了。这是我想出的主要部分。

代码语言:javascript
复制
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

}

我的完整代码如下:

代码语言:javascript
复制
#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

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49928171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档