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!

Function line issue to restart a service..

Status
Not open for further replies.

Junbron

Technical User
Mar 5, 2012
15
NZ
I am trying to create a Script to stop and start a Service...for a list of machines
Below is my code,...
I kept on getting an issue with The Function Line????
========


Const ForReading = 1

dim objFSO, strFile, strFilePath, OFile, oFile2, strPCNum, strOutputFile, strRemoteFile, strFileVersion, strOutText
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

strInputFile = InputBox("Enter the file path to the file containing the list of PCs", "Enter the file path")

if strInputFile = "" then
wscript.echo "No file path supplied. Quitting Program"
wscript.quit
end if

strOutputFile = InputBox("Enter the file path to write the results to", "Enter the file path")

if strOutPutFile = "" then
wscript.echo "No file path supplied. Quitting Program"
wscript.quit
end if

set oFile = objFSO.OpenTextFile(strInputFile,1)
set oFile2 = objFSO.OpenTextFile(strOutputFile,8,True)
do while not oFile.atendofstream
strPCNum=oFile.readline
if Ping(strPCNum) = False Then
strOutText = strPCNum + vbTab + "Off LINe"
oFile2.WriteLine(strOutText)

Else

Const SERVNAME = "schedule"
Const WAITTIME = 10000


Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strPCNum & "\root\cimv2")
Set colServices = objWMIService.ExecQuery ("SELECT * FROM win32_Service")
For Each objService in colServices
'WScript.Echo objService.Name 'Uncomment to list all service names on the machine
If objService.Name = SERVNAME Then
errReturnCode = objService.StopService()
WScript.Sleep WAITTIME
errReturnCode = objService.StartService()
strOutText = strPCNum + vbTab + "Restarted Service"
oFile2.WriteLine(strOutText)
End If

Next

Function Ping(PC)
Set objWshScriptExec = objShell.Exec("ping.exe -n 1 " & PC)
Set objStdOut = objWshScriptExec.StdOut

awake=False
Do Until objStdOut.AtEndOfStream
strLine = objStdOut.ReadLine
awake = awake Or InStr(LCase(strLine), "bytes=") > 0
Loop
Ping = awake

End Function


=========


PLease help
 
At least 2 problems:
1) Do While Not OFile.AtEndOfStream 'without corresponding Loop
2) If Ping(strPCNum) = False Then 'without corresponding End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks the funnction not the issue anymore...
-----its the last code..
==============================
Const ForReading = 1
On Error Resume Next

dim objFSO, strFile, strFilePath, OFile, oFile2, strPCNum, strOutputFile, strRemoteFile, strFileVersion, strOutText
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

strInputFile = InputBox("Enter the file path to the file containing the list of PCs", "Enter the file path")

if strInputFile = "" then
wscript.echo "No file path supplied. Quitting Program"
wscript.quit
end if

strOutputFile = InputBox("Enter the file path to write the results to", "Enter the file path")

if strOutPutFile = "" then
wscript.echo "No file path supplied. Quitting Program"
wscript.quit
end if

if objFSO.FileExists(strInputFile) then
set oFile = objFSO.OpenTextFile(strInputFile,1)
set oFile2 = objFSO.OpenTextFile(strOutputFile,8,True)
do while not oFile.atendofstream
strPCNum=oFile.readline
if Ping(strPCNum) = False Then
strOutText = strPCNum + vbTab + "Off LINe"
oFile2.WriteLine(strOutText)

end if
loop
else
wscript.echo "File containing list of PCs does not exist. Quitting Program"
wscript.quit
oFile.close
oFile2.Close

Function Ping(PC)
Set objWshScriptExec = objShell.Exec("ping.exe -n 1 " & PC)
Set objStdOut = objWshScriptExec.StdOut

awake=False
Do Until objStdOut.AtEndOfStream
strLine = objStdOut.ReadLine
awake = awake Or InStr(LCase(strLine), "bytes=") > 0
Loop
Ping = awake
End Function

Const SERVNAME = "schedule"
Const WAITTIME = 10000


Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strPCNum & "\root\cimv2")
Set colServices = objWMIService.ExecQuery ("SELECT * FROM win32_Service")
For Each objService in colServices
'WScript.Echo objService.Name 'Uncomment to list all service names on the machine
If objService.Name = SERVNAME Then
errReturnCode = objService.StopService()
WScript.Sleep WAITTIME
errReturnCode = objService.StartService()
strOutText = strPCNum + vbTab + "Restarted Service"
oFile2.WriteLine(strOutText)
End If
Next
================================================

Getting error "Line 67 Char 5
 
You're still missing an End If:
Code:
else
   wscript.echo "File containing list of PCs does not exist. Quitting Program"
   wscript.quit
[red]End If[/red]
oFile.close
oFile2.Close

Or wherever it may belong.

CHeers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top