Solved

Copy files from network shares using a vbs script

Posted on 2010-11-24
2
920 Views
Last Modified: 2012-05-10
Hi
Is there a way to modify the below script to include a specific username and password (this user account will have permisiions to access both the source and destination network shares. Thanks

 Dim sOriginFolder, sDestinationFolder, sFile, oFSO, oShell  
 Set oFSO = CreateObject("Scripting.FileSystemObject")
 Set oShell = CreateObject("Wscript.Shell")
 sOriginFolder = "networkfoldera"
 sDestinationFolder = "networkfolderb"
 On Error Resume Next  
  sLastTime = oShell.RegRead("HKLM\SYSTEM\LastTimeCopied")
  iErr = Err.Number
 On Error GoTo 0
  If iErr <> 0 Then
   oShell.RegWrite "HKLM\SYSTEM\LastTimeCopied",Now,"REG_SZ"
   Call CopyFiles("ALL")
  Else
   oShell.RegWrite "HKLM\SYSTEM\LastTimeCopied",Now,"REG_SZ"
   Call CopyFiles(sLastTime)
  End If  
   
 Sub CopyFiles(sArg)
   If sArg = "ALL" Then
     For Each sFile In oFSO.GetFolder(sOriginFolder).Files
       If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then
         oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
        End If
     Next
   Else
     For Each sFile In oFSO.GetFolder(sOriginFolder).Files
       If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then
         If DateDiff("s",sLastTime,oFSO.GetFile(sFile).DateCreated) > 0 Then  
           oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
           
          End If
       End If  
     Next  
   End If  
0
Comment
Question by:kwatt562
2 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 34210701
Hi, you can map a network drive using the alternate credentials:

strUser = "domain\user"
strPass = "password"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "Y:", "\\server\source", False, strUser, strPass
objNetwork.MapNetworkDrive "Z:", "\\server\destination", False, strUser, strPass

Then you can copy files between the drives.

Regards,

Rob.
0
 
LVL 5

Assisted Solution

by:MaxSoullard
MaxSoullard earned 250 total points
ID: 34212507
You can use this script that uses RunAs.

Save your script on the same folder as this script. Instantiate the variables.

The password will be asked once and will be saved by RunAs which is a more secure way than having you password on a script.
strUser = "USER_NAME"
strDomain = "DOMAIN_NAME"
strScriptName = "SCRIPT_NAME.vbs" ' The name of the script you want to run with anotehr account
strCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
strRunAs ="runas /savecred /user:" & strDomain & "\" & strUser&" " & Chr(34)& "cscript.exe "& Chr(34)& strCurPath & "\"& strScriptName & Chr(34)

Set WshShell = CreateObject("WScript.Shell")

WshShell.Run (strRunAs)

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now