Link to home
Create AccountLog in
Avatar of bsharath
bsharathFlag for India

asked on

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
Avatar of chandru_sol
chandru_sol
Flag of India image

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
Avatar of bsharath

ASKER

For Computer.
Just need to show if the service is enabled or disabled.
If possible if disabled then we can enable it.
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
Sharath,

Did you try this script?
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
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
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.
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?
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.
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....
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  
---------------------------
ASKER CERTIFIED SOLUTION
Avatar of chandru_sol
chandru_sol
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Chandru,
Excellent but the results come 4 times it repeats
Can you post the resuilts?
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




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