• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7614
  • Last Modified:

vbscript to generate script file for Winscp

Hi,
Can you help me with a vbscript for running a Winscp ssh session to a UNIX server to get some of the files to the local C drive?

I have 3 text files with list of servers.
text1:
server01
server02
server03

the full path of the server location in the unix server is /drive/var/ex1/server01,/drive/var/ex1/server02,/drive/var/ex1/server03

text2:
server04
server05

the pull path of the server folder in the server is
/drive/var/ex2/server05,/drive/var/ex2/server05

Winscp script

REM This is the BAT file that we'll run in Windows.
REM It will compress a bigger file, upload all the selected
REM file to the remote server, and clean up the compressed file

REM Set some variables for locations to which we will refer in this
REM script.
set scp_script_dir="C:\scripts"

REM Call the WinSCP script with the necessary login info and you
REM will be prompted for a password.
"C:\Program Files\WinSCP\WinSCP.exe" /console /script=%scp_script_dir%"\winscp_script.txt"

:END

where winscp_script.txt is as below

# Automatically answer all prompts negatively not to stall
# the script on errors
option batch on
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect using a password
# open user:password@example.com
# Connect
open root:test@server
# Change remote directory
cd /vmfs/volumes/ex1/server-01
# Force binary mode transfer
option transfer binary
# Download file to the local directory C:\
get server-01.vmx c:\scripts\test.txt
# Disconnect
close
# Exit WinSCP
exit

I want to generate the winscp_script for the list of servers from each files and that should be used to run the winscp script

regards
Chandru
0
chandru_sol
Asked:
chandru_sol
  • 11
  • 9
1 Solution
 
RobSampsonCommented:
Hi Chandru,

Have a look at this VBS file.

'=================

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Const intForReading = 1

scp_script_dir="C:\scripts"
scp_script_dir="N:\Scripting\Test Scripts\Write_Script_File_Based_On_Computer_Names"

strTextFiles = "text1.txt;text2.txt;text3.txt"
arrTextFiles = Split(strTextFiles, ";")

For intFile = LBound(arrTextFiles) To UBound(arrTextFiles)
      Set objTextFile = objFSO.OpenTextFile(arrTextFiles(intFile), intForReading, False)
      While Not objTextFile.AtEndOfStream
            strServerName = objTextFile.ReadLine
            strScriptText = "# Auto-generated script file For server: " & strServerName & vbCrLf
            strScriptText = strScriptText & "# Automatically answer all prompts negatively not to stall" & vbCrLf
            strScriptText = strScriptText & "# the script on errors" & vbCrLf
            strScriptText = strScriptText & "option batch on" & vbCrLf
            strScriptText = strScriptText & "# Disable overwrite confirmations that conflict with the previous" & vbCrLf
            strScriptText = strScriptText & "option confirm off" & vbCrLf
            strScriptText = strScriptText & "# Connect using a password" & vbCrLf
            strScriptText = strScriptText & "# open user:password@example.com" & vbCrLf
            strScriptText = strScriptText & "# Connect" & vbCrLf
            strScriptText = strScriptText & "open root:test@server" & vbCrLf
            strScriptText = strScriptText & "# Change remote directory" & vbCrLf
            strScriptText = strScriptText & "cd /vmfs/volumes/ex1/" & strServerName & vbCrLf
            strScriptText = strScriptText & "# Force binary mode transfer" & vbCrLf
            strScriptText = strScriptText & "option transfer binary" & vbCrLf
            strScriptText = strScriptText & "# Download file to the local directory C:\" & vbCrLf
            strScriptText = strScriptText & "get " & strServerName & ".vmx c:\scripts\test.txt" & vbCrLf
            strScriptText = strScriptText & "# Disconnect" & vbCrLf
            strScriptText = strScriptText & "close" & vbCrLf
            strScriptText = strScriptText & "# Exit WinSCP" & vbCrLf
            strScriptText = strScriptText & "exit"
            Set objOutputFile = objFSO.CreateTextFile(scp_script_dir & "\winscp_script.txt", True)
            objOutputFile.Write strScriptText
            objOutputFile.Close
            Set objOutputFile = Nothing
            MsgBox "About to run:" & VbCrLf & """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_script.txt" & _
                  VbCrLf & "When you click OK, the winscp_script.txt file will be displayed for you to check, then when you close it, you will be asked whether to run that script."
            objShell.Run "notepad """ & scp_script_dir & "\winscp_script.txt" & """", 1, True
            strResponse = MsgBox("Do you want to run the winscp_script.txt file?", vbYesNo, "Run Script File?")
            If strResponse = vbYes Then
                  objShell.Run """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_script.txt", 1, True
            End If
      Wend
      objTextFile.Close
      Set objTextFile = Nothing
