Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

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
0
bsharath
Asked:
bsharath
  • 9
  • 7
1 Solution
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now