在远程计算机上重新启动服务最简单的方法

什么是在远程Windows系统上重新启动服务的最简单的编程方式? 语言或方法无关紧要,只要不需要人际交往即可。

从Windows XP开始,可以使用sc.exe与本地和远程服务进行交互。 安排一个任务来运行一个类似如下的batch file:

 sc \\服务器停止服务
 sc \\服务器启动服务

确保任务在目标服务器上的特权用户帐户下运行。

Sysinternals PSTools中的 psservice.exe也可以做这个工作:

 psservice \\服务器重新启动服务

说明: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

如果它不需要人机交互,这意味着将没有UI调用这个操作,我假定它会以某个设定的时间间隔重新启动? 如果您有权访问计算机,则可以使用旧的NET STOP和NET START来设置计划任务以执行batch file

 net stop "DNS Client" net start "DNS client" 

或者如果你想要更复杂一些,你可以试试Powershell

将有这么多的服务将进入“停止等待”的情况。操作系统会抱怨说,“不能停止服务xyz”。 如果你想确保服务重新启动,你应该杀死这个进程。 您可以通过在bat文件中执行以下操作来完成此操作

 taskkill /F /IM processname.exe timeout 20 sc start servicename 

要找出哪个进程与您的服务相关联,请转到任务pipe理器 – >服务选项卡 – >右键单击您的服务 – >转到进程。

请注意,这应该是一个工作,直到你找出为什么你的服务必须重新启动。 你应该寻找内存泄漏,无限循环和其他这样的条件,使你的服务变得没有反应。

看看sysinternals的各种工具来帮助你实现这个目标。 例如,psService将重新启动远程机器上的服务。

我推荐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; 
  1. 使用openscmanager打开服务控制pipe理器数据库
  2. 使用EnumDependService()获得依赖服务
  3. 使用ChangeConfig()停止所有相关的服务,如果这些服务启动,则向这个函数发送STOP信号
  4. 停止实际的服务
  5. 获取服务的所有服务依赖关系
  6. 如果停止,则使用StartService()启动所有服务依赖项
  7. 开始实际的服务

因此,您的服务重新开始照顾所有的依赖关系。

从Powershell v3起,PSsessions允许任何本地cmdlet在远程机器上运行

 $session = New-PSsession -Computername "YourServerName" Invoke-Command -Session $Session -ScriptBlock {Restart-Service "YourServiceName"} Remove-PSSession $Session 

在这里看到更多的信息

我相信PowerShell现在在简单性方面胜过“sc”命令:

 Restart-Service "servicename"