Windows Scripting
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsSystem AdministrationWindows Scripting

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 30th, 2008, 07:05 PM
limester limester is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 1 limester User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 19 sec
Reputation Power: 0
Question WMI - Check if service is running and start it

Hello,

I am trying to piece together a script that will do the following:

1. Check status of a service daily
2. Start the service if it is no running
3. Log the actions to a log file

I would run this as a scheduled task. I have the following pieces of WMI to:

1. check status of service:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name = 'someService'")
For Each objService in colRunningServices
Wscript.Echo objService.DisplayName & ": " & objService.State
Next

> I would prefer not to have any messsage box appear

2. Start the Service:

Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
WScript.Echo " Starting " & objService.Name
objService.StartService()

Any help to piece this together into a vbs script would be greatly appreciated!

Cheers!

Reply With Quote
  #2  
Old October 31st, 2008, 01:24 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
You're on the right track. You have all of the code that you need to accomplish this, but you are taking a bit of a run-around approach.
vb Code:
Original - vb Code
  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  3. Set colRunningServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Name = 'someService'")
You have correctly made use of WMI to return an instance of a single service. Notice that colRunningServices does return a collection, but in this case, it only contains a single item since there can only be one service with a given name.
vb Code:
Original - vb Code
  1. For Each objService in colRunningServices
  2.     WScript.Echo objService.DisplayName & ": " & objService.State
  3. Next
This next bit of code is a standard coding convention for iterating through the items in a collection. You don't need to do this. Since you know that you're only dealing with a single item, you can simply reference that item directly.
vb Code:
Original - vb Code
  1. Set objService = colRunningServices(0)
Next, you have some code that looks like this:
vb Code:
Original - vb Code
  1. Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
  2. WScript.Echo " Starting " & objService.Name
  3. objService.StartService()
This code uses WMI to return an object reference for a specific service. As you can see from the last line of code I used above, we already have this so there's no need to query WMI twice. Simply wrap this all up in an IF block to perform your logic.
vb Code:
Original - vb Code
  1. If objService.State <> "Running" And objService.State <> "Starting" Then
  2.     objService.StartService()
  3. End If
Voila! This code uses the existing service reference. If the service's status is not running and the service isn't starting, then we force it to start with the ServiceStart method.

So the complete code looks like this:
vb Code:
Original - vb Code
  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  3. Set colRunningServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Name = 'someService'")
  4.  
  5. Set objService = colRunningServices(0)
  6.  
  7. If objService.State <> "Running" And objService.State <> "Starting" Then
  8.     objService.StartService()
  9. End If
__________________
Click the image if at any point you don't like my decision.

Scripting problems? Windows questions? Ask the Windows Guru!


Reply With Quote
Reply

Viewing: ASP Free ForumsSystem AdministrationWindows Scripting > WMI - Check if service is running and start it


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT