How to start and and stop a Windows service remotely using PSEXEC? Preferably the syntax to write I tried the cmdlet given below
psexec \\Server -u Administrator -p Somepassword ServiceName
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"
}
}
来源:https://stackoverflow.com/questions/1366236/start-and-stop-windows-service-remotely-using-psexec