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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check to see if a service is running on a remote machine 1

Status
Not open for further replies.

Jesse222

IS-IT--Management
Aug 6, 2004
7
CA
Hey All,

Last week i was running this code with a non privileged user and it seemed to work fine. I have tested it on a remote computer that was in a workgroup as well as a computer that was in our active directory domain.

Now when I run it against the computer that is in a workgroup I get the Permission Denied error when the "Set objComputer = GetObject("WinNT://" & strPrimaryNCR & ",computer")" runs and if I run it against a computer in our AD domain it get a Access Denied Error when "Set objService = objComputer.GetObject("service", "Symantec Antivirus")" runs

I was surprised this even worked last week. I would have thought I would need an admin account to check to see of a service was running, now I think I might. If I make that account running the scrip admin the script runs again.


Any idea why it would have worked last week and not this week?. Nothing should have changes on either of the test computers. And if I do need admin rights is there a way to pass in admin credentials into the getobject method?


Thanks
Jesse


' Check to see if the service is running
strComputer = “MyTest01”
Set objComputer = GetObject("WinNT://" & strComputer & ",computer")
Set objService = objComputer.GetObject("service", "Symantec Antivirus")
If objService.Status = 4 Then
Msgbox "The service is running"
Else
MsgBox "The service is NOT running"
End If
End If
 
Code:
strComputer = "FullComputerName" 
strDomain = "DOMAIN" 
Wscript.StdOut.Write "Please enter your user name:"
strUser = Wscript.StdIn.ReadLine 
Set objPassword = CreateObject("ScriptPW.Password")
Wscript.StdOut.Write "Please enter your password:"
strPassword = objPassword.GetPassword()
 
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, _
    "root\cimv2", _
     strUser, _
     strPassword, _
     "MS_409", _
     "ntlmdomain:" + strDomain)
Set colSwbemObjectSet = _
    objSWbemServices.ExecQuery("Select * From Win32_Service")
For Each objService in colSWbemObjectSet
    Wscript.Echo "Service Name: " & objService.Name 
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Here's how I do it:
Code:
Do While strComputer = ""
  strComputer = InputBox("Computer name" & vbCrLf & "type quit to exit")
  If LCase(strComputer) = "quit" Or LCase(strComputer) = "exit" Then
    WScript.Quit
  End If
Loop
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

If Err.Number <> 0 Then
  MsgBox "Error number = " & Err.Number
  WScript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile(strComputer & "_services.csv", True)

Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)

objOutput.WriteLine "Caption,DisplayName,Name,AcceptPause,AcceptStop,CheckPoint,CreationClassName,Description," & _
                    "DesktopInteract,ErrorControl,ExitCode,InstallDate,PathName,ProcessID,ServiceSpecificExitCode," & _
                    "ServiceType,Started,StartMode,StartName,State,Status,SystemCreationClassName,SystemName," & _
                    "TagID,WaitHint"

For Each objItem in colItems
  objOutput.WriteLine objItem.Caption & "," & _
                      objItem.DisplayName & "," & _
                      objItem.Name & "," & _
                      objItem.AcceptPause & "," & _
                      objItem.AcceptStop & "," & _
                      objItem.CheckPoint & "," & _
                      objItem.CreationClassName & "," & _
                      objItem.Description & "," & _
                      objItem.DesktopInteract & "," & _
                      objItem.ErrorControl & "," & _
                      objItem.ExitCode & "," & _
                      objItem.InstallDate & "," & _
                      objItem.PathName & "," & _
                      objItem.ProcessId & "," & _
                      objItem.ServiceSpecificExitCode & "," & _
                      objItem.ServiceType & "," & _
                      objItem.Started & "," & _
                      objItem.StartMode & "," & _
                      objItem.StartName & "," & _
                      objItem.State & "," & _
                      objItem.Status & "," & _
                      objItem.SystemCreationClassName & "," & _
                      objItem.SystemName & "," & _
                      objItem.TagId & "," & _
                      objItem.WaitHint
Next
This asks for the computer name you want to check then dumps the result into a .csv called <computername>_services.csv located in the same folder as the .vbs.

My script doesn't take into account the username/password but that could be integrated probably relatively easily?

I'll see if I can do that then get back.

h
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top