質問

「net stop thingie」を使用してサービスを再起動するバッチスクリプトの最後に、サービスのステータスを知る必要があります。および「net start thingie」。

最も好きな理想の世界では、状態を自分にメールで送信し、寒い冬の夜に読んで、サーバーが正しく動作していることの暖かさと快適さで安心します。

ご存知のとおり、私はWindows Server 2003プラットフォームを使用しており、バッチファイルが最良の選択と思われました。私は他のものを使うことを気にせず、提案に非常に開かれているでしょうが、知識のためだけに(ゾンビが脳を渇望しているので、私は自分自身を膨らませてはいけないと思いました)、チェックすることができるコマンドがありますコマンドラインでのサービスのステータスについて

コマンドの出力をファイルにリダイレクトするだけですか?

私のズボンはどこにあるの? (ゴーシュ、これに挿入されたユーモアが誰をもin辱しないことを本当に願っています。水曜日の朝です。ユーモアも必要です:P)

[編集:]私が使用した解決策は--link redacted-

からダウンロードできます(もはやありません)。

夜間に実行されるように設定されたタスクとして使用され、午前中に電子メールをチェックして、サービスが正しく再起動されたかどうかを確認します。

役に立ちましたか?

解決

Windowsスクリプトの使用:

Set ComputerObj = GetObject("WinNT://MYCOMPUTER")    
ComputerObj.Filter = Array("Service")    
For Each Service in ComputerObj    
    WScript.Echo "Service display name = " & Service.DisplayName    
    WScript.Echo "Service account name = " & Service.ServiceAccountName    
    WScript.Echo "Service executable   = " & Service.Path    
    WScript.Echo "Current status       = " & Service.Status    
Next

必要な特定のサービスについて上記を簡単にフィルタリングできます。

他のヒント

sc.exe を試しましたか?

C:\> for /f "tokens=2*" %a in ('sc query audiosrv ^| findstr STATE') do echo %b
4  RUNNING

C:\> for /f "tokens=2*" %a in ('sc query sharedaccess ^| findstr STATE') do echo %b
1  STOPPED

バッチファイル内では、各パーセント記号を2重にすることに注意してください。

サービスで net start" service name" を呼び出すことができます。開始されていない場合は開始してerrorlevel = 0を返し、既に開始されている場合はerrorlevel = 2を返します。

pstools の使用-特に psservice および" query" -例:

psservice query "serviceName"

階層も見ます:

NET START | FIND"サービス名" >ヌル    IF errorlevel 1 ECHOサービスが実行されていません

コピー元: http://ss64.com/nt/sc.html

PowerShellを使用できる場合...

Get-Service -DisplayName *Network* | ForEach-Object{Write-Host 

PowerShellを使用できる場合...

Stopped : napagent
Stopped : NetDDE
Stopped : NetDDEdsdm
Running : Netman
Running : Nla
Stopped : WMPNetworkSvc
Stopped : xmlprov

お渡しします...

<*>

1つのサービスのみをチェックする必要がある場合は、**** Network ****を特定のサービス名に置き換えることができます。

.Status :

PowerShellを使用できる場合...

<*>

お渡しします...

<*>

1つのサービスのみをチェックする必要がある場合は、**** Network ****を特定のサービス名に置き換えることができます。

.Name}

お渡しします...

<*>

1つのサービスのみをチェックする必要がある場合は、**** Network ****を特定のサービス名に置き換えることができます。

たぶん、これはサービスを開始して結果を確認する最良の方法かもしれません

もちろん、File.BATのようなバッチ内からこの例のようなものを置きますが、単に&quot; NameOfSercive&quot;を置き換えます。希望するサービス名で、REM行を独自のコードに置き換えます。

@ECHO OFF

REM Put whatever your Batch may do before trying to start the service

net start NameOfSercive 2>nul
if errorlevel 2 goto AlreadyRunning
if errorlevel 1 goto Error

REM Put Whatever you want in case Service was not running and start correctly

GOTO ContinueWithBatch

:AlreadyRunning
REM Put Whatever you want in case Service was already running
GOTO ContinueWithBatch

:Error
REM Put Whatever you want in case Service fail to start
GOTO ContinueWithBatch

:ContinueWithBatch

REM Put whatever else your Batch may do

別のことは、変更せずに状態を確認することです。そのためには、もっと簡単な方法があります。実行するだけです:

net start

そのように、パラメータなしでは、開始されたすべてのサービスのリストが表示されます...

だから、単純なgrepか、パイプ上でそれを見つけると...

もちろん、File.BATのようなバッチ内からこの例のようなものを置きますが、単に&quot; NameOfSercive&quot;を置き換えます。希望するサービス名で、REM行を独自のコードに置き換えます。

@ECHO OFF

REM Put here any code to be run before check for Service

SET TemporalFile=TemporalFile.TXT
NET START | FIND /N "NameOfSercive" > %TemporalFile%
SET CountLines=0
FOR /F %%X IN (%TemporalFile%) DO SET /A CountLines=1+CountLines
IF 0==%CountLines% GOTO ServiceIsNotRunning

REM Put here any code to be run if Service Is Running

GOTO ContinueWithBatch

:ServiceIsNotRunning

REM Put here any code to be run if Service Is Not Running

