使用PSEXEC远程启动和停止Windows服务

时间:2009-09-02 07:13:24

标签: windows-services psexec

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

psexec \\Server -u Administrator -p Somepassword ServiceName

4 个答案:

答案 0 :(得分:16)

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

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

其中:

  

查询显示服务的状态。

     

config 显示服务的配置。

     

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

     

开始启动服务。

     

停止停止服务。

     

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

     

暂停暂停服务

     

恢复暂停的服务。

     

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

     

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

     

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

     

\\ computer 指定指定的NT / Win2K系统。

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

答案 1 :(得分:12)

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

sc \\server start ServiceName

sc \\server stop ServiceName

没有“登录”信息,因此您可能需要执行

net use \\server password /USER:user

执行sc命令之前。

优于psexec的一个优点是远程计算机中没有显示控制台窗口。

答案 2 :(得分:9)

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

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

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

答案 3 :(得分:0)

使用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"
         }
}
相关问题