Next

MsgBox "Done"
'==============

I couldn't see any reference to the EX2 folder, so i just copied the text from your winscp_script.txt file, and used the script to place in the server names.

Regards,

Rob.
0
 
chandru_solAuthor Commented:
Hi Rob,
Nice to see you back. I will try the script and post the results

regards
Chandru
0
 
chandru_solAuthor Commented:
Hi Rob,

I have checked the script and it is working fine. Can you help me with some changes?

The file names will ex1, ex2, ex3, ex4 and the same should be used in this location of the text file that is created cd /vmfs/volumes/ex1/.

Can we alos generate the name of the text files from a specific folder c:\scripts instead of manually entering the information as below?
TextFiles = "text1.txt;text2.txt;text3.txt"

The servername ends with a slash "/" can this be removed before passing to create the script file

regards
Chandru
0
[Video] Oticon Case Study

Open office environments can create the dynamics for innovation, but they also bring some challenges. With over 1,000 employees in an open office, Oticon needed a solution that would preserve the environment while mitigating disruptive background noises.

Watch how they did it.

 
RobSampsonCommented:
Chandru,

I have made the changes according to points 2 and 3 above, but not the first one yet.  I have changed the script to read all of the *.txt files from the scp_script_dir folder, which is C:\Scripts

For this to work however, I have had to change the name of the winscp_script.txt to winscp_script.scp, just so that it is no longer a txt file, otherwise the script tries to generate based on the text within it's own script.....

I have also added a line to remove any trailing slash from the server name.

As for the first point, do you want to go to the ex1,ex2, ex3, and ex4 for each server, meaning you would have
cd /vmfs/volumes/ex1/Server01
get Server01.vmx c:\scripts\test.txt
cd /vmfs/volumes/ex2/Server01
get Server01.vmx c:\scripts\test.txt
cd /vmfs/volumes/ex3/Server01
get Server01.vmx c:\scripts\test.txt
cd /vmfs/volumes/ex4/Server01
get Server01.vmx c:\scripts\test.txt

I'm not quite sure on that one.....

'==============
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Const intForReading = 1

scp_script_dir="C:\scripts"

For Each objFile In objFSO.GetFolder(scp_script_dir).Files
      If LCase(Right(objFile.Name, 4)) = ".txt" Then
            Set objTextFile = objFSO.OpenTextFile(objFile.Name, intForReading, False)
            While Not objTextFile.AtEndOfStream
                  strServerName = objTextFile.ReadLine
                  If Right(strServerName, 1) = "\" Then strServerName = Left(strServerName, Len(strServerName) - 1)
                  strScriptText = "# Auto-generated script file For server: " & strServerName & vbCrLf
                  strScriptText = strScriptText & "# Automatically answer all prompts negatively not to stall" & vbCrLf
                  strScriptText = strScriptText & "# the script on errors" & vbCrLf
                  strScriptText = strScriptText & "option batch on" & vbCrLf
                  strScriptText = strScriptText & "# Disable overwrite confirmations that conflict with the previous" & vbCrLf
                  strScriptText = strScriptText & "option confirm off" & vbCrLf
                  strScriptText = strScriptText & "# Connect using a password" & vbCrLf
                  strScriptText = strScriptText & "# open user:password@example.com" & vbCrLf
                  strScriptText = strScriptText & "# Connect" & vbCrLf
                  strScriptText = strScriptText & "open root:test@server" & vbCrLf
                  strScriptText = strScriptText & "# Change remote directory" & vbCrLf
                  strScriptText = strScriptText & "cd /vmfs/volumes/ex1/" & strServerName & vbCrLf
                  strScriptText = strScriptText & "# Force binary mode transfer" & vbCrLf
                  strScriptText = strScriptText & "option transfer binary" & vbCrLf
                  strScriptText = strScriptText & "# Download file to the local directory C:\" & vbCrLf
                  strScriptText = strScriptText & "get " & strServerName & ".vmx c:\scripts\test.txt" & vbCrLf
                  strScriptText = strScriptText & "# Disconnect" & vbCrLf
                  strScriptText = strScriptText & "close" & VbCrLf
                  strScriptText = strScriptText & "# Exit WinSCP" & vbCrLf
                  strScriptText = strScriptText & "exit"
                  Set objOutputFile = objFSO.CreateTextFile(scp_script_dir & "\winscp_script.scp", True)
                  objOutputFile.Write strScriptText
                  objOutputFile.Close
                  Set objOutputFile = Nothing
                  MsgBox "About to run:" & VbCrLf & """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_script.scp" & _
                        VbCrLf & "When you click OK, the winscp_script.scp file will be displayed for you to check, then when you close it, you will be asked whether to run that script."
                  objShell.Run "notepad """ & scp_script_dir & "\winscp_script.scp" & """", 1, True
                  strResponse = MsgBox("Do you want to run the winscp_script.scp file?", vbYesNo, "Run Script File?")
                  If strResponse = vbYes Then
                        objShell.Run """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_script.scp", 1, True
                  End If
            Wend
            objTextFile.Close
            Set objTextFile = Nothing
      End If
