Simplest way to restart service on a remote computer

后端 未结 11 1968
我寻月下人不归
我寻月下人不归 2020-12-23 13:07

What\'s the easiest programmatic way to restart a service on a remote Windows system? Language or method doesn\'t matter as long as it doesn\'t require human interaction.

11条回答
  •  醉酒成梦
    2020-12-23 14:07

    I recommend the method given by doofledorfer.

    If you really want to do it via a direct API call, then look at the OpenSCManager function. Below are sample functions to take a machine name and service, and stop or start them.

    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;
    

提交回复
热议问题