我需要知道我的批处理脚本末尾的服务状态,它使用“net stop thingie”重新启动服务。和“net start thingie”。

在我最喜欢的理想世界中,我想通过电子邮件将状态发送给自己,在寒冷的冬夜里阅读,以确保我知道运行正常的服务器的温暖和舒适。

为了让您知道,我正在使用Windows Server 2003平台,批处理文件似乎是最佳选择。我不介意使用别的东西,并且会对建议非常开放,但仅仅是为了知识(因为僵尸渴望大脑,我想,为什么不给我自己充气),是否有一个命令允许我检查关于服务的状态,在命令行?

我应该将命令的输出重定向到文件吗?

到底哪里是我的裤子? (天哪,我真的希望插入这里的幽默不会侮辱任何人。这是星期三早上,幽默我也需要: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

请注意,在批处理文件中,您需要将每个百分号加倍。

您可以在服务上调用 net start" service name" 。如果它没有启动,它将启动它并返回errorlevel = 0,如果它已经启动它将返回errorlevel = 2.

使用 pstools - 特别是 psservice 和“query” - 例如:

psservice query "serviceName"

看起来也很高兴:

NET START |查找“服务名称” > NUL    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

会给你......

<*>

如果您只需要检查一项服务,可以将****网络****替换为特定的服务名称。

.Status :

如果您可以使用PowerShell ......

<*>

会给你......

<*>

如果您只需要检查一项服务,可以将****网络****替换为特定的服务名称。

.Name}

会给你......

<*>

如果您只需要检查一项服务,可以将****网络****替换为特定的服务名称。

也许这可能是启动服务并检查结果的最佳方式

当然,从像File.BAT这样的批处理内部放置类似于此示例的内容,但只需替换“NameOfSercive”即可。使用您想要的服务名称,并使用您自己的代码替换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这样的批处理内部放置类似于此示例的内容,但只需替换“NameOfSercive”即可。使用您想要的服务名称,并使用您自己的代码替换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

希望这可以帮助!!这是我通常使用的。

嗯,我看到“尼克卡瓦迪亚斯”告诉这个:

  

“根据此 http://www.computerhope.com/nethlp.htm 它应该是NET START / LIST ...“

如果您在Windows XP中键入:

NET START /LIST

您将收到错误,只需输入

即可
NET START

/ LIST仅适用于Windows 2000 ...如果您完全阅读此类网页,您会看到/ LIST仅适用于Windows 2000部分。

希望这有帮助!!!

我的目的是创建一个脚本,用于打开和关闭服务(在1个脚本中)

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

...

帮了很多!! TYVM z666

但是,例如服务被禁用(也是errorlevel = 2?)它进入“AlreadyRuning”并且永远不会来

if errorlevel 1 goto Error  ?!!

我想要那个案例的输出......

 :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

希望这对你有所帮助!享受!

编辑:啊,服务状态为4表示服务正在运行,服务状态为1表示不是。我不确定2或3是什么意思,但我愿意打赌他们正在停止/开始。

根据 http://www.computerhope.com/nethlp.htm 应该是NET START / LIST但是我无法通过XP box来解决它。我确定会有一些WMI会给你列表。

Ros我发布的代码也是为了知道正在运行的服务数量......

想象一下,你想知道有多少服务就像Oracle *那么你把Oracle放在而不是NameOfSercive ......并且你获得了在变量%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

当然它只列出正在运行的服务,我不知道net可以列出什么方式不运行服务...

希望这有帮助!!!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top