代码之家  ›  专栏  ›  技术社区  ›  Olivier Tremblay

获取服务状态的Windows命令?

  •  21
  • Olivier Tremblay  · 技术社区  · 16 年前

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

    在我最喜欢的理想世界里,我想用电子邮件把这个国家告诉我自己,在寒冷的冬夜里读书,用我知道运行正常的服务器的温暖和舒适来安慰我自己。

    只是让你知道,我使用的是WindowsServer2003平台,批处理文件似乎是最好的选择。我不介意使用其他东西,并且会非常乐于接受建议,但是仅仅是为了知识(僵尸渴望大脑,我想,为什么不膨胀自己的大脑),是否有一个命令允许我在命令行中检查服务的状态?

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

    我的裤子到底在哪里?(天哪,我真的希望这里面的幽默不会侮辱任何人。今天是星期三早上,我也需要幽默:p)

    [编辑:]i使用的解决方案(不再)可从--link redacted下载--

    它被用作一个在夜间执行的任务集,在早上检查我的电子邮件时,我会查看服务是否正确地重新启动。

    12 回复  |  直到 12 年前
        1
  •  5
  •   OregonGhost    16 年前

    使用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
    

    您可以为您想要的特定服务轻松筛选上述内容。

        2
  •  26
  •   Rômulo Ceccon    16 年前

    你试过了吗? 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
    

    注意,在批处理文件中,每个百分号都要加倍。

        3
  •  19
  •   JRL    16 年前

    你可以打电话 net start "service name" 为您服务。如果没有启动,它将启动并返回错误级别=0;如果已经启动,它将返回错误级别=2。

        4
  •  12
  •   Marc Gravell    16 年前

    使用 pstools -特别是 psservice 和“查询”—例如:

    psservice query "serviceName"
    
        5
  •  10
  •   Ros    14 年前

    还看HIER:

    net start查找“服务名称”>nul 如果ErrorLevel 1 Echo服务未运行

    复制自: http://ss64.com/nt/sc.html

        6
  •  9
  •   aphoria    16 年前

    如果PowerShell对您可用…

    Get-Service -DisplayName *Network* | ForEach-Object{Write-Host $_.Status : $_.Name}
    

    会给你…

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

    如果只需要检查一个服务,可以用特定的服务名称替换****网络****

        7
  •  2
  •   z666zz666z    14 年前

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

    当然,在一个类似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
    

    希望这能有所帮助!!这是我通常使用的。

        8
  •  2
  •   z666zz666z    14 年前

    我看到“尼克·卡瓦迪亚斯”这样说:

    “根据这个 http://www.computerhope.com/nethlp.htm 它应该是net start/list…”

    如果在Windows XP中键入以下内容:

    NET START /LIST
    

    您将得到一个错误,只需键入

    NET START
    

    /列表仅适用于Windows 2000…如果您完全阅读这样的网站,您将看到/列表仅在Windows 2000部分。

    希望这有帮助!!!!

        9
  •  2
  •   xy77    14 年前

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

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

    帮了很多忙!TYVM Z666!

    但是,当服务被禁用时(同样错误级别=2?)它会进入“准备好了”并且永远不会出现

    if errorlevel 1 goto Error  ?!!
    

    我想要那个案子的结果…

     :AlreadyRunning
     net stop NameOfSercive
     if errorlevel 1 goto Error
    
    
     :Error
     Echo ERROR!!1!
     Pause
    

    我的2美分,希望这有帮助

        10
  •  1
  •   Sam Stelfox    16 年前

    好吧,我不确定你是否可以从一个批处理文件中通过电子邮件发送结果。如果我可以提出另一个建议来解决您的问题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是什么意思,但我敢打赌他们会停止/开始。

        11
  •  0
  •   Nick Kavadias    16 年前

    根据这个 http://www.computerhope.com/nethlp.htm 它应该是net start/list,但我不能让它在xp box上工作。我相信有一些WMI会给你列表。

        12
  •  0
  •   z666zz666z    14 年前

    ROS我发布的代码也用于知道有多少服务正在运行…

    假设您想知道有多少服务与Oracle*类似,那么您将Oracle而不是命名为Sercive…您可以在变量%countlines%上运行Oracle*这样的服务,如果您想做一些事情,如果只有4个,您可以这样做:

    如果4=%countlines%转到四个服务正在运行

    更强大的是…您的代码不会让您知道所需的服务是否正在运行…如果有另一个同名的目录…想象: - ServiceOne -服务一个人

    如果您搜索ServiceOne,但它只运行ServiceOne个人,您的代码将告诉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
    

    当然,它只列出运行中的服务,我不知道网络可以列出不运行的服务的任何方式…

    希望这有帮助!!!!