Logon script to check if these services are running.

Hi,

How with a logon script i can find if the services are running.
Ex:
Remote registry
Windows firewall.

Etc.

Regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
 
chandru_solCommented:
Try this............

On Error Resume Next
Const ForReading = 1

'Outputfile

strwritefile = "c:\results.txt"

'old file will be deleted
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objfso.FileExists(strwritefile) Then
    objfso.DeleteFile strwritefile, True
End if

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\Scripts\Computers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputer = Split(strText, VbCrLf)

Results = ""

ArrServices = Array("Windows Firewall/Internet Connection Sharing (ICS)", "Remote Registry","Windows Management Instrumentation",_
"Computer Browser","DHCPClient","Sophos Agent")
i = 0
For Each strComputer In ArrComputer
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        For Each Service In ArrServices
            If i > 0 Then
                str = str & " or DisplayName = '" & Service & "'"
            Else
                str = "DisplayName = '" & Service & "'"
                i = i + 1
            End If
        Next
        Set colItems = objWMIService.ExecQuery("Select DisplayName, State from Win32_Service where " & str)
        For Each objItem in colItems
          Results = Results & strcomputer & " " & objItem.DisplayName & " "  & objItem.State & VbCrLf
            'Wscript.echo Results
       Next

      Set objoutputFile = objfso.openTextFile(strwritefile,8, True)
      objoutputFile.writeline Results & VbCrLf
      objoutputFile.close

Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing
Next

Wscript.echo "Completed"
Wscript.quit
0
 
chandru_solCommented:
Hi Sharatu,

Do you want to use it as a long script for users or Computers?

What is that you want to do if the service is running or if not running?

regards
Chandru
0
 
bsharathAuthor Commented:
For Computer.
Just need to show if the service is enabled or disabled.
If possible if disabled then we can enable it.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
chandru_solCommented:
Hi,

Can you try the below code?

ArrComputer = Array(".")
ArrServices = Array("SharedAccess", "Remote Registry")
i = 0
For Each strComputer In ArrComputer
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        For Each Service In ArrServices
            If i > 0 Then
                str = str & " or DisplayName = '" & Service & "'"
            Else
                str = "DisplayName = '" & Service & "'"
                i = i + 1
            End If
        Next
        Set colItems = objWMIService.ExecQuery("Select DisplayName, State from Win32_Service where " & str)
        For Each objItem in colItems
            Wscript.echo "DisplayName: "  & objItem.DisplayName
            Wscript.echo "State: "  & objItem.State & VbCrLf
       Next
Next

regards
Chandru
0
 
chandru_solCommented:
Sharath,

Did you try this script?
0
 
bsharathAuthor Commented:
Chanru..
Should i edit these 2 lines as
Wscript.echo "DisplayName: "  & objItem.DisplayName
            Wscript.echo "State: "  & objItem.State & VbCrLf

What should i put in State
0
 
chandru_solCommented:
Sharath,
Do you want the output to a text file in the local machine?

When you run this on your machine can you let me know what is result you see
0
 
bsharathAuthor Commented:
Ok Chandru,

When i run on my machine i get a popup saying Remote registry running.
Can you add say 4 more services that i need to check on a list of machines and get the results to a file.
0
 
chandru_solCommented:
Can you let me know the other services?

Do you want the output to be on the local machine? As this is a login script the results will be created on the local machine?
0
 
bsharathAuthor Commented:
Windows Management Instrumentation
Remote Registry
Computer Browser
DHCP Client
Sophos Agent
Can you add these 5 services if They are on of off (Started or Stopped) No action should be performed but a results file that can results in the machine i run it from.
Can you change it from logon script to run on the machine i have in a Computers.txt file.
0
 
chandru_solCommented:
Can you try the script below?

On Error Resume Next
Const ForReading = 1

'Outputfile

strwritefile = "c:\results.txt"

'old file will be deleted
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objfso.FileExists(strwritefile) Then
    objfso.DeleteFile strwritefile, True
End if

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\Scripts\Computers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputer = Split(strText, VbCrLf)

Results = ""

ArrServices = Array("Windows Firewall/Internet Connection Sharing (ICS)", "Remote Registry","Windows Management Instrumentation
","Computer Browser","DHCP Client","Sophos Agent")
i = 0
For Each strComputer In ArrComputer
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        For Each Service In ArrServices
            If i > 0 Then
                str = str & " or DisplayName = '" & Service & "'"
            Else
                str = "DisplayName = '" & Service & "'"
                i = i + 1
            End If
        Next
        Set colItems = objWMIService.ExecQuery("Select DisplayName, State from Win32_Service where " & str)
        For Each objItem in colItems
          Results = Results & strcomputer & " " & objItem.DisplayName & " "  & objItem.State & VbCrLf
            'Wscript.echo Results
       Next

      Set objoutputFile = objfso.openTextFile(strwritefile,8, True)
      objoutputFile.writeline Results & VbCrLf
      objoutputFile.close

Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing
Next

Wscript.echo "Completed"
Wscript.quit

Hope this helps....
0
 
bsharathAuthor Commented:
I get this.

---------------------------
Windows Script Host
---------------------------
Script:      C:\Services.vbs
Line:      22
Char:      128
Error:      Unterminated string constant
Code:      800A0409
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
bsharathAuthor Commented:
Chandru,
Excellent but the results come 4 times it repeats
0
 
chandru_solCommented:
Can you post the resuilts?
0
 
bsharathAuthor Commented:
Its 3 times...
dev-chen-mrd100 Computer Browser Running
dev-chen-mrd100 Remote Registry Running
dev-chen-mrd100 Windows Firewall/Internet Connection Sharing (ICS) Stopped
dev-chen-mrd100 Sophos Agent Running
dev-chen-mrd100 Windows Management Instrumentation Running


dev-chen-mrd100 Computer Browser Running
dev-chen-mrd100 Remote Registry Running
dev-chen-mrd100 Windows Firewall/Internet Connection Sharing (ICS) Stopped
dev-chen-mrd100 Sophos Agent Running
dev-chen-mrd100 Windows Management Instrumentation Running



dev-chen-mrd100 Computer Browser Running
dev-chen-mrd100 Remote Registry Running
dev-chen-mrd100 Windows Firewall/Internet Connection Sharing (ICS) Stopped
dev-chen-mrd100 Sophos Agent Running
dev-chen-mrd100 Windows Management Instrumentation Running



0
 
chandru_solCommented:

Can you make sure the text file for computers doesn't have space after one computer name?

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.