Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Need some help with services script

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,40352
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Need some help with services script
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Need some help with services script - 12/4/2006 8:54:58 PM   
  buffi

 

Posts: 87
Score: 0
Joined: 3/28/2005
From:
Status: offline
Hi All,
I am trying to create a script that will check if a certain serivce is up (started).
If the service is up then exit and if the service is down then restart the service.

The only thing that i know how to do is to stop/start a service and change its mode to disable, automatic, manual.
I have taken a part of a script from here but i have trouble to finish it.


[code]
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service " _
               & "where Name='TlntSvr'") 'and Status='Stopped'")
For Each objService In colServiceList
errReturn = objService.StartService()
Next
[code]

Can anybody help :)

Cheers.
buff
 
 
Post #: 1
 
 RE: Need some help with services script - 12/5/2006 12:21:04 AM   
  SAPIENScripter


Posts: 276
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
The best approach for debugging WMI scripts like this is to test your query in WBEMTest.  You'll need to strip out the VBScript code in it first.  Once you have the query working, then paste it back into your script.  If there is still a problem, at least you know it isn't the WMI query which is a big step.

You might also have to check if the service is disabled because you'll get an error if you try to start it.

If after all of that you still have problems, tell us what error you are getting.

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

Follow Me: http://www.twitter.com/JeffHicks

(in reply to buffi)
 
 
Post #: 2
 
 RE: Need some help with services script - 12/5/2006 1:29:35 AM   
  buffi

 

Posts: 87
Score: 0
Joined: 3/28/2005
From:
Status: offline
Isnt there a script that can check the status of the service?
Like if the service is stopped?

I know the service is not disabled.
What i am trying to accomplish is to create a script that will run through task scheduler.
I have a problem on my server that sometimes one of the services crashes and stops.
so i want to create like an ongourd script on that certain service.
to check every 1 min if the service is down and if it is then to start it.

Any idea on how to create such a thing?

Regards,
Buff

(in reply to SAPIENScripter)
 
 
Post #: 3
 
 RE: Need some help with services script - 12/5/2006 1:37:08 AM   
  SAPIENScripter


Posts: 276
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
There are many scripts like that.  You should be able to find something via Google or http://www.searchscripting.com.  Here's one example I found:

      

You should also be able to configure the service to restart after it fails or run a script. The challenge is that if a service fails, restarting it with a script might still fail until you correct any underlying problem that is stopping the service in the first place.

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

Follow Me: http://www.twitter.com/JeffHicks

(in reply to buffi)
 
 
Post #: 4
 
 RE: Need some help with services script - 12/5/2006 1:47:24 AM   
  buffi

 

Posts: 87
Score: 0
Joined: 3/28/2005
From:
Status: offline
Hey thanks for the quick reply.

I have made a progress and managed to check the service status and if the service is stoped then to start it and if it is started then to quit.
I have another small issue.
I want the script to create a log with the date and time of each time the serivce was found down and restarted.
Any idea?

here is what i have so far.


      

Regards,
Buff

< Message edited by buffi -- 12/5/2006 1:49:46 AM >

(in reply to SAPIENScripter)
 
 
Post #: 5
 
 RE: Need some help with services script - 12/5/2006 2:02:01 AM   
  SAPIENScripter


Posts: 276
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
Use the FileSystemObject to create a text file.  You'll want to make sure you use APPEND if you want to keep the file.

      

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

Follow Me: http://www.twitter.com/JeffHicks

(in reply to buffi)
 
 
Post #: 6
 
 RE: Need some help with services script - 12/5/2006 2:27:36 AM   
  buffi

 

Posts: 87
Score: 0
Joined: 3/28/2005
From:
Status: offline
Hi and Thanks again.
I have managed to get my script to change the service from disabled to manual and start the service if needed.
I have problems with the log. On line 15 which is Sub WriteToLog(strMessage).
Can someone help :)


This is my script now.


      


Regards,
Buff

< Message edited by buffi -- 12/5/2006 2:59:35 AM >

(in reply to SAPIENScripter)
 
 
Post #: 7
 
 RE: Need some help with services script - 12/5/2006 4:30:31 AM   
  SAPIENScripter


Posts: 276
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
You can't have a subroutine like that in the middle of your script.  Get rid of the SUB and End Sub lines, and even though it won't be the prettiest code at the ball I think it should work.

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

Follow Me: http://www.twitter.com/JeffHicks

(in reply to buffi)
 
 
Post #: 8
 
 RE: Need some help with services script - 12/5/2006 4:55:13 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
It is perfectly valid to stick a sub in the middle. Granted it makes the code harder to read and certainly is not best practice, but it would compile and run just fine. Here is a sample to demonstarte:

      

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to SAPIENScripter)
 
 
Post #: 9
 
 RE: Need some help with services script - 12/5/2006 5:06:52 AM   
  SAPIENScripter


Posts: 276
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
Except in this case, his subroutine needed a runtime parameter which he was never calling.  In this case I don't think he really needs the sub at all.

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

Follow Me: http://www.twitter.com/JeffHicks

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: Need some help with services script - 12/5/2006 5:09:58 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
I agree that the sub appears useless in the posted code. I just wanted to make sure that it was clear that there is no right or wrong place to put procedures as far as the compiler is concerned. There are simply guidelines to make things easier to read. 

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to SAPIENScripter)
 
 
Post #: 11
 
 RE: Need some help with services script - 12/5/2006 5:02:56 PM   
  buffi

 

Posts: 87
Score: 0
Joined: 3/28/2005
From:
Status: offline
Sup guys,
thanks for the replies.  I know that i am doing something wrong since i am a newbie :P
Can anybody check my code and help me fix it ?

I took the sub from one of the posts here since i want to create a small log that will log each time the service was restarted.

and idea on how to do it.  The first part of the script works.
But i need the log


cheers
buff

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: Need some help with services script - 12/6/2006 12:14:56 AM   
  mbouchard


Posts: 1922
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
I made a couple changes to the script.  Moved the sub to the bottem, it is not "required" but makes it easier to read.  I also removed the text you had in the writeline inside the sub.  You do not need this as you can pass the text when you call the sub.  I also added that call in 2 places, when you have to start the service and if the service is already started.  lastly, you needed to create the FS object so I added that into the Sub.  This sub must have been from one of my less comment heavy scripts as i have started adding a "requirements" comment inside the sub.  Always makes you feel good when someone can reuse your code.


      

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to buffi)
 
 
Post #: 13
 
 RE: Need some help with services script - 12/6/2006 12:57:32 AM   
  buffi

 

Posts: 87
Score: 0
Joined: 3/28/2005
From:
Status: offline
mbouchard
You are a genious thanks alot.


Cheers
bufff

(in reply to mbouchard)
 
 
Post #: 14
 
 RE: Need some help with services script - 12/6/2006 1:16:50 AM   
  mbouchard


Posts: 1922
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
quote:

ORIGINAL: buffi

mbouchard
You are a genious thanks alot.


Cheers
bufff


Why, yes, yes I am .

You are welcome.

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to buffi)
 
 
Post #: 15
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Need some help with services script Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts