在远程Windows系统上重启服务的最简单的编程方法是什么?语言或方法无关紧要,只要它不需要人为干预即可。
答案 0 :(得分:94)
从Windows XP开始,您可以使用sc.exe
与本地和远程服务进行交互。安排任务以运行类似于此的批处理文件:
sc \\server stop service sc \\server start service
确保任务在目标服务器上具有特权的用户帐户下运行。
来自Sysinternals PSTools的 psservice.exe
也将完成这项工作:
psservice \\server restart service
答案 1 :(得分:13)
描述: SC是用于与之通信的命令行程序 NT服务控制器和服务。 用法: sc [命令] [服务名称] ...
The option <server> has the form "\\ServerName"
Further help on commands can be obtained by typing: "sc [command]"
Commands:
query-----------Queries the status for a service, or
enumerates the status for types of services.
queryex---------Queries the extended status for a service, or
enumerates the status for types of services.
start-----------Starts a service.
pause-----------Sends a PAUSE control request to a service.
interrogate-----Sends an INTERROGATE control request to a service.
continue--------Sends a CONTINUE control request to a service.
stop------------Sends a STOP request to a service.
config----------Changes the configuration of a service (persistant).
description-----Changes the description of a service.
failure---------Changes the actions taken by a service upon failure.
qc--------------Queries the configuration information for a service.
qdescription----Queries the description for a service.
qfailure--------Queries the actions taken by a service upon failure.
delete----------Deletes a service (from the registry).
create----------Creates a service. (adds it to the registry).
control---------Sends a control to a service.
sdshow----------Displays a service's security descriptor.
sdset-----------Sets a service's security descriptor.
GetDisplayName--Gets the DisplayName for a service.
GetKeyName------Gets the ServiceKeyName for a service.
EnumDepend------Enumerates Service Dependencies.
The following commands don't require a service name:
sc <server> <command> <option>
boot------------(ok | bad) Indicates whether the last boot should
be saved as the last-known-good boot configuration
Lock------------Locks the Service Database
QueryLock-------Queries the LockStatus for the SCManager Database
实施例: sc start MyService
答案 2 :(得分:5)
如果它不需要人工交互,这意味着没有UI可以调用此操作,我认为它会在某个设置间隔重新启动?如果您有权访问计算机,则可以使用旧的NET STOP和NET START设置计划任务以执行批处理文件
net stop "DNS Client"
net start "DNS client"
或者如果您想要更复杂一些,可以试试Powershell
答案 3 :(得分:3)
There will be so many instances where the service will go in to "stop pending".The Operating system will complain that it was "Not able to stop the service xyz." In case you want to make absolutely sure the service is restarted you should kill the process instead. You can do that by doing the following in a bat file
taskkill /F /IM processname.exe
timeout 20
sc start servicename
To find out which process is associated with your service, go to task manager--> Services tab-->Right Click on your Service--> Go to process.
Note that this should be a work around until you figure out why your service had to be restarted in the first place. You should look for memory leaks, infinite loops and other such conditions for your service to have become unresponsive.
答案 4 :(得分:2)
查看sysinternals有关各种工具的信息,以帮助您实现这一目标。例如,psService将重新启动远程计算机上的服务。
答案 5 :(得分:1)
我推荐doofledorfer给出的方法。
如果您真的想通过直接API调用来实现,请查看OpenSCManager function。以下是获取机器名称和服务以及停止或启动它们的示例函数。
function ServiceStart(sMachine, sService : string) : boolean; //start service, return TRUE if successful
var schm, schs : SC_Handle;
ss : TServiceStatus;
psTemp : PChar;
dwChkP : DWord;
begin
ss.dwCurrentState := 0;
schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT); //connect to the service control manager
if(schm > 0)then begin // if successful...
schs := OpenService( schm,PChar(sService),SERVICE_START or SERVICE_QUERY_STATUS); // open service handle, start and query status
if(schs > 0)then begin // if successful...
psTemp := nil;
if (StartService(schs,0,psTemp)) and (QueryServiceStatus(schs,ss)) then
while(SERVICE_RUNNING <> ss.dwCurrentState)do begin
dwChkP := ss.dwCheckPoint; //dwCheckPoint contains a value incremented periodically to report progress of a long operation. Store it.
Sleep(ss.dwWaitHint); //Sleep for recommended time before checking status again
if(not QueryServiceStatus(schs,ss))then
break; //couldn't check status
if(ss.dwCheckPoint < dwChkP)then
Break; //if QueryServiceStatus didn't work for some reason, avoid infinite loop
end; //while not running
CloseServiceHandle(schs);
end; //if able to get service handle
CloseServiceHandle(schm);
end; //if able to get svc mgr handle
Result := SERVICE_RUNNING = ss.dwCurrentState; //if we were able to start it, return true
end;
function ServiceStop(sMachine, sService : string) : boolean; //stop service, return TRUE if successful
var schm, schs : SC_Handle;
ss : TServiceStatus;
dwChkP : DWord;
begin
schm := OpenSCManager(PChar(sMachine),nil,SC_MANAGER_CONNECT);
if(schm > 0)then begin
schs := OpenService(schm,PChar(sService),SERVICE_STOP or SERVICE_QUERY_STATUS);
if(schs > 0)then begin
if (ControlService(schs,SERVICE_CONTROL_STOP,ss)) and (QueryServiceStatus(schs,ss)) then
while(SERVICE_STOPPED <> ss.dwCurrentState) do begin
dwChkP := ss.dwCheckPoint;
Sleep(ss.dwWaitHint);
if(not QueryServiceStatus(schs,ss))then
Break;
if(ss.dwCheckPoint < dwChkP)then
Break;
end; //while
CloseServiceHandle(schs);
end; //if able to get svc handle
CloseServiceHandle(schm);
end; //if able to get svc mgr handle
Result := SERVICE_STOPPED = ss.dwCurrentState;
end;
答案 6 :(得分:1)
因此,重新启动您的服务,注意所有依赖项。
答案 7 :(得分:1)
从Powershell v3开始,PSsessions允许在远程计算机上运行任何本机cmdlet
$session = New-PSsession -Computername "YourServerName"
Invoke-Command -Session $Session -ScriptBlock {Restart-Service "YourServiceName"}
Remove-PSSession $Session
有关详细信息,请参阅here
答案 8 :(得分:0)
我相信PowerShell现在胜过&#34; sc&#34;命令就简单而言:
Restart-Service "servicename"
答案 9 :(得分:0)
这是我使用其他帐户远程重新启动服务时要做的事情
使用其他登录名打开CMD
runas /noprofile /user:DOMAIN\USERNAME cmd
使用SC停止和启动
sc \\SERVERNAME query Tomcat8
sc \\SERVERNAME stop Tomcat8
sc \\SERVERNAME start Tomcat8
答案 10 :(得分:0)
如果您尝试在不同域上的服务器上执行此操作,则所需的内容要比到目前为止大多数答案所建议的要多一些,这就是我用来完成此操作的方法:
#Configuration
$servername = "ABC",
$serviceAccountUsername = "XYZ",
$serviceAccountPassword = "XXX"
#Establish connection
try {
if (-not ([System.IO.Directory]::Exists('\\' + $servername))) {
net use \\$servername /user:$serviceAccountUsername $serviceAccountPassword
}
}
catch {
#May already exists, if so just continue
Write-Output $_.Exception.Message
}
#Restart Service
sc.exe \\$servername stop "ServiceNameHere"
sc.exe \\$servername start "ServiceNameHere"