Next

MsgBox "Done"
'============

Regards,

Rob.
0
 
chandru_solAuthor Commented:
Hi Rob,

Thanks for that.

The text file ex1 will have list of servers.
ex1:
server1
server2

cd /vmfs/volumes/ex1/Server1
cd /vmfs/volumes/ex1/Server2
ex2:
server3
server4
cd /vmfs/volumes/ex1/Server3
cd /vmfs/volumes/ex1/Server4

please let me know if this is confusing

regards
Chandru
0
 
chandru_solAuthor Commented:
Sorry the above ex2 file should be

ex2:
server3
server4
cd /vmfs/volumes/ex2/Server3
cd /vmfs/volumes/ex2/Server4

regards
Chandru
0
 
RobSampsonCommented:
From what I understand here is that you will have the following text files:
ex1.txt, which contains:
server01
server02

ex2.txt, which contains
server03
server04

Then, in the winscp_script file, you want to go to the ex1 directory for those servers in that ex1.txt file, and you want to go to the ex2 directory for those servers in that ex2.txt file.

Is that correct?  If so, I will change the line:
strScriptText = strScriptText & "cd /vmfs/volumes/ex1/" & strServerName & vbCrLf

so that the "ex#" part is taken from the filename of the text file.

Regards,

Rob.
0
 
chandru_solAuthor Commented:
Yes that is the requirement.

Can you also change the C:\scripts\test.txt to C:\scripts\servername.txt

regards
Chandru
0
 
RobSampsonCommented:
OK then, try this one:

'======================

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Const intForReading = 1

scp_script_dir="C:\scripts"

