【问题标题】:Start And Stop Windows Service remotely using PSEXEC使用 PSEXEC 远程启动和停止 Windows 服务
【发布时间】:2009-09-02 07:13:23
【问题描述】:

如何使用 PSEXEC 远程启动和停止 Windows 服务?最好写的语法我尝试了下面给出的cmdlet

psexec \\Server -u Administrator -p Somepassword ServiceName

【问题讨论】:

  • 这在 serverfault.com 上不是更好吗?

标签: windows-services psexec


【解决方案1】:

PSService on SysInternals专门用于远程控制服务::`

psservice [\\computer [-u username] [-p password]] <command> <options>

地点:

查询显示服务的状态。

config 显示服务的配置。

setconfig 设置服务的启动类型(禁用、自动、需求)。

start 启动服务。

stop停止服务。

重新启动停止然后重新启动服务。

暂停暂停服务

继续恢复暂停的服务。

depend 列出依赖于指定服务的服务。

security转储服务的安全描述符。

find 在网络中搜索指定的服务。

\\computer 以指定的 NT/Win2K 系统为目标。

如果您的安全凭证不允许您从远程系统获取性能计数器信息,请使用带有用户名和密码的 -u 开关登录远程系统。如果您指定了 -u 选项,但没有使用 -p 选项指定密码,PsService 将提示您输入密码,并且不会将其回显到屏幕上。

【讨论】:

    【解决方案2】:

    psexec 的另一种替代方法是 sc。您可以使用 sc 远程启动或停止服务:

    sc \\server start ServiceName
    
    sc \\server stop ServiceName
    

    没有“登录”信息,所以可能需要执行

    net use \\server password /USER:user
    

    在执行 sc 命令之前。

    与 psexec 相比的一个优点是远程计算机中不显示控制台窗口。

    【讨论】:

    • 好一个。我发现我必须这样做:net use \\server\IPC$ password /USER:user
    【解决方案3】:

    我现在无法测试,但应该是:

    psexec \\server -u username -p password net start ArgusCommunityWorkerService
    

    psexec \\server -u username -p password net stop ArgusCommunityWorkerService
    

    【讨论】:

      【解决方案4】:

      使用 PSEXEC

      下面的批处理文件可以让您在多台远程机器上停止和启动服务。在运行批处理文件的同一目录中创建 Computers.txt 文件,并每行列出一个 PC 主机名。

      @echo off
      TITLE Manage Services v1.0
      SET suffix=%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
      SET /P username=Enter your admin username: 
      set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
          $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
              [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
      for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
      :service
      SET /P servicename=Enter service name:
      :begin
      echo ========================================
      echo 1) Start
      echo 2) Stop
      echo 3) Choose another service
      echo ========================================
      ECHO.
      set /p op=Select an option:
      if "%op%"=="1" SET action=start
      if "%op%"=="2" SET action=stop
      if "%op%"=="3" goto service
      
      psexec "\\@%~dp0Computers.txt" -u %username% -p %password% -h net %action% %servicename% >>%suffix%.log 2>&1
      
      pause
      cls
      goto begin
      

      使用 PowerShell

      # Point the script to the text file with remote computers
      $RemoteComputers = Get-Content "$PSScriptRoot\Computers.txt"
      
      # sets service name
      $Service = "uvnc_service"
      
      # Counter for progress bar
      $counter = 0
      
      ForEach ($Computer in $RemoteComputers) {
          $counter++
           Try
               {
                Write-Progress -Activity 'Processing computers' -CurrentOperation $Computer -PercentComplete (($counter / $RemoteComputers.count) * 100)
                Start-Sleep -Milliseconds 200
                Get-Service -Name $Service -ComputerName $Computer | Restart-Service -Force -ErrorAction Stop
                Write-Output "$(Get-Date -format "yyyy-MM-dd hh:mm:ss"),$computer" | out-file -append -filepath "$PSScriptRoot\success.log"
               }
           Catch
               {
                Write-Output "$(Get-Date -format "yyyy-MM-dd hh:mm:ss"),$computer" | out-file -append -filepath "$PSScriptRoot\failed.log"
               }
      }
      

      【讨论】:

        【解决方案5】:

        参考Microsoft,另一种方法是使用 psservice,它是 pstools 的一部分,可通过以下链接下载:

        Download PsTools (2.7 MB)

        如果有人需要

        远程启动、停止、重启等...一个 Windows 服务

        正如微软官方参考中提到的,使用提供的可执行文件的适当命令

        PsService.exe

        如果您使用的是 Windows PowerShell

        ,可能类似于以下内容(案例 1 和案例 2)

        案例 1:登录后将执行所需命令(例如重启)的用户是具有适当权限的远程计算机用户

        .\PsService.exe \\Remote-ComputerName-OR-ServerName -u 'RemoteComputerName-OR-ServerName\Remote-ComputerUser' -p 'Remote-ComputerUser-Password' 重启服务名

        案例 2:登录后将执行所需命令(例如重启)的用户是域超级用户(例如 DomainAdministrator)

        .\PsService.exe \\Remote-ComputerName-OR-ServerName -u 'DomainShortName\DomainAdministrator' -p 'DomainAdministrator-Password' 重启服务名

        PS:注意在这种情况下,单引号参数值用于

        用户名

        -u

        和密码

        -p

        对于复杂的密码和/或用户名

        就是这样,你的命令应该可以顺利执行了,请耐心等待!

        【讨论】: