リモート Windows システムでサービスを再起動する最も簡単なプログラムによる方法は何ですか? 人間の介入を必要としない限り、言語や方法は問題ではありません。
11 に答える
Windows XP では、 を使用sc.exe
してローカルおよびリモート サービスと対話できます。次のようなバッチ ファイルを実行するタスクをスケジュールします。
sc \\サーバー停止サービス sc \\サーバー開始サービス
ターゲット サーバーで特権を持つユーザー アカウントでタスクが実行されていることを確認します。
psservice.exe
Sysinternals PSToolsからも仕事をしています:
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を使用して、スケジュールされたタスクを設定してバッチファイルを実行できます
net stop "DNS Client"
net start "DNS client"
または、もう少し洗練されたい場合は、Powershellを試すことができます
Powershell v3 の時点で、PSsessions を使用すると、任意のネイティブ コマンドレットをリモート マシンで実行できます。
$session = New-PSsession -Computername "YourServerName"
Invoke-Command -Session $Session -ScriptBlock {Restart-Service "YourServiceName"}
Remove-PSSession $Session
詳しくはこちら
doofledorferの方法をお勧めします。
直接API呼び出しを介してそれを本当に実行したい場合は、OpenSCManager関数を見てください。以下は、マシン名とサービスを取得し、それらを停止または開始するためのサンプル関数です。
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;
その目標を達成するのに役立つさまざまなツールについては、sysinternalsを参照してください。たとえば、psService は、リモート マシンでサービスを再起動します。
- openscmanagerを使用してサービスコントロールマネージャーデータベースを開く
- EnumDependService()を使用して依存サービスを取得する
- ChangeConfig()を使用してすべての依存サービスを停止し、開始された場合はこの関数にSTOP信号を送信します
- 実際のサービスを停止する
- サービスのすべてのサービス依存関係を取得する
- 停止している場合は、StartService()を使用してすべてのサービス依存関係を開始します
- 実際のサービスを開始する
したがって、すべての依存関係に注意してサービスが再起動されます。
シンプルさの点で、PowerShell は "sc" コマンドよりも優れていると思います。
Restart-Service "servicename"