Start And Stop Windows Service remotely using PSEXEC

可紊 提交于 2019-12-03 06:15:37

I can't test this right now, but it ought to be:

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

and

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

PSService on SysInternals is specifically for remotely controlling services::`

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

where:

query Displays the status of a service.

config Displays the configuration of a service.

setconfig Sets the start type (disabled, auto, demand) of a service.

start Starts a service.

stop Stops a service.

restart Stops and then restarts a service.

pause Pauses a service

cont Resumes a paused service.

depend Lists the services dependent on the one specified.

security Dumps the service's security descriptor.

find Searches the network for the specified service.

\\computer Targets the NT/Win2K system specified.

Include the -u switch with a username and password to login to the remote system if your security credentials do not permit you to obtain performance counter information from the remote system. If you specify the -u option, but not a password with the -p option, PsService will prompt you to enter the password and will not echo it to the screen.

Another alternative to psexec is sc. You can use sc to start or stop services remotely:

sc \\server start ServiceName

sc \\server stop ServiceName

There is no "login" information, so maybe you need to execute

net use \\server password /USER:user

before executing sc command.

One advantage over psexec is that no console window shows in the remote machine.

Using PSEXEC

The below batch file will let you stop and start services on multiple remote machines. Create Computers.txt file in the same directory where the batch file runs from and list PC hostnames one per line.

@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

Using 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"
         }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!