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!

Writing to text file with append mode

Status
Not open for further replies.

mbp007

Technical User
Nov 2, 2012
13
US
Hello,
I am not that savvy in vb script but I have below script. What I want is it writes the log info in the txt file with append mode. I would like to have Date, Time and How much numbers was it if it restarted the service. If it did not restart the service, it does not need to write in the same text file but different text file with date, time, and numbers. Also can the TermService stop the UmRdpService service. I have to do it separate below but if it can stop dependency, it would be great.

'Local C Drive Test
Set fs = CreateObject("Scripting.FileSystemObject")
folderName = "C:\test\Test\"
numbers = fs.GetFolder(folderName).Files.Count
if (numbers > 5) Then
Const EVENT_WARNING = 2
Set objShell = CreateObject("Wscript.Shell")
objShell.LogEvent 2, "There are more than 5 files in the directory. The directory is " & folderName & ". Number of files are " & numbers
msgbox "There are more than 5 files in the directory. " & vbcrlf & "The directory is " & folderName & "." & vbcrlf & "Number of files are " & numbers & vbcrlf & "RESTART SEPM SERVICES ON C DRIVE ",,"C DRIVE: Total SEPM Files: " & numbers

'Stop Service
strServiceName = "UmRdpService"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StopService()
Next

'Stop Service
strServiceName = "TermService"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StopService()
Next

'Start Service
strServiceName = "UmRdpService"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StartService()
Next

'Start Service
strServiceName = "TermService"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StartService()
Next

else
msgbox ("There is no concern of the files in the directory. " & vbcrlf & "The directory is " & folderName & "." & vbcrlf & "Number of files are " & numbers)

End If
 
Thanks all of you for helping me get this working. All is working fine.

strongm: Regarding your note above "BTW, a Win32_Process has a .STATE property which means you can avoid repeatedly and inefficiently polling all the services to see check whether a specific service has stopped", can you check the code above and tell me what can I change to get it working the way you told me to be more efficient?

Thanks
 
Also, I noticed that the wscript is keep runnning in the task manager even though it is not doing all the steps. I need to make sure after all the steps are completed, the wscript is done and not running in the task manager

This is probably a result of constantly polling services - it takes a lot of resources. As strongm suggested, we can replace the current inefficient service polling routine with a simple quick look at the services .state property.

Code:
Sub stopService()
	set colService = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = 'TermService'")
	if (colService.count) then
   		stopService("UmRdpService")
	end if 
	
	do
		set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = 'TermService'")
		wscript.sleep 100
	loop until (colProcesses.count <> 0)
End Sub

with

Code:
sub stopService(strServiceName)
	set colService = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
	for each objService in colService
		objService.stopService
		[red]do until (objService.State = "Stopped") : wscript.sleep 100 : loop[/red]
	next
end sub

However, using a for..loop doesn't make to much sense to me because all services have unique names. And if the name is the only attribute that is being queried, a "collection" of services is impossible. Therefore, there is no need to use a for..loop to iterate a collection of one item.

This is how I would implement thing. (@any: if any, please voice your concerns with this method)

Code:
sub stopService(strServiceName)
	set objComputer = GetObject("WinNT://.,computer")
	set objService  = objComputer.GetObject("service", strServiceName)
	
	if (isObject(objService)) then
		objService.Stop
		do until (objService.State = "Stopped") : wscript.sleep 100 : loop
	end if
end sub

-Geates

 
I tried the last code which you posted but I get error "The service has not been started". Line: 28, Char: 3.

Here is the code below. I removed other stuff so I can just try the stop service first before I put it in the main vbscript file.

Code:
'open file called output.txt in append mode (8)
Const EVENT_WARNING = 2

Set fs = CreateObject("Scripting.FileSystemObject") 
set objShell = CreateObject("Wscript.Shell")
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

folderName = "C:\test\Test\"
numbers = fs.GetFolder(folderName).Files.Count
if (numbers > 5) Then
    'begin
		Call stopService("UmRdpService")
		Call stopService("TermService") ' stop this first, then whatever was pass originaly stop second. 
		
else
     'do something else
end if

'End of Main script

'&&&&&&&&&& Functions and Subroutine definitions below here &&&&&&&&&&

sub stopService(strServiceName)
	set objComputer = GetObject("WinNT://.,computer")
	set objService  = objComputer.GetObject("service", strServiceName)
	
	if (isObject(objService)) then
		objService.Stop
		do until (objService.State = "Stopped") : wscript.sleep 100 : loop
	end if
end sub
 
Then start the service.

Please don't take this the wrong way but how do you expect to learn VBS if you're not willing to troubleshoot the issues it may present.

Also, I'm not sure what line is line 28. What if you added comments to the top? Line 28 to you X where as to me it Y. So when specifying a problematic line, please identify the line.

-Geates

 
The property that strongm suggested is a object member of the Win32_Service class, not the Win32_Process class. In order to use it (which is recommended for the reasons strongm previously mentioned), services must be gathered against the Win32_Service class. Note, however, that this changes other object member methods. Most notably, objService.stop. While [tt].stop()[/tt] belongs to Win32_Process, [tt].stopService()[/tt] belongs to Win32_Service.

Below takes everything into consideration and accomplishes the derivative goals of the OP.

Code:
sub stopService(strServiceName)
	set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	set colServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & strServiceName & "'")
	
	for each objService In colServices
		objService.StopService
		do until(objService.State = "Stopped") : wscript.sleep 100 : loop
	next
end sub

-Geates

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top