Posts Tagged ‘Scripts’

Posted by keith.wirch at March 23, 2013

Category: Scripts, Visual Basic

Tags:

This is a script that can be used to push out files and folders to computer across a network using computer names located within a text file, one by one. The script takes the name from the file and turns it into \\\c$\path\to\destination\. It is executed through Visual Basic’s Scripting Host (VBS). It will output what is currently happening and the outcome of that computer. If it fails, it will continue on to the next computer.

For those of you who can read the comments decipher what to do, go for it. It’s quite simple. If you need further explanation, keep reading.

Const ForReading = 1
Const ForWriting = 2
Const OverwriteExisting = True

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get list of Computers
Set objFile = objFSO.OpenTextFile("C:\Scripts\Computers.txt")
Set fso = CreateObject("Scripting.FileSystemObject")

' Put Errors in Text File
ErrorFile = "C:\Scripts\ErrorLog-FilePush.txt"
Set ErrorsFound = fso.OpenTextFile(ErrorFile, ForWriting, True)

'Keep going through the list until the end
Do Until objFile.AtEndOfStream
	strComputer = objFile.Readline
	WScript.Echo "Now working on " & strComputer
	strRemoteFile = "\\" & strComputer & "\c$\path\to\destination"
'To push folder make this "objFSO.CopyFolder"
'To push files make this "objFSO.CopyFile"
'Change filepath there were the source of the files are.
	objFSO.CopyFile "C:\path\to\source", strRemoteFile, OverwriteExisting
' If Computer is not found, goto next.  If is found, say yay!
	If Err = 0 Then
		Wscript.echo "Success.  Yay!"
'Otherwise print "Fail.  BOOO!!! , note it in ErrorFile and goto next"
	Else
		Wscript.echo "Fail.  BOOO!!!"
		ErrorsFound.WriteLine(strComputer & " Failed")
	End If
On Error Resume Next
Loop

ErrorsFound.WriteLine ("=======  Script Ended ========")

Step 1: Create a folder named “Scripts” in “C:\” This is decent place to keep scripts that you collect. We will be working from this scripts folder, so any more files you create can be placed here in the “Scripts” folder.

Step 2: Open notepad (or your favorite text editor like notepad++) and copy/paste the code from above. Save the file in your new folder.

Step 3: Make two more text files named “computers.txt” and “ErrorLog-FilePush.txt” and have them in the same folder.

Step 4: On line 18 change “path\to\destination” to the actual destination on your remote computers.

Step 5: On line 22 change “path\to\source” to your actual source file or folders.  Take a look at “objFSO.CopyFile”.  If you are pushing a whole folder to your destination, you need to change this too “objFSO.CopyFolder”.

Step 6: Once you have all that configured, open a command prompt in the C:\Scripts folder and execute the command “cscript FilePush.vbs” and the script will start.  Any errors that happen will show in ErrorLog-FilePush.txt.

The script should execute with something like this.  (Computer names are blocked out to protect the innocent.)

(Note: I didn’t put “cscript” infront of filepush.vbs because running cscript is default behavior of .vbs files on my workstation)

Capture

==========================================
TROUBLESHOOTING
==========================================

I’m getting File not found errors on lines 18 or 22!
Be sure you have the path correct.  I like copying and pasting from windows explorer.  When copying whole folders on like 22, you usually need to an extra backslash at the end.

I’m getting File not found errors on line 12!
This is because the script cannot find the error file to output errors too.  Make sure you created the files in Step 3 in the right place.

I’m getting access denied!
Make sure you have permissions on the remote coomputers.  The script will take your current permissions of the logged in user.  To use the permissions of another user, Shift+Right-Click “cmd” in the start menu and select “Run as different user”