We help IT Professionals succeed at work.

Logon script to check if these services are running.

bsharath
bsharath asked
on
284 Views
Last Modified: 2010-04-16
Hi,

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

Etc.

Regards
Sharath
Comment
Watch Question

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

Author

Commented:
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?

Author

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

Author

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.
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?

Author

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.
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....

Author

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  
---------------------------
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Chandru,
Excellent but the results come 4 times it repeats
Can you post the resuilts?

Author

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




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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.