Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Need to check for running services

HTVi
TV Discussion Newsgroups

Need to check for running services
"Steve Gould" <steven.gould at seattle.gov> 6/14/2007 10:22:45 PM
I am trying to devise a script that will run against a remote server to
check the status of Services that are set to start on boot (automatic). I
have been looking at the sc.exe utility. It looks promising using the query
or queryex switch, but it only shows the running services.

Actually what I could use is a script that checks for boot services that are
NOT running (stopped). I need to run this against over 50 servers at regular
intervals...

Suggestions anyone?

Steve



RE: Need to check for running services
J Ford 6/15/2007 1:46:01 PM
Please account for word wrap... here is a sub routine where you could pass
machine names to.

<script begin>

EnumServices "." '<- Pass names here

Sub EnumServices(sMachineName)
On Error Resume Next

Set objWMIService = GetObject("winmgmts:\\" & sMachineName & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name, StartMode,
State, Status FROM Win32_Service WHERE StartMode='Auto'")

For Each objItem in colItems
Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
objItem.Status
Next

Set objWMIService = Nothing
Set colItems = Nothing
End Sub
</script end>

"Steve Gould" wrote:

[Quoted Text]
> I am trying to devise a script that will run against a remote server to
> check the status of Services that are set to start on boot (automatic). I
> have been looking at the sc.exe utility. It looks promising using the query
> or queryex switch, but it only shows the running services.
>
> Actually what I could use is a script that checks for boot services that are
> NOT running (stopped). I need to run this against over 50 servers at regular
> intervals...
>
> Suggestions anyone?
>
> Steve
>
>
>
>
Re: Need to check for running services
"Steve Gould" <steven.gould at seattle.gov> 6/15/2007 9:46:43 PM
OK, I am closer. Here is the script modified to attempt to pull computer
names from a text file. I get a vb error on line 10,1. Suggestions anyone?

INPUT_FILE_NAME = "C:\List.txt"
Const FOR_READING = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
For Each strComputer In arrComputers
EnumServices strComputer '<- Pass names here
Sub EnumServices(sMachineName)
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & sMachineName & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name, StartMode,
State, Status FROM Win32_Service WHERE StartMode='Auto' AND
State='Stopped'")
For Each objItem in colItems
Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
objItem.Status
Next
Set objWMIService = Nothing
Set colItems = Nothing
End Sub
Next


"J Ford" <JFord[ at ]discussions.microsoft.com> wrote in message
news:FA842703-16E1-4EE0-ADF1-BAB5E3068F39[ at ]microsoft.com...
[Quoted Text]
> Please account for word wrap... here is a sub routine where you could pass
> machine names to.
>
> <script begin>
>
> EnumServices "." '<- Pass names here
>
> Sub EnumServices(sMachineName)
> On Error Resume Next
>
> Set objWMIService = GetObject("winmgmts:\\" & sMachineName &
> "\root\cimv2")
> Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name,
> StartMode,
> State, Status FROM Win32_Service WHERE StartMode='Auto'")
>
> For Each objItem in colItems
> Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
> objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
> objItem.Status
> Next
>
> Set objWMIService = Nothing
> Set colItems = Nothing
> End Sub
> </script end>
>
> "Steve Gould" wrote:
>
>> I am trying to devise a script that will run against a remote server to
>> check the status of Services that are set to start on boot (automatic). I
>> have been looking at the sc.exe utility. It looks promising using the
>> query
>> or queryex switch, but it only shows the running services.
>>
>> Actually what I could use is a script that checks for boot services that
>> are
>> NOT running (stopped). I need to run this against over 50 servers at
>> regular
>> intervals...
>>
>> Suggestions anyone?
>>
>> Steve
>>
>>
>>
>>


Re: Need to check for running services
J Ford 6/15/2007 10:07:02 PM
On your "for each" statement you are missing the "next"

For Each strComputer In arrComputers
EnumServices strComputer '<- Pass names here
Next

"Steve Gould" wrote:

[Quoted Text]
> OK, I am closer. Here is the script modified to attempt to pull computer
> names from a text file. I get a vb error on line 10,1. Suggestions anyone?
>
> INPUT_FILE_NAME = "C:\List.txt"
> Const FOR_READING = 1
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
> strComputers = objFile.ReadAll
> objFile.Close
> arrComputers = Split(strComputers, vbCrLf)
> For Each strComputer In arrComputers
> EnumServices strComputer '<- Pass names here
> Sub EnumServices(sMachineName)
> On Error Resume Next
> Set objWMIService = GetObject("winmgmts:\\" & sMachineName & "\root\cimv2")
> Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name, StartMode,
> State, Status FROM Win32_Service WHERE StartMode='Auto' AND
> State='Stopped'")
> For Each objItem in colItems
> Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
> objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
> objItem.Status
> Next
> Set objWMIService = Nothing
> Set colItems = Nothing
> End Sub
> Next
>
>
> "J Ford" <JFord[ at ]discussions.microsoft.com> wrote in message
> news:FA842703-16E1-4EE0-ADF1-BAB5E3068F39[ at ]microsoft.com...
> > Please account for word wrap... here is a sub routine where you could pass
> > machine names to.
> >
> > <script begin>
> >
> > EnumServices "." '<- Pass names here
> >
> > Sub EnumServices(sMachineName)
> > On Error Resume Next
> >
> > Set objWMIService = GetObject("winmgmts:\\" & sMachineName &
> > "\root\cimv2")
> > Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name,
> > StartMode,
> > State, Status FROM Win32_Service WHERE StartMode='Auto'")
> >
> > For Each objItem in colItems
> > Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
> > objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
> > objItem.Status
> > Next
> >
> > Set objWMIService = Nothing
> > Set colItems = Nothing
> > End Sub
> > </script end>
> >
> > "Steve Gould" wrote:
> >
> >> I am trying to devise a script that will run against a remote server to
> >> check the status of Services that are set to start on boot (automatic). I
> >> have been looking at the sc.exe utility. It looks promising using the
> >> query
> >> or queryex switch, but it only shows the running services.
> >>
> >> Actually what I could use is a script that checks for boot services that
> >> are
> >> NOT running (stopped). I need to run this against over 50 servers at
> >> regular
> >> intervals...
> >>
> >> Suggestions anyone?
> >>
> >> Steve
> >>
> >>
> >>
> >>
>
>
>
Re: Need to check for running services
"B-Mann" <RemoveSpaces(b __ m a n n [ at ] h o t m a i l.c o m)> 6/16/2007 2:21:29 AM

"J Ford" <JFord[ at ]discussions.microsoft.com> wrote in message
news:FA842703-16E1-4EE0-ADF1-BAB5E3068F39[ at ]microsoft.com...
[Quoted Text]
> Please account for word wrap... here is a sub routine where you could pass
> machine names to.
>
> <script begin>
>
> EnumServices "." '<- Pass names here
>
> Sub EnumServices(sMachineName)
> On Error Resume Next
>
> Set objWMIService = GetObject("winmgmts:\\" & sMachineName &
> "\root\cimv2")
> Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name,
> StartMode,
> State, Status FROM Win32_Service WHERE StartMode='Auto'")
>
> For Each objItem in colItems
> Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
> objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
> objItem.Status
> Next
>
> Set objWMIService = Nothing
> Set colItems = Nothing
> End Sub
> </script end>
>
> "Steve Gould" wrote:
>
>> I am trying to devise a script that will run against a remote server to
>> check the status of Services that are set to start on boot (automatic). I
>> have been looking at the sc.exe utility. It looks promising using the
>> query
>> or queryex switch, but it only shows the running services.
>>
>> Actually what I could use is a script that checks for boot services that
>> are
>> NOT running (stopped). I need to run this against over 50 servers at
>> regular
>> intervals...
>>
>> Suggestions anyone?
>>
>> Steve
>>

If you wish to use sc, here is a batch file that will provide the
information
you are looking for:

::: Start ofbBatch

:: Set the srvlst variable to a text file containing the list of servers.
set srvlst=.\srvrlist.txt

if not exist "%srvlst%" goto end
if exist "%temp%\inactive.lst" del "%temp%\inactive.lst"
if exist "%temp%\startype.lst" del "%temp%\startype.lst"

for /f "tokens=1" %%s in (%srvlst%) do (
sc \\%%s query state= inactive|find /i
"Service_Name">%temp%\inactive.lst
for /f "tokens=2 delims=:" %%x in (%temp%\inactive.lst) do (
setlocal enabledelayedexpansion
set tmpsvc=%%x
set tmpsvc=!tmpsvc:~1!
echo Start_Type: !tmpsvc!>>%temp%\startype.lst
sc \\%%s qc "!tmpsvc!"|find /i "Start_Type">>%temp%\startype.lst
endlocal)

if exist %temp%\startype.lst (
set b=0
setlocal enabledelayedexpansion
for /f "tokens=2 delims=:" %%x in (%temp%\startype.lst) do (
set /a b=b^^1
set tmpsvc=%%x
set tmpsvc=!tmpsvc:~1!
if [!b!]==[1] set svcname=!tmpsvc!
if [!b!]==[0] (
set tmpsvc=!tmpsvc:~0,1!
if [!tmpsvc!]==[2] echo %%s: !svcname! is STOPPED))
endlocal)

if exist "%temp%\inactive.lst" del "%temp%\inactive.lst"
if exist "%temp%\startype.lst" del "%temp%\startype.lst")
:end

:::: End of batch


B-Mann


Re: Need to check for running services
"Steve Gould" <steven.gould at seattle.gov> 6/18/2007 3:13:26 PM
Thanks. I had my next statement at the wrong place. Your correction worked.


Thank you for all your help.

Steve

"J Ford" <JFord[ at ]discussions.microsoft.com> wrote in message
news:262D2B17-09E7-49B4-8627-63998FBE0209[ at ]microsoft.com...
[Quoted Text]
> On your "for each" statement you are missing the "next"
>
> For Each strComputer In arrComputers
> EnumServices strComputer '<- Pass names here
> Next
>
> "Steve Gould" wrote:
>
>> OK, I am closer. Here is the script modified to attempt to pull computer
>> names from a text file. I get a vb error on line 10,1. Suggestions
>> anyone?
>>
>> INPUT_FILE_NAME = "C:\List.txt"
>> Const FOR_READING = 1
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
>> strComputers = objFile.ReadAll
>> objFile.Close
>> arrComputers = Split(strComputers, vbCrLf)
>> For Each strComputer In arrComputers
>> EnumServices strComputer '<- Pass names here
>> Sub EnumServices(sMachineName)
>> On Error Resume Next
>> Set objWMIService = GetObject("winmgmts:\\" & sMachineName &
>> "\root\cimv2")
>> Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name,
>> StartMode,
>> State, Status FROM Win32_Service WHERE StartMode='Auto' AND
>> State='Stopped'")
>> For Each objItem in colItems
>> Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
>> objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
>> objItem.Status
>> Next
>> Set objWMIService = Nothing
>> Set colItems = Nothing
>> End Sub
>> Next
>>
>>
>> "J Ford" <JFord[ at ]discussions.microsoft.com> wrote in message
>> news:FA842703-16E1-4EE0-ADF1-BAB5E3068F39[ at ]microsoft.com...
>> > Please account for word wrap... here is a sub routine where you could
>> > pass
>> > machine names to.
>> >
>> > <script begin>
>> >
>> > EnumServices "." '<- Pass names here
>> >
>> > Sub EnumServices(sMachineName)
>> > On Error Resume Next
>> >
>> > Set objWMIService = GetObject("winmgmts:\\" & sMachineName &
>> > "\root\cimv2")
>> > Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name,
>> > StartMode,
>> > State, Status FROM Win32_Service WHERE StartMode='Auto'")
>> >
>> > For Each objItem in colItems
>> > Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
>> > objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
>> > objItem.Status
>> > Next
>> >
>> > Set objWMIService = Nothing
>> > Set colItems = Nothing
>> > End Sub
>> > </script end>
>> >
>> > "Steve Gould" wrote:
>> >
>> >> I am trying to devise a script that will run against a remote server
>> >> to
>> >> check the status of Services that are set to start on boot
>> >> (automatic). I
>> >> have been looking at the sc.exe utility. It looks promising using the
>> >> query
>> >> or queryex switch, but it only shows the running services.
>> >>
>> >> Actually what I could use is a script that checks for boot services
>> >> that
>> >> are
>> >> NOT running (stopped). I need to run this against over 50 servers at
>> >> regular
>> >> intervals...
>> >>
>> >> Suggestions anyone?
>> >>
>> >> Steve
>> >>
>> >>
>> >>
>> >>
>>
>>
>>


Re: Need to check for running services
"Steve Gould" <steven.gould at seattle.gov> 6/18/2007 3:14:09 PM
Thank you.

Steve

"B-Mann" <RemoveSpaces(b __ m a n n [ at ] h o t m a i l.c o m)> wrote in message
news:uNMFP07rHHA.1864[ at ]TK2MSFTNGP04.phx.gbl...
[Quoted Text]
>
> "J Ford" <JFord[ at ]discussions.microsoft.com> wrote in message
> news:FA842703-16E1-4EE0-ADF1-BAB5E3068F39[ at ]microsoft.com...
>> Please account for word wrap... here is a sub routine where you could
>> pass
>> machine names to.
>>
>> <script begin>
>>
>> EnumServices "." '<- Pass names here
>>
>> Sub EnumServices(sMachineName)
>> On Error Resume Next
>>
>> Set objWMIService = GetObject("winmgmts:\\" & sMachineName &
>> "\root\cimv2")
>> Set colItems = objWMIService.ExecQuery("SELECT DisplayName, Name,
>> StartMode,
>> State, Status FROM Win32_Service WHERE StartMode='Auto'")
>>
>> For Each objItem in colItems
>> Wscript.Echo sMachineName & "," & objItem.DisplayName & "," &
>> objItem.Name & "," & objItem.StartMode & "," & objItem.State & "," &
>> objItem.Status
>> Next
>>
>> Set objWMIService = Nothing
>> Set colItems = Nothing
>> End Sub
>> </script end>
>>
>> "Steve Gould" wrote:
>>
>>> I am trying to devise a script that will run against a remote server to
>>> check the status of Services that are set to start on boot (automatic).
>>> I
>>> have been looking at the sc.exe utility. It looks promising using the
>>> query
>>> or queryex switch, but it only shows the running services.
>>>
>>> Actually what I could use is a script that checks for boot services that
>>> are
>>> NOT running (stopped). I need to run this against over 50 servers at
>>> regular
>>> intervals...
>>>
>>> Suggestions anyone?
>>>
>>> Steve
>>>
>
> If you wish to use sc, here is a batch file that will provide the
> information
> you are looking for:
>
> ::: Start ofbBatch
>
> :: Set the srvlst variable to a text file containing the list of servers.
> set srvlst=.\srvrlist.txt
>
> if not exist "%srvlst%" goto end
> if exist "%temp%\inactive.lst" del "%temp%\inactive.lst"
> if exist "%temp%\startype.lst" del "%temp%\startype.lst"
>
> for /f "tokens=1" %%s in (%srvlst%) do (
> sc \\%%s query state= inactive|find /i
> "Service_Name">%temp%\inactive.lst
> for /f "tokens=2 delims=:" %%x in (%temp%\inactive.lst) do (
> setlocal enabledelayedexpansion
> set tmpsvc=%%x
> set tmpsvc=!tmpsvc:~1!
> echo Start_Type: !tmpsvc!>>%temp%\startype.lst
> sc \\%%s qc "!tmpsvc!"|find /i "Start_Type">>%temp%\startype.lst
> endlocal)
>
> if exist %temp%\startype.lst (
> set b=0
> setlocal enabledelayedexpansion
> for /f "tokens=2 delims=:" %%x in (%temp%\startype.lst) do (
> set /a b=b^^1
> set tmpsvc=%%x
> set tmpsvc=!tmpsvc:~1!
> if [!b!]==[1] set svcname=!tmpsvc!
> if [!b!]==[0] (
> set tmpsvc=!tmpsvc:~0,1!
> if [!tmpsvc!]==[2] echo %%s: !svcname! is STOPPED))
> endlocal)
>
> if exist "%temp%\inactive.lst" del "%temp%\inactive.lst"
> if exist "%temp%\startype.lst" del "%temp%\startype.lst")
> :end
>
> :::: End of batch
>
>
> B-Mann
>


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen