Windows命令获取服务状态?
我需要知道在使用“net stop thingie”和“net start thingie”重新启动服务的批处理脚本末尾的服务状态。Windows命令获取服务状态?
在我最喜欢的理想世界中,我想通过电子邮件将自己的状态发送给自己,在寒冷的冬夜里阅读,让自己放心,我知道服务器的温暖和舒适,我知道它是正确的。
仅供大家知道,我使用的是Windows Server 2003平台,批处理文件似乎是最佳选择。我不介意使用别的东西,并且会很乐意提供建议,但仅仅是为了知识(就像僵尸渴望大脑,我想,为什么不让自己膨胀),是否有一个命令可以让我检查在服务的状态,在命令行?
我应该只是将命令的输出重定向到一个文件?
地狱是我的裤子? (天哪,我真的希望插入的幽默不会侮辱任何人。这是星期三早上,幽默我也需要:P)
[编辑:]我使用的解决方案是(不再提供)下载from --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。
不知道为什么我对此有一个downvote,但是因为他使用bat文件来启动net start命令的服务,并且它内置到命令中以返回错误代码,所以最简单的方法是简单地检查错误代码。但是如果强制为它创建一个单独的vbs而不是检查返回,我不在乎。 – JRL 2009-04-29 15:25:33
嗯,我不确定是否可以通过电子邮件发送批处理文件的结果。如果我可以提出一个可以解决你的问题的替代建议vbscript。我用vbscript很不擅长,但你可以用它来查询本地机器上运行的服务。下面的脚本会通过电子邮件向您发送运行脚本的计算机上运行的所有服务的状态。你显然想要替换smtp服务器和电子邮件地址。如果您是域的一部分,并且以特权用户身份运行此脚本(它们必须是远程计算机上的管理员),那么您也可以通过使用fqdn替换localhost来查询远程计算机。
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 = "[email protected]"
objMessage.To = "[email protected]"
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是什么意思,但我愿意打赌他们停止/开始。
非常感谢该脚本!将在我的批处理文件中使用它,以一种简单高效的shell调用形式对vb脚本进行调用,这很容易由服务器处理:P – 2009-04-29 14:06:21
与我选择的其他脚本相结合,作为一个很好的答案,我完成了我的停止/重新启动/邮件结果循环。 (NET START | FIND“NameOfService”> nul)&& SET ServiceRunning = 1`或者GOTO替换为“NET START”的 – 2009-04-29 14:07:45
如果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
可以与特定服务替换**** ****网如果你只需要检查一个服务的名称。
也许这可能是启动服务和检查结果
的最佳方式从像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
希望这可以帮助!这是我通常使用的。
嗯,我看 “尼克Kavadias” 讲述这件事:
“根据本http://www.computerhope.com/nethlp.htm应该是NET START/LIST ......”
如果您在Windows XP中此类型:
NET START /LIST
,你会得到一个错误,只是类型,而不是
NET START
/LIST仅适用于Windows 2000 ...如果您完全阅读这样的网页,您将看到/ LIST仅在Windows 2000部分。
希望这有助于!
罗斯的代码我张贴也可以知道有多少服务正在运行...
想象一下,你要知道有多少服务,如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
当然它只能运行列表中的服务,我做不知道任何方式网可以列出不运行服务...
希望这有助于!
我的意图是要创造出将业务切换ON和OFF(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 ?!!
我想对于这种情况下的输出...
我的2分,希望这有助于
选择,因为不需要安装。感谢你们! – 2009-04-29 12:36:36