Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check Service State

Status
Not open for further replies.

1nsyz1on

Technical User
Nov 9, 2009
1
ZA
you can use WMI and Win32_Service class, should find loads of examples on the site. i find ADSI more readableSet objComputer = GetObject("WinNT://computername,computer")objComputer.Filter = Array("Service")For Each aService In objComputer Wscript.Echo aService.Name & "=" & aService.StatusNext

I got this snippet of code from another closed thread.

I nee some more work done here.

I need to check the status of the Service and that value you get e.g 4=running I need to get written to a log file.

Is this possible?


Regards
 
Code:
Set objComputer = GetObject("winmgmts:\\" & strComputer)
Set objServices = objComputer.InstancesOf("Win32_Service") 

for each objService in objServices
   if (objService.State = 4) then
      msgbox objService.Name & "I'm running!!"
   end if
next

-Geates
 
hello Geates,

just wondering if I may trouble you for a sec is this inquiry came up in the previous closed thread 1nsyz1on stated but..

I love the services script that uses the array. I can place the hostname of the pc I need services to identify if started or stopped but, I can't seem to run it remotely.. the pc I am on is in a workgroup and I want to get the script to speak to a domain controller in a domain .. will this not work because of the disparate workgroup to domain issue?

script:
ArrComputer = Array("gmanoss")
ArrServices = Array("Alerter", "DHCP Client", "DNS Client")
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


The error I receive is "permission denied: 'get-object'

Would it work assumingly if I joined this computer I am running the script from to the domain that the domain controller is on? or is this an elevated admin based issue?

any guidance would be so great and thank again!

blade
 
Instead of using the computer name, use its IP address. However, if you want to use the computer name, make sure the DNS suffix on the computer is that of the domain, otherwise, the computer will not be found.

Joining the computer to the domain is another option. This establishes a trust relationship between computer and domain.

Both avenues require that the local PC contain the appropriate domain group memberships as to allow a domain user to access the pc.

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top