【问题标题】:Run Remote Command via Powershell通过 Powershell 运行远程命令
【发布时间】:2019-03-07 02:11:47
【问题描述】:

我正在尝试通过脚本在一系列远程系统上调用离散的 CLI 命令,但我无法让任何 PowerShell 命令接受它们。我将在下面提供一些我正在尝试做的事情的伪代码,而不是试图解释问题的细节。

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

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

foreach ($server in $list)
     cd C:\Program Files\SplunkUniversalForwarder\bin
     splunk stop
     splunk clone-prep-clear-config
     splunk start

【问题讨论】:

  • 推荐:用你尝试过的东西(以及结果)更新你的问题。

标签: powershell


【解决方案1】:

您可以通过多种方式做到这一点。使用 WMI c/o Powershell:

Starting,Stopping and Restarting Remote Services with PowerShell

您也可以使用 Windows 远程处理,但我会从这里开始。

【讨论】:

    【解决方案2】:

    你可以试试……

    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

    【讨论】:

      【解决方案3】:

      我今天早上确实这样做了。这是我想出的主要部分。

      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
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-04
        • 2012-04-28
        • 2018-05-27
        相关资源
        最近更新 更多