GOTO ContinueWithBatch
:ContinueWithBatch
DEL -P %TemporalFile% 2>nul
SET TemporalFile=

REM Put here any code to be run after check for Service

これが役立つことを願っています!!私が普段使っているものです。

「ニック・カバディアス」を参照してください;これを伝える:

  

&quot;これによれば http://www.computerhope.com/nethlp.htm NET START / LIST ...&quot;

である必要があります

Windows XPでこれを入力する場合:

NET START /LIST

エラーが発生します。代わりに入力します

NET START

/ LISTはWindows 2000専用です...このようなWebを完全に読んだ場合、/ LISTはWindows 2000セクションのみにあります。

これが役立つことを願っています!!!

私の意図は、サービスのオンとオフを切り替えるスクリプトを作成することでした(1つのスクリプトで)

net start NameOfSercive 2>nul
if errorlevel 2 goto AlreadyRunning
if errorlevel 1 goto Error
     

...

大いに助けてくれました!! TYVM z666

ただし、たとえばサービスが無効になっている(errorlevel = 2?)

if errorlevel 1 goto Error  ?!!

iはその場合の出力が必要でした...

 :AlreadyRunning
 net stop NameOfSercive
 if errorlevel 1 goto Error


 :Error
 Echo ERROR!!1!
 Pause

私の2セント、これが役立つことを願っています

まあ、バッチファイルから結果をメールで送信できるかどうかはわかりません。問題のvbscriptを解決する別の提案をする場合。私はvbscriptにはあまり適していませんが、ローカルマシンで実行されているサービスを照会するためにそれを使用できます。以下のスクリプトは、スクリプトが実行されるマシンで実行されているすべてのサービスのステータスをメールで送信します。あなたは明らかにSMTPサーバーと電子メールアドレスを置き換えることになるでしょう。ドメインの一部であり、このスクリプトを特権ユーザーとして実行する場合(リモートマシンの管理者である必要があります)、localhostをfqdnに置き換えることにより、リモートマシンもクエリできます。

Dim objComputer, objMessage
Dim strEmail

' If there is an error getting the status of a service it will attempt to move on to the next one
On Error Resume Next

' Email Setup
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Service Status Report"
objMessage.From = "service_report@noreply.net"
objMessage.To = "youraddress@example.net"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.net"

'Server port (typically 25)
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

Set objComputer = GetObject("WinNT://localhost")
objComputer.Filter = Array("Service")

For Each aService In objComputer
strEmail = strEmail &chr(10) & aService.Name & "=" & aService.Status
Next

objMessage.TextBody = strEmail
objMessage.Configuration.Fields.Update
objMessage.Send

これがお役に立てば幸いです!お楽しみください!

編集:もう1つ、サービスステータス4はサービスが実行されていることを意味し、サービスステータス1はサービスが実行されていないことを意味します。 2または3が何を意味するのかわかりませんが、それらが停止/開始していることは間違いありません。

この http://www.computerhope.com/nethlp.htm に従ってNET START / LISTである必要がありますが、XPボックスで動作させることはできません。リストを提供するWMIがあると確信しています。

私が投稿したコードも、実行中のサービスの数を知るためのものです...

Oracle *のようなサービスの数を知りたい場合、NameOfSercive ...の代わりにOracleを配置し、変数%CountLines%で実行されているOracle *のようなサービスの数を取得します。このようなことができるのは4つだけです:

IF 4 ==%CountLines%GOTO FourServicesAreRunning

それははるかに強力です...そして、あなたのコードは、希望するサービスが実行されているかどうかを知らせません...同じ名前で始まる別のsreciveがある場合...想像してください: -ServiceOne -ServiceOnePersonal

ServiceOneを検索し、ServiceOnePersonalのみを実行している場合、コードはServiceOneが実行されていることを示します...

私のコードは簡単に変更できます。ファイルのすべての行を読み取り、行ごとに読み取るため、各サービスに必要なことを何でも行うことができます...これを参照してください:

@ECHO OFF
REM Put here any code to be run before check for Services

SET TemporalFile=TemporalFile.TXT
NET START > %TemporalFile%
SET CountLines=0
FOR /F "delims=" %%X IN (%TemporalFile%) DO SET /A CountLines=1+CountLines
SETLOCAL EnableDelayedExpansion
SET CountLine=0
FOR /F "delims=" %%X IN (%TemporalFile%) DO @(
 SET /A CountLine=1+CountLine

 REM Do whatever you want to each line here, remember first and last are special not service names

 IF 1==!CountLine! (

   REM Do whatever you want with special first line, not a service.

 ) ELSE IF %CountLines%==!CountLine! (

   REM Do whatever you want with special last line, not a service.

 ) ELSE (

   REM Do whatever you want with rest lines, for each service.
   REM    For example echo its position number and name:

   echo !CountLine! - %%X

   REM    Or filter by exact name (do not forget to not remove the three spaces at begining):
   IF "   NameOfService"=="%%X" (

     REM Do whatever you want with Service filtered.

   )
 )

 REM Do whatever more you want to all lines here, remember two first are special as last one

)

DEL -P %TemporalFile% 2>nul
SET TemporalFile=

REM Put here any code to be run after check for Services

もちろん、実行中のサービスのみを一覧表示しますが、ネットが実行中のサービスを一覧表示する方法はわかりません...

これが役立つことを願っています!!!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top