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

11

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

psexec \\Server -u Administrator -p Somepassword ServiceName

1
这个问题放在serverfault.com上会不会更好? - Bryan
5个回答

19

SysInternals上的PSService是专门用于远程控制服务的:

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

命令说明:

query 显示服务的状态。

config 显示服务的配置。

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

start 启动服务。

stop 停止服务。

restart 停止并重新启动服务。

pause 暂停服务。

cont 恢复暂停的服务。

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

security 显示服务的安全描述符。

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

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

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


14

除了psexec外,另一个选择是sc。您可以使用sc远程启动或停止服务:

sc \\server start ServiceName

sc \\server stop ServiceName

没有"登录"信息,所以也许你需要执行:

net use \\server password /USER:user

在执行sc命令之前。

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


3
好的。我发现我需要执行以下操作:net use \server\IPC$ password /USER:user - Neil Mosafi

13

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

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

并且

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

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"
         }
}

0

参考Microsoft,另一种选择是使用psservice,它是pstools的一部分,可在以下链接下载:

下载 PsTools(2.7 MB)

如果有人需要远程启动、停止、重启等 Windows 服务,如 Microsoft 的官方参考所述,可以使用提供的可执行文件执行适当的命令。

PsService.exe

如果您正在使用Windows PowerShell,则可能类似于以下内容(案例1和案例2)

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

.\PsService.exe \\Remote-ComputerName-OR-ServerName -u 'RemoteComputerName-OR-ServerName\Remote-ComputerUser' -p 'Remote-ComputerUser-Password' restart ServiceName

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

.\PsService.exe \\Remote-ComputerName-OR-ServerName -u 'DomainShortName\DomainAdministrator' -p 'DomainAdministrator-Password' restart ServiceName

PS:请注意,在此情况下,对于

用户名

-u

和密码

-p

参数值需要使用单引号

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

就这样,您的命令应该能够顺利执行,只需耐心等待!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接