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

Noob needing some help

Status
Not open for further replies.

ipeca

IS-IT--Management
Jan 11, 2007
17
0
0
US
Hi All,

Hoping you can help a noob out. Recent job change has given me the oppurtunity to learn scripting and programming. However I'm a bit stumped with a bit of code and was wondering if someone could point me in the right direction.

I have a server that is brought down for backups and restarted nightly. On ocassion a particular service doesn't always restart so I have borrowed and modified a script to resolve this. I have the output being sent to a logfile however I would prefer the logfile to append rather than overwrite and a time stamp to be placed between the entries.

Here's the code:
Code:
strComputer = "."
Set objShell = CreateObject("WScript.Shell")
Logfile = "C:\fso\fso.txt"
Const ForAppending = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Logfile, ForAppending)
systime=now()


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
  Set colRunningServices =  objWMIService.ExecQuery ("Select * from Win32_Service Where Name = 'spooler'")
  For Each objItem In colRunningServices
  If objItem.State="Stopped" Then 
      objFile.WriteLine "the service is STOPPED" & objItem.DisplayName & " -- " & objItem.State
      Call SetManStart
  Else
      objFile.WriteLine " the service is RUNNING" & space(1) & objItem.DisplayName & " -- " & objItem.State 
   End If
  Next

Sub SetManStart
'Set service to Manual
Set objWMIService = GetObject("winmgmts:" _ 
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colServicesState = objWMIService.ExecQuery _ 
 ("Select * from Win32_Service where Name='spooler'") 
For Each objService in colServicesState 
   objService.ChangeStartMode("AUTO")
   wscript.sleep 5000
'Start service
If objService.State="Stopped" then 
objService.StartService()
   wscript.sleep 2000
   objFile.WriteLine "service was Started"
End If
Next
End Sub

TIA
 
Change this:

Const ForAppending = 2

to this:

Const ForAppending = 8

to make it append. As for the time stamp, simply create a string that is the time stamp and prepend it to the string that you are writing. It could be as simple as:

objFile.WriteLine Now() & "service was Started"

Or as complex as you want if you want to control the formatting of the timestamp.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Worked liked a charm and was exactly what I wanted, thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top