For Each objFile In objFSO.GetFolder(scp_script_dir).Files
      If LCase(Right(objFile.Name, 4)) = ".txt" Then
            Set objTextFile = objFSO.OpenTextFile(objFile.Name, intForReading, False)
            While Not objTextFile.AtEndOfStream
                  strServerName = objTextFile.ReadLine
                  If Right(strServerName, 1) = "\" Then strServerName = Left(strServerName, Len(strServerName) - 1)
                  strScriptText = "# Auto-generated script file For server: " & strServerName & vbCrLf
                  strScriptText = strScriptText & "# Automatically answer all prompts negatively not to stall" & vbCrLf
                  strScriptText = strScriptText & "# the script on errors" & vbCrLf
                  strScriptText = strScriptText & "option batch on" & vbCrLf
                  strScriptText = strScriptText & "# Disable overwrite confirmations that conflict with the previous" & vbCrLf
                  strScriptText = strScriptText & "option confirm off" & vbCrLf
                  strScriptText = strScriptText & "# Connect using a password" & vbCrLf
                  strScriptText = strScriptText & "# open user:password@example.com" & vbCrLf
                  strScriptText = strScriptText & "# Connect" & vbCrLf
                  strScriptText = strScriptText & "open root:test@server" & vbCrLf
                  strScriptText = strScriptText & "# Change remote directory" & vbCrLf
                  strScriptText = strScriptText & "cd /vmfs/volumes/" & Replace(objFile.Name, ".txt", "") & "/" & strServerName & vbCrLf
                  strScriptText = strScriptText & "# Force binary mode transfer" & vbCrLf
                  strScriptText = strScriptText & "option transfer binary" & vbCrLf
                  strScriptText = strScriptText & "# Download file to the local directory C:\" & vbCrLf
                  strScriptText = strScriptText & "get " & strServerName & ".vmx c:\scripts\" & strServerName & ".txt" & vbCrLf
                  strScriptText = strScriptText & "# Disconnect" & vbCrLf
                  strScriptText = strScriptText & "close" & VbCrLf
                  strScriptText = strScriptText & "# Exit WinSCP" & vbCrLf
                  strScriptText = strScriptText & "exit"
                  Set objOutputFile = objFSO.CreateTextFile(scp_script_dir & "\winscp_script.scp", True)
                  objOutputFile.Write strScriptText
                  objOutputFile.Close
                  Set objOutputFile = Nothing
                  MsgBox "About to run:" & VbCrLf & """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_script.scp" & _
                        VbCrLf & "When you click OK, the winscp_script.scp file will be displayed for you to check, then when you close it, you will be asked whether to run that script."
                  objShell.Run "notepad """ & scp_script_dir & "\winscp_script.scp" & """", 1, True
                  strResponse = MsgBox("Do you want to run the winscp_script.scp file?", vbYesNo, "Run Script File?")
                  If strResponse = vbYes Then
                        objShell.Run """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_script.scp", 1, True
                  End If
            Wend
            objTextFile.Close
            Set objTextFile = Nothing
      End If
Next

MsgBox "Done"
'=====================

Regards,

Rob.
0
 
chandru_solAuthor Commented:
Hi Rob,

The script seems to be working fine. Can you please help me in running the script without opening the notepad everytime and clicking yes for making the winscp script to run.

regards
Chandru
0
 
