2009-04-29 122 views
21

我需要知道在使用「net stop thingie」和「net start thingie」重新啓動服務的批處理腳本末尾的服務狀態。Windows命令獲取服務狀態?

在我最喜歡的理想世界中,我想通過電子郵件將自己的狀態發送給自己,在寒冷的冬夜裏閱讀,讓自己放心,我知道服務器的溫暖和舒適,我知道它是正確的。

僅供大家知道,我使用的是Windows Server 2003平臺,批處理文件似乎是最佳選擇。我不介意使用別的東西,並且會很樂意提供建議,但僅僅是爲了知識(就像殭屍渴望大腦,我想,爲什麼不讓自己膨脹),是否有一個命令可以讓我檢查在服務的狀態,在命令行?

我應該只是將命令的輸出重定向到一個文件?

地獄是我的褲子? (天哪,我真的希望插入的幽默不會侮辱任何人。這是星期三早上,幽默我也需要:P)

[編輯:]我使用的解決方案是(不再提供)下載from --link redacted--

它用作在晚上執行的任務集,並在早上檢查我的電子郵件,看看服務是否已正確重新啓動。

回答

5

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

選擇,因爲不需要安裝。感謝你們! – 2009-04-29 12:36:36

12

使用pstools - 尤其是psservice和 「查詢」 - 例如:

psservice query "serviceName" 
+0

這是我最喜歡的^^ – 2014-02-25 06:33:08

26

你試過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 

請注意,在批處理文件中,每個百分號都會加倍。

19

您可以致電net start "service name"爲您服務。如果它沒有啓動,它會啓動它並返回errorlevel = 0,如果它已經啓動它將返回errorlevel = 2。

+0

不知道爲什麼我對此有一個downvote,但是因爲他使用bat文件來啓動net start命令的服務,並且它內置到命令中以返回錯誤代碼,所以最簡單的方法是簡單地檢查錯誤代碼。但是如果強制爲它創建一個單獨的vbs而不是檢查返回,我不在乎。 – JRL 2009-04-29 15:25:33

1

嗯,我不確定是否可以通過電子郵件發送批處理文件的結果。如果我可以提出一個可以解決你的問題的替代建議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是什麼意思,但我願意打賭他們停止/開始。

+0

非常感謝該腳本!將在我的批處理文件中使用它,以一種簡單高效的shell調用形式對vb腳本進行調用,這很容易由服務器處理:P – 2009-04-29 14:06:21

+0

與我選擇的其他腳本相結合,作爲一個很好的答案,我完成了我的停止/重新啓動/郵件結果循環。 (NET START | FIND「NameOfService」> nul)&& SET ServiceRunning = 1`或者GOTO替換爲「NET START」的 – 2009-04-29 14:07:45

9

如果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 

可以與特定服務替換**** ****網如果你只需要檢查一個服務的名稱。

10

也看起來更高:

NET START | FIND「服務名」> NUL IF ERRORLEVEL 1 ECHO服務沒有運行

從剛纔複製: http://ss64.com/nt/sc.html

+0

+,獲取正在運行的服務列表的方式並不明顯 – Fr0sT 2015-10-27 07:05:00

2

也許這可能是啓動服務和檢查結果

的最佳方式從像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 

希望這可以幫助!這是我通常使用的。

2

嗯,我看 「尼克Kavadias」 講述這件事:

「根據本http://www.computerhope.com/nethlp.htm應該是NET START/LIST ......」

如果您在Windows XP中此類型:

NET START /LIST 

,你會得到一個錯誤,只是類型,而不是

NET START 

/LIST僅適用於Windows 2000 ...如果您完全閱讀這樣的網頁,您將看到/ LIST僅在Windows 2000部分。

希望這有助於!

0

羅斯的代碼我張貼也可以知道有多少服務正在運行...

想象一下,你要知道有多少服務,如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 

當然它只能運行列表中的服務,我做不知道任何方式網可以列出不運行服務...

希望這有助於!

2

我的意圖是要創造出將業務切換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分,希望這有助於