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
 
instead of [tt]objShell.LogEvent[/tt], which logs events to the Application event log, open a new text file in append mode

Code:
'open file in append mode (8)
set objLog = fs.OpenTextFile("myLogFile.txt", 8, true, 0)

'write to file
objLog.WriteLine "Hello"

'close file
objLog.close

Also, no need to redefine the objWMIService object more than once. To emulate dependancy, use a [red]
flag[/red] and an [blue]if..then statement[/blue]

Code:
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set colService = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'TermService'")
for each objService in colServices
   objServices.StopService
   [red]blnStopRDP = true[/red]
next

[blue]if (blnStopRDP = true) then
   set colDependentService = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'UmRdpService'")
   for each objDependentService in colDependentService
      objService.StopService()
   next
end if[/blue]

-Geates
 
Thanks for helping. I got the write file working. The services is giving error: Object not a collection, Code: 800A01C3 on Line 3, Char 1. Please let me know what am I doing wrong below.

This is what I have in just new vbscript file with stop service. Eventually once I have it working, I would copy to the main file above.

set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set colService = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'TermService'")
for each objService in colServices
objServices.StopService
blnStopRDP = true
next

if (blnStopRDP = true) then
set colDependentService = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'UmRdpService'")
for each objDependentService in colDependentService
objService.StopService()
next
end if
 
Object not a collection" means the object you are trying to iterate, in this case using a [tt]for each[/tt] loop, is not a recognized collection of objects. This is typically brought on by a typo in the variable names.

Notice the colored variable name - they should be the same but they are not.

Code:
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set [blue]colService[/blue] = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'TermService'")
for each [red]objService[/red] in [blue]colServices[/blue]
 [red]objServices[/red].StopService
next

-Geates
 
Thanks again. I got that fixed. I need help with two more things.

I would like to run only one thing at a time when the previous thing is over.. Example: when it completes below SEP_SupportTool.exe, then the service is stop, then it needs to start the service, then it needs to write the log, etc...

I would like to run below command before it can stop any services.

c:c:\test\testSep_SupportTool.exe /h -noup -client -version12 -console -s -def -lp -out " s:\Program Files (x86)\Symantec\Symantec Endpoint Protection Manager\dataSST\"

This would need to go right after below thing:
'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

---------here--------

-----stop service -------

------ start service -------

------- write log -------

------ else -----

--- end -----
 
I'm not following exactly. You keep referring to "thing" and "the service" but have not defined what they are. Should I assume you mean "phone" and "street sweeping", respectively? Should I also assume that you've opted not to use the requested logging method?

Code:
---------here--------
'huh?

set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set colService = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'TermService'")

for each objService in colServices
  -----stop service -------
  objServices.StopService


next 
 
------ start service -------
 
------- write log -------
 
------ else -----
 
--- end -----
 
stupid "submit" hot-key

Code:
 ---------here--------
 'huh?
 
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set colService = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'TermService'")
 
for each objService in colServices
 -----stop service -------
 objServices.StopService
 wscript.sleep 1000 'wait 1000 millisecs
 ------ start service -------
 objServices.StartService
 ------- write log -------
 objLog.WriteLine "Log something"
next 
 
 ------ else -----
 huh?

-Geates
 
The problem with that is it can be 100 millisecs or it can be 10000 or 100000 millisecs. I don't want to guess and be wrong. Is there a way to verify the stop service is complete before starting those service again and then verifying the service is started before it can write log.


Also, I need help to run below command before the stop service step.

Sep_SupportTool.exe /h -noup -client -version12 -console -s -def -lp -out " s:\Program Files (x86)\Symantec\Symantec Endpoint Protection Manager\dataSST\
 
btw.. i used your new stop code and working great and also the write code and working.. just need help regarding my last posting.
 
The problem with that is it can be 100 millisecs or it can be 10000 or 100000 millisecs. I don't want to guess and be wrong. Is there a way to verify the stop service is complete before starting those service again and then verifying the service is started before it can write log.

instead of sleep, keep checking for the service. if no collection is returned, then sthere is no service. Seeing as how this is done more than once, I recommend using functions

Code:
Const EVENT_WARNING = 2
 
set objFSO = createObject("Scripting.FileSystemObject")
set objShell = CreateObject("Wscript.Shell")
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

function stopService(strServiceName)
	set colService = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
	for each objService in colService
		objService.stopService
	next
	
	do
		set collection = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
		wscript.sleep 100
	loop until (colProcesses.count = 0)
end function

function startService(strServiceName)
	set colService = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
	for each objService in colService
		objService.startService
	next
	do
		set collection = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
		wscript.sleep 100
	loop until (colProcesses.count <> 0)
