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

script takes forever to run 2

Status
Not open for further replies.

holidayIT

IS-IT--Management
Apr 2, 2004
138
0
0
US
i have a script that runs for days. It connects to a machine on the network (uses a txt file for recordset), pings and connects to machine, then shuts down 2 services, then moves a file, then restarts the services (then moves to next record). I have put the script together with the invaluable support of other tek-tips members. At the present, however, i am unsure if it is working. if anyone can help (corrections, improvements, anything), i'd appreciate it.

here is my code:


On Error Resume Next

'Wscript.Echo "Beginning Script "

' define ADSI status constants
Const ADS_SERVICE_STOPPED = 1
Const ADS_SERVICE_START_PENDING = 2
Const ADS_SERVICE_STOP_PENDING = 3
Const ADS_SERVICE_RUNNING = 4
Const ADS_SERVICE_CONTINUE_PENDING = 5
Const ADS_SERVICE_PAUSE_PENDING = 6
Const ADS_SERVICE_PAUSED = 7
Const ADS_SERVICE_ERROR = 8

' define string constants for service methods
Const START_SERVICE = "START"
Const STOP_SERVICE = "STOP"
Const PAUSE_SERVICE = "PAUSE"
Const CONTINUE_SERVICE = "CONTINUE"

' declare global variables
Dim objWsh
Dim objEnv
Dim strComputerName

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'open the data file
Set oTextStream = oFSO.OpenTextFile("wslist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close
Set objEnv = WSHShell.Environment("PROCESS")

myFile="file.file"
sourceDir="\\server\folder\"
destDir="\program files\folder\"

Set WSHNetwork = CreateObject("WScript.Network")
WSHNetwork.MapNetworkDrive "U:", sourceDir,"True"

For Each strWorkstation In RemotePC
'Do something useful with strWorkstation

strComputerName = strWorkstation

set objShell = CreateObject("WScript.Shell")
' Wscript.Echo "Pinging " & strComputerName
response = ping(strComputerName)
intSuccess = instr(response," Received = 0")

if intSuccess = 0 then
'Wscript.Echo "Connected " & strComputerName
blnSeverUp = true
else
blnServerUp = false
'Wscript.Echo "Not Connected " & strComputerName
end if

If blnServerUp = false Then

destDir2 = "\\" & strComputerName & "\C$" & destDir

'Wscript.Echo destDir2

WSHNetwork.MapNetworkDrive "Y:", destDir2
' Wscript.Echo "Mapped"

' call CycleService() to stop all the services
CycleService strComputerName,"FSMA",STOP_SERVICE,False
CycleService strComputerName,"FSPM",STOP_SERVICE,False

Call WSHShell.Run("cmd.exe /C xcopy U:\" & myFile & " Y:\")

' wscript.Echo "cmd.exe /C xcopy U:\" & myFile & destDir2

'Wscript.Echo "Copied"

' call CycleService() to start all the services
CycleService strComputerName,"FSPM",START_SERVICE,False
CycleService strComputerName,"FSMA",START_SERVICE,False

'Else
' WScript.Echo strComputerName & " did not respond to ping."
End If

' Wscript.Echo strComputerName & " Completed"

If err.number <> 0 Then
wscript.Echo "Error!!! " & err.Description
End If

Next

Set oFSO = Nothing
Set WSHShell = Nothing
Set WSHNetwork = Nothing
Wscript.Echo "All done"
WScript.Quit(0)

' ****************************************
' CycleService() subroutine
' this subroutine is passed four variables:
' 1. strComputer = the name of the computer
' 2. strService = the name of the service (e.g. w3svc, smtpsvc, etc.)
' 3. strOperation = the operation to be completed (e.g. start, stop)
' 4. boolTrace = True will output trace information, False will not
' ****************************************
Sub CycleService(strComputer,strService,strOperation,boolTrace)
On Error Resume Next

' declare variables
Dim objComputer
Dim objService
Dim strTrace
Dim boolSuccess

' get ADSI objects and initial variables
Set objComputer = GetObject("WinNT://" & strComputer & ",computer")
Set objService = objComputer.GetObject("Service",strService)
strTrace = strOperation & " " & strService & " on " & strComputer
boolSuccess = False

' output trace information if needed
If boolTrace Then Trace "Attempting to " & strTrace & "..."

' determine the operation and carry it out
Select Case (strOperation)
Case START_SERVICE
If (objService.Status = ADS_SERVICE_STOPPED) Then
objService.Start
' If Err.Number<>0 Then ErrorHandler strTrace
While objService.Status <> ADS_SERVICE_RUNNING: Wend
boolSuccess = True
End If
Case STOP_SERVICE
If (objService.Status = ADS_SERVICE_RUNNING) Or (objService.Status = ADS_SERVICE_PAUSED) Then
objService.Stop
' If Err.Number<>0 Then ErrorHandler strTrace
While objService.Status <> ADS_SERVICE_STOPPED: Wend
boolSuccess = True
End If
Case PAUSE_SERVICE
If (objService.Status = ADS_SERVICE_RUNNING) Then
objService.Pause
' If Err.Number<>0 Then ErrorHandler strTrace
While objService.Status <> ADS_SERVICE_PAUSED: Wend
boolSuccess = True
End If
Case CONTINUE_SERVICE
If (objService.Status = ADS_SERVICE_PAUSED) Then
objService.Continue
' If Err.Number<>0 Then ErrorHandler strTrace
While objService.Status <> ADS_SERVICE_RUNNING: Wend
boolSuccess = True
End If
End Select

' output trace information if needed
If boolTrace And boolSuccess Then Trace strTrace & " was successful."

End Sub

' ****************************************
' Trace() subroutine
' outputs time and trace information
' ****************************************
Sub Trace(strText)
WScript.Echo Now & " : " & strText
End Sub

' ****************************************
' ErrorHandler() subroutine
' outputs error status and exits
' ****************************************
Sub ErrorHandler(strText)

'Dim strError
'strError = Now & " : The following error occurred trying to " & strText & vbCrLf
'strError = strError & vbCrLf & "0x" & Hex(Err.Number)& " - " & Err.Description
'WScript.Echo strError
'WScript.Quit
Err.Clear

End Sub

function ping(strTarget)
cmdline="%COMSPEC% /c ping " & response
Call ExternalCMDCall(cmdline,strOut)
ping=strOut
end function

Sub ExternalCMDCall(cmdline,strOut)
set objExCmd = objShell.Exec(cmdline)
strOut=objExCmd.StdOut.ReadAll
End Sub


 
If blnServerUp = false Then
Seems you try to work with computers that didn't respond to ping ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hello holidayIT,

[1] Do not use %comspec& /c with say ping for .exec
Code:
    [red]'cmdline="%COMSPEC% /c ping " & response[/red]
    cmdline="ping " & strTarget

[2] intSuccess = instr(response," Received = 0")
Code:
    [red]'if intSuccess = 0 then[/red]
    if intSuccess <> 0 then
        'Wscript.Echo "Connected " & strComputerName
        blnSeverUp = true
    else
        blnServerUp = false
        'Wscript.Echo "Not Connected " & strComputerName
    end if
Note: there are cases where host is unknown and the coding is such that it would result in blnServerUp=true as well.
[3] Mapnetworkdrive
Code:
    [red]'WSHNetwork.MapNetworkDrive "U:", sourceDir,"True"[/red]
    WSHNetwork.MapNetworkDrive "U:", sourceDir,True
regards - tsuji

 
Correction:

Please ignore [2]. The original is correct. My mistake. Sorry! (my footnote on host unknown still a possible case for further consideration.)

- tsuji
 
I made the changes you both suggested, but now it opens a command prompt for ping.exe and sits for a few seconds till it finally gives me an error for "No network provider accepted the given network path" i am double checking it now.
 
nevermind about the ping, it works fine, but the network mapping seems to be the issue now.
 
has anyone seen this error before with drivemapping??? i cannot find too much info online about it.

error -2147023693 "no network provider accepted the given network path
 
holidayIT,

For the error, you need to assure the network resource is actually started, the folder is shared, etc...

Also, consider map the drive providing the credential:
object.MapNetworkDrive(strLocalName, strRemoteName, [bUpdateProfile], [strUser], [strPassword])

- tsuji
 
i get the user and password, but the bUpdateProfile...is that the boolean for making the map static???
 
Hi holidayIT,

I assume you could significantly increase the performance of your script by using Perl instead of VBScript.
There is a Perl forum here (forum219). You could use DOS commands such as NET USE, NET START a.s.o to start a service, connect to a PC etc.

Perhaps a quick Keyword search in Perl forum will already give you a good start.

Hope that helps,
Andy

[blue]The last voice we will hear before the world explodes will be that of an expert saying:
"This is technically impossible!" - Sir Peter Ustinov[/blue]
andreas.galambos@bowneglobal.de
HP:
 
i am afraid that i am even less knowledgable with perl than with vbscript. I really don't care which scripting language i use, so long as i get it to work.

I added the changes to the mapnetworkdrive functions, and i no longer get any errors, however, the file is not being copied over. does it matter if the file exists already? i need to copy over it. could that be it? or does anyone know if the command i am using to copy the file is just plain wrong?
 
i need to copy over it
Add the /Y switch to your xcopy command line.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I just cannot figure it out. That doesn't seem to fix it either. I have tried several switches for xcopy, but the file just doesn't seem to be copying over. The machines are connecting, and the drives seem to be mapping, but the file doesn't copy.

if ANY of you have ANY idea, i am willing to try anything at this point.
 
[idea]
Do you have read/write permissions on the destination drive or perhaps just read-only rights?
 
as an admin i should have write, but you think that maybe it? or the mapped drives? the command prompt doesn't stay open long enough for me to see if there is an error.
 
holidayIT,

[1] As pointed out earlier, to make the commandprompt stay (for debugging purpose), you can use /k rather than /c. Was that advice taken?

[2] You can always use scripting.filesystemobject to handle the xcopy part. It has the advantage of staying in the uniform working environment. If you need this revision, let the forum know.

[3] For immediate debugging purpose, I would suggest you make sure the process sees drives U: and Y: and that "U:\" & myfile is well-defined. For this particular purpose, insert these lines before the .run xcopy.
Code:
[COLOR=blue]'above is the line : CycleService strComputerName,"FSPM",STOP_SERVICE,False[/color]

'make sure there is "on error resume next" above
'this seems to be the case of your script
dim oitem, fso
set fso=createobject("scripting.filesystemobject")
set oitem=fso.getdrive("U:")
if err<>0 then
    wscript.echo "U: drive is not recognized. XCopy would fail."
    err.clear
end if
set oitem=fso.getdrive("Y:")
if err<>0 then
    wscript.echo "Y: drive is not recognized. XCopy would fail."
    err.clear
end if
set oitem=fso.getfile("U:\" & myfile)
if err<>0 then
    wscript.echo "U:\<myfile> is not recognized. XCopy would fail."
    err.clear
end if
set oitem=nothing : set fso=nothing

[COLOR=blue]'below is the line : Call WSHShell.Run("cmd.exe /C xcopy U:\" & myFile & " Y:\")[/color]
- tsuji
 
Thanks for the code, it's awesome. Now i know the problem is the mapped drives. they are not working, and i am not sure why. any ideas?
 
holidayIT,

As I mentioned previously, "you need to assure the network resource is actually started, the folder is shared".

You can try to run this to test whether the intended u: resource is actually shared.
Code:
'reference to your variable : sourceDir="\\server\folder\"
set fsvc=getobject("WinNT://" & server & "/LanmanServer")
ofs.filter=array("FileShare")
for each ores in fsvc
    wscript.echo ores.name
next
set fsvc=nothing
Do you see \\server\folder being shown?

- tsuji
 
The sourcedir is in a shared folder. It is available for all on the network. It shows up with the code you gave me (thanks btw - i can definately use that code somewhere else also).

Even though i am not using those drive letters, might i have to remove all of my other mapped drives first? (i have no idea why this might help, but at this point i'll try anything)
 
holidayIT,

In that case, click out the commandprompt window. Issue something like:
xcopy \\server\folder\filename c:(just being figurative here, see what I mean?) Does it work?

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top