RobSampsonCommented:
Sure, that was just for debugging, to make sure we were running the right things.
Just do a search through the file for MsgBox and then place an apostrophe ( ' ) at the start of that line to comment it out.

I have done that for you here:
'============
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Const intForReading = 1

scp_script_dir="C:\scripts"

For Each objFile In objFSO.GetFolder(scp_script_dir).Files
      If LCase(Right(objFile.Name, 4)) = ".txt" Then
            Set objTextFile = objFSO.OpenTextFile(objFile.Name, intForReading, False)
            While Not objTextFile.AtEndOfStream
                  strServerName = objTextFile.ReadLine
                  If Right(strServerName, 1) = "\" Then strServerName = Left(strServerName, Len(strServerName) - 1)
                  strScriptText = "# Auto-generated script file For server: " & strServerName & vbCrLf
                  strScriptText = strScriptText & "# Automatically answer all prompts negatively not to stall" & vbCrLf
                  strScriptText = strScriptText & "# the script on errors" & vbCrLf
                  strScriptText = strScriptText & "option batch on" & vbCrLf
                  strScriptText = strScriptText & "# Disable overwrite confirmations that conflict with the previous" & vbCrLf
                  strScriptText = strScriptText & "option confirm off" & vbCrLf
                  strScriptText = strScriptText & "# Connect using a password" & vbCrLf
                  strScriptText = strScriptText & "# open user:password@example.com" & vbCrLf
                  strScriptText = strScriptText & "# Connect" & vbCrLf
                  strScriptText = strScriptText & "open root:test@server" & vbCrLf
                  strScriptText = strScriptText & "# Change remote directory" & vbCrLf
                  strScriptText = strScriptText & "cd /vmfs/volumes/" & Replace(objFile.Name, ".txt", "") & "/" & strServerName & vbCrLf
                  strScriptText = strScriptText & "# Force binary mode transfer" & vbCrLf
                  strScriptText = strScriptText & "option transfer binary" & vbCrLf
                  strScriptText = strScriptText & "# Download file to the local directory C:\" & vbCrLf
                  strScriptText = strScriptText & "get " & strServerName & ".vmx c:\scripts\" & strServerName & ".txt" & vbCrLf
                  strScriptText = strScriptText & "# Disconnect" & vbCrLf
                  strScriptText = strScriptText & "close" & VbCrLf
                  strScriptText = strScriptText & "# Exit WinSCP" & vbCrLf
                  strScriptText = strScriptText & "exit"
                  Set objOutputFile = objFSO.CreateTextFile(scp_script_dir & "\winscp_script.scp", True)
                  objOutputFile.Write strScriptText
                  objOutputFile.Close
                  Set objOutputFile = Nothing
                  'MsgBox "About to run:" & VbCrLf & """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_script.scp" & _
                  '      VbCrLf & "When you click OK, the winscp_script.scp file will be displayed for you to check, then when you close it, you will be asked whether to run that script."
                  'objShell.Run "notepad """ & scp_script_dir & "\winscp_script.scp" & """", 1, True
                  'strResponse = MsgBox("Do you want to run the winscp_script.scp file?", vbYesNo, "Run Script File?")
                  'If strResponse = vbYes Then
                        objShell.Run """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_script.scp", 1, True
                  'End If
            Wend
            objTextFile.Close
            Set objTextFile = Nothing
      End If
Next

'MsgBox "Done"
'==========

Regards,

Rob.
0
 
chandru_solAuthor Commented:
Hi Rob,

Thanks again. I was running a batch file and calling this vbscript as part of this script. Can this also be done with vbscript.

batch file below
start /wait Putty.exe root@server -pw root -m c:\scripts\winscp\getfiles.txt

REM WINSCP to local drive
REM Set some variables for locations to which we will refer in this
REM script.
set scp_script_dir="C:\scripts\winscp"

REM Call the WinSCP script with the necessary login info and you
REM will be prompted for a password.
"C:\Program Files\WinSCP\WinSCP.exe" /console /script=%scp_script_dir%"\winscp_getfiles.txt"
call c:\scripts\esx\win2.vbs

regards
Chandru
0
 
RobSampsonCommented:
Yes, here is the direct translation of that batch file:
'=============
strCommand = """C:\Program Files\Putty\Putty.exe"" root@server -pw root -m c:\scripts\winscp\getfiles.txt"
objShell.Run strCommand, 1, True

REM WINSCP to local drive
REM Set some variables for locations to which we will refer in this
REM script.
scp_script_dir="C:\scripts\winscp"

REM Call the WinSCP script with the necessary login info and you
REM will be prompted For a password.
strCommand = """C:\Program Files\WinSCP\WinSCP.exe"" /console /script=" & scp_script_dir & "\winscp_getfiles.txt"
objShell.Run strCommand, 1, True
strCommand = "Wscript c:\scripts\esx\win2.vbs"
objShell.Run strCommand, 1, True
'============

Regards,

Rob.
0
 
chandru_solAuthor Commented:
Hi Rob,

Thanks. you are awesome!
But i get one error in Line 9 of the older script when i use the above script to call this script

Error: File not Found
Code: 800A0035

regards
Chandru
0
 
RobSampsonCommented:
Is the folder with the EX files still at
"C:\scripts"

If not, change the line near the top that says:
scp_script_dir="C:\scripts"

Or if that hasn't changed, try changing this:
Set objTextFile = objFSO.OpenTextFile(objFile.Name, intForReading, False)

to this:
Set objTextFile = objFSO.OpenTextFile(objFile.Path, intForReading, False)

Regards,

Rob.
0
 
chandru_solAuthor Commented:
the second suggestion did the trick

Can let me know any location to know and understand vbscripting?

regards
Chandru
0
 
RobSampsonCommented:
Hi, it takes a while, and is just practice and research......
First, I would suggest downloading and using VBSEdit 3.3 from www.vbsedit.com as it gives a fantastic environment to work in, and also gives you a lot of samples and other stuff.

Then, there's a whole lot more samples here:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/all.mspx

And a getting started guide here (and lots of other bits to read):
http://www.microsoft.com/technet/scriptcenter/default.mspx

Oh, and the VB Language Reference:
http://msdn2.microsoft.com/en-us/library/d1wf56tt.aspx

Then, if you search some terms in Google along with the word vbscript, you can pretty find info on anything to give you a start.

Hope that helps.

Regards,

Rob.
0
 
chandru_solAuthor Commented:
Thanks Rob.

That will help me to start on learning vbscript.

Your are very helpful

regards
Chandru
0
 
RobSampsonCommented:
No problem.  There's also some good scripts here:
http://www.computerperformance.co.uk/vbscript/index.htm

Rob.
0
 
chandru_solAuthor Commented:
Thanks. I have given the points to you
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now