end function

function runSupportTool()
	objShell.Run "Sep_SupportTool.exe /h -noup -client -version12 -console -s -def -lp -out "" s:\Program Files (x86)\Symantec\Symantec Endpoint Protection Manager\data SST\""", true
end function

'begin
stopService("TermService")
startService("TermService")
runSupportTool

stopService("SomeOtherService")
startService("SomeOtherService")
runSupportTool

folderName = "C:\test\Test\"
numbers = fs.GetFolder(folderName).Files.Count
if (numbers > 5) Then
	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
end if

-Geates

[sup][/sup]
 
Geates, I'd replace (2 times) this:
set collection =
with this:
set colProcesses =
 
It gives me error on colProcesses. Object required error.
 
I found the two replacement for colProcesses but the problem is when i run, it only stops one service. I want the service to stop all dependency along with it. I have tried below code but i guess i am doing wrong. Please help.

'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

function stopService
set colService = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = 'UmRdpService'")
for each objService in colService
objService.stopService
blnStopRDP = true
next
if (blnStopRDP = true) then
set colDependentService = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'TermService'")
for each objDependentService in colDependentService
objDependentService.StopService()
next

do
set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
wscript.sleep 100
loop until (colProcesses.count <> 0)
end function

'begin
stopService
 
@PHV: oops, good point.

@mbp007:
for future posts could you please surround your code in [ignore]
Code:
[/ignore] tags - it really help when reading code.

but the problem is when i run, it only stops one service. I want the service to stop all dependency along with it.
You'll have to figure out what the dependent services are. Then, using my previous code

Code:
set colService = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = 'SomeService'")
if (colService.count) then
   stopService("DependentServiceA")
   stopService("DependentServiceB")
   stopService("DependentServiceC")
end if

-Geates


 
Sorry for not entering in correct format. I just created brand new vbscript file just to test the stop services function before i can start merging in one big file. I get error: Wrong number of arguments or invalid property assignment: 'stopService'. Line 15 Char 6

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

function 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 function

'begin
stopService

else

end if
 
I don't know if this is causing you the problem, but you have your function block defined within an If statement. It is very likely incorrect syntax, at the very least it is bad form.

Try the following (all your code is there, just rearranged. I changed the function into a subroutine since it does not return a value):

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()
else

end if

'End of Main script

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

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
 
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
 
Here is what I have so far. It is some odd reason not working after the first steps (runsupporttool). If i disabled that, the stop services works fine. But then the start services does not start both services.

strongm (How to do the stop services as you say using the .STATE so it can be done more efficiently).

I still need help to get it work all of it properly. 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.

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
	runSupportToolFolder = "c:\test\SST\"
	beforeFileCount =  fs.GetFolder(runSupportToolFolder).Files.Count
	Call runSupportTool() ' this is blocking call
	afterFileCount = fs.GetFolder(runSupportToolFolder).Files.Count 
	if (beforeFileCount < afterFileCount) Then
		Call stopService("UmRdpService")
		Call stopService("TermService") ' stop this first, then whatever was pass originaly stop second. 
		'Call StartService("UmRdpService")
		'Call StartService("TermService")
		
		'write Log to output.txt File
		set objLog = fs.OpenTextFile("output.txt", 8, true, 0)
		objLog.WriteLine now() & vbcrlf & "There are more than 5 files in the directory. " & vbcrlf & "The directory is " & folderName & vbcrlf & "Number of files are " & numbers & vbcrlf & "Services on FCIS661 Server Restarted " & vbcrlf
		objLog.Close
	end if
else
     'do something else
end if

'End of Main script

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

function runSupportTool()
	objShell.Run "Sep_SupportTool.exe /h -noup -client -version12 -console -s -def -lp -out ""c:\test\SST", true
end function

Sub stopService(serviceName)
	set colService = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & serviceName & "'")
	'if (colService.count > 1) then
		For Each obj in colService
            		obj.StopService()
        Next       	
	'end if 
	
	do
		set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & serviceName & "'")
		wscript.sleep 100
	loop until (colProcesses.count <> 0) 
End Sub 

Sub startService(serviceName)
	set colService = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & serviceName & "'")
	'if (colService.count > 1) then
		For Each obj in colService
            		obj.StartService()
        Next       	
	'end if 
	
	do
		set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & serviceName & "'")
		wscript.sleep 100
	loop until (colProcesses.count = 0) 
End Sub
 
objShell.Run "Sep_SupportTool.exe /h -noup -client -version12 -console -s -def -lp -out ""c:\test\SST", [!],[/!]true

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top