PDA

View Full Version : Calling var from text file



impu007
05-25-2004, 01:47 PM
I am trying to call various servers from a text file rather than clobbering all in the main script. Script suppose to call list of servers and go to it and copy iis logs files and provide success/failure report. Some servers require authentication and some doesn't... (I map a drive and then disconnect after the copy operation).


This is what I have in the script...

ServerListFile = "c:\iislogs\ServerList.txt"
TargetFolder = "\\hercules3107\logs"

Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set SLF = FSO.OpenTextFile(ServerListFile, ForReading)

Do While Not SLF.AtEndOfStream

s = SLF.ReadLine
If s = "" Then
Else

MyArray = split(s,",")
ServerName = Trim(MyArray(0))
LogFolderPath = Trim(MyArray(1))
End If

StrFile = "oz" & ".log"


'##################server: wblogic10a######################
'Set the config for weblogic server1
Dim objNet
Set objNet= CreateObject("WScript.Network")
objNet.MapNetworkDrive "w:", "\\wblogic10a\C$\WINDOWS\system32\LogFiles\W3SVC1", false
SourceFolder = "W:"
SourceFile = SourceFolder & "\" & StrFile
DestFile = TargetFolder & "\" & "wblogic10a" & "\" & StrFile
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.GetFile(SourceFile)

'Trap "File not found" errors
If Err.number <> 0 Then
Operation = "find"
ErrorLog = ErrorLog & varError("wblogic10a", Err.number, Err.description, Operation)
End If

' Copy the file to the target folder and trap any errors
MyFile.Copy DestFile, True

If Err.number <> 0 Then
Operation = "copy"
ErrorLog = ErrorLog & varError("wblogic10a", Err.number, Err.description, Operation)
End If

'Delete the original file and trap any errors
'MyFile.Delete

'If Err.number <> 0 Then
'Operation = "delete"
'ErrorLog = ErrorLog & varError("wblogic10a", Err.number, Err.description, Operation)
'End If

objNet.RemoveNetworkDrive "w:", true, false

'##################server: wblogic20a######################
'Set the config for weblogic server2
Dim objNet
Set objNet= CreateObject("WScript.Network")
objNet.MapNetworkDrive "w:", "\\wblogic20a\C$\WINDOWS\system32\LogFiles\W3SVC1", false
SourceFolder = "W:"
SourceFile = SourceFolder & "\" & StrFile
DestFile = TargetFolder & "\" & "wblogic20a" & "\" & StrFile
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.GetFile(SourceFile)

'Trap "File not found" errors
If Err.number <> 0 Then
Operation = "find"
ErrorLog = ErrorLog & varError("wblogic20a", Err.number, Err.description, Operation)
End If

' Copy the file to the target folder and trap any errors
MyFile.Copy DestFile, True

If Err.number <> 0 Then
Operation = "copy"
ErrorLog = ErrorLog & varError("wblogic20a", Err.number, Err.description, Operation)
End If

'Delete the original file and trap any errors
'MyFile.Delete

'If Err.number <> 0 Then
'Operation = "delete"
'ErrorLog = ErrorLog & varError("wblogic20a", Err.number, Err.description, Operation)
'End If

objNet.RemoveNetworkDrive "w:", true, false

Loop
SLF.Close
'Release the objects
Set fso = Nothing
Set MyFile = Nothing


objNet.RemoveNetworkDrive "w:", true, false



The above serverlist.txt file contains the following...


wblogic10a,C$\WINDOWS\system32\LogFiles\W3SVC1
wblogic20a,C$\WINDOWS\system32\LogFiles\W3SVC1





"When all else fails, take a break and smoke a bud"

Brf
05-26-2004, 11:18 AM
I dont know about the authentication part, but you shouldnt have to map a network drive. The scripting file object can access a network share by UNC.

impu007
05-26-2004, 12:51 PM
So, how would you call it from a text file and call that for repeat action.

Say I have 5 servers wblogic1, wblogic2, wblogic3 and so on....

Now, I want to make sure the script first copies file from wblogic1 and when done go to wblogic2 and so on...


Looking at the code, how can I not hardcode the servername but a varable that basically calls servername (and logpath) form a text file.

'Set the config for weblogic server1
Dim objNet
Set objNet= CreateObject("WScript.Network")
objNet.MapNetworkDrive "w:", "\\wblogic10a\C$\WINDOWS\system32\LogFiles\W3SVC1", false
SourceFolder = "W:"
SourceFile = SourceFolder & "\" & StrFile
DestFile = TargetFolder & "\" & "wblogic10a" & "\" & StrFile



"When all else fails, take a break and smoke a bud"

Brf
05-26-2004, 02:23 PM
How about:

sourcefolder = "\\" & servername & "\C$\WINDOWS\system32\LogFiles\W3SVC1"

instead of that
sourcefolder = "W:"
line. You have already put the name of your server into the variable "servername" when you read your file.... Then you can call the filecopy chunk as a subroutine.

impu007
05-28-2004, 10:58 AM
Definitely smart thinking there Brf, but unfortunately the path in many case aren't the same.

Say for example, in the serverlist.txt file (where I want to call the servers from has the two server listed:)

(one is w2k and another one is w2k3 server)

wblogic10a,C$\winnt\system32\LogFiles\W3SVC1
wblogic20a,C$\WINDOWS\system32\LogFiles\W3SVC1

how do you allow script to perform the action on first server and when done, move to the next server in the serverlist.txt.

Also, how do you suggest I ensure it completes one before moving on to the other server? Can you talk about the subroutine a little more? May be a snippet :)

"When all else fails, take a break and smoke a bud"

Brf
05-28-2004, 12:01 PM
Yeah.... you are reading your filepath in too, remember?

So the line should read:

sourcefolder = "\\" & servername & "\" & Logfolderpath

You can put the stuff between the
######
######
in a subroutine and call it when you set your filenames.... like this:

Set SLF = FSO.OpenTextFile(ServerListFile, ForReading)

s = SLF.ReadAll

MyLineArray = split(s,vbcrlf)
for each myline in mylinearray
MyArray = split(myline,",")
ServerName = Trim(MyArray(0))
LogFolderPath = Trim(MyArray(1))
processfiles
next

sub processfiles()
sourcefolder = "\\" & servername & "\" & Logfolderpath

**** stuff between the ####
end sub