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

VBScript to copy files

Is there any way in VBScript to copy a file from one machine to another and then execute that file.  I understand that FSO.CopyFile "\\file to be copied path", "\\destination directory" only works on the local machine, so any ideas would be great.

Thanks in advance,

Aaron
0
aaronbg
Asked:
aaronbg
  • 11
  • 9
  • 3
1 Solution
 
aaronbgAuthor Commented:
I think I have found a solution to my own problem anyone out there let me know what you think....this is just psuedo code to check my logic, any comments are welcome

*********VBScript************

Map network drive   \ \computername\c$ = to X:\

Copy File C:\printer.bat  to X:\

      *Sub*
      execute printer.bat via \ \computername\c$\printer.bat

      pause 5 secs

            **********Batch run on computername, tested and works*******************
                        Could also install printer with other switches

            RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /y /n /c[\\Carsdmwrwkx0504\HPLaserJ]
            
            ************************************************************************

      *EndSub*

Delete File X:\printer.bat

*****************************
0
 
David LeeCommented:
Hi, Aaron.

Are you saying that you want to execute the file on the remote computer after the copy?  Is the copy merely to get that file to the remote computer for execution?  If yes, then you can do this very easily using PSEXEC (http://www.microsoft.com/technet/sysinternals/utilities/psexec.mspx).
0
 
aaronbgAuthor Commented:
I have tried PSEXEC but the process just hangs...I can see it in task manager but the batch file wont run...eventually have to kill PSEXEC process
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
David LeeCommented:
I've seen that happen too.  It's typically when the batch file contains a command that asks a question or causes a dialog-box to pop up.  It sits there waiting endlessly for a response that will never come.
0
 
RobSampsonCommented:
PSExec cannot execute remote process via a UNC path.  You would need to have the batch file on the local computer, then use PSExec to run it, when the client computer runs your script.
0
 
aaronbgAuthor Commented:
Thanks Guys,

But I am getting a little confused.  ComputerA is my workstation.  I want to run a script from ComputerA to install and set default printer on ComputerB (in futer would be an array of computers).  With that in mind let me see if I understand what you are telling me.  The Bat needs to be on ComputerB and I run PSEXEC from Computer A.  Then I would run "psexec \\ComputerB C:\printer.bat"  from ComputerA.  If this is correct, the next step I would want to do is have a script call the psexec command after the copy of printer.bat to ComputerB.  

Thanks Again

Aaron
0
 
David LeeCommented:
Hi, Aaron.

You're close.  You run PSEXEC from Computer A telling it to copy Printer.Bat to ComputerB and then run it on ComputerB.  PSEXEC also accepts a list of computer names to perform these actions on.  I used PSEXEC about an hour ago to install a program on six remote computers.  Here's the command line I used:

psexec @C:\Systems.txt -c -w c:\load -u mydomain\username -p password c:\eetesting\installprogram.cmd

This command-line tells PSEXEC to copy the file installprogram.cmd to the c:\load folder on each of the computers listed in the file Systems.txt, then execute that file using the username and password specified.
0
 
RobSampsonCommented:
Hmmm,  I've just ran into a problem with this.
From ComputerA, when you use PSExec to run the file sitting on ComputerB, it runs that using the user accounts credentials, that are currently logged into ComputerA.  This is a problem with network printers, because the printer connection must be made under the specific profile it is required for.

I am trying to find out if you can run the process as the logged in user of ComputerB.

In the meantime, if your local pc script (MapPrinterOnLocal.vbs) has the following code:
'==============================
Dim objFSO, wshNetwork
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set wshNetwork = CreateObject("WScript.Network")
'************************* MAP PRINTERS *************************
wshNetwork.AddWindowsPrinterConnection "\\MCCDC02\RingwoodCivic_Finance1"
wshNetwork.AddWindowsPrinterConnection "\\MCCDC02\RingwoodCivic_CommunityServices3"
'************************* SET DEFAULT PRINTER *************************
wshNetwork.SetDefaultPrinter "\\MCCDC02\RingwoodCivic_Finance1"
'=============================

you could create a batch file that runs:
wscript C:\MapPrinterOnLocal.vbs
and get the USER to run that.

I'll keep you posted if I find anything.

Rob.
0
 
RobSampsonCommented:
Hmmm, from what I've been trying, this can't be done.  Of course if you had the above script in a logon script, that would run under the profile of the logged in user, and wouldn't have a problem.
If you really wanted a challenge, you could try to obtain the SID of the domain user, and add the printer keys to the registry under HKEY_USERS\{users sid} but that seems like overkill.
In a log on script, you can see if an AD user is part of a specific user group, and map printers accordingly, which is probably better in the long run, than hitting user's remotely on demand.

Rob.
0
 
aaronbgAuthor Commented:
Once again thanks to all.  Rob, as far as adding the printer for all users I believe that this line below put into a bat or called from a script to run against the remote computer will add for all users.  When I looked it up the /ga option is global add, so unless I missed the boat I think it will add and set as default for all users.

 RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /y /n /c "\\Server\Printer"

Let me know what you think.

Aaron
0
 
RobSampsonCommented:
Aaron,
Nice one!  Sorry, I missed that command in your question!  That does actually work in mapping a printer to all users, but unfortunately is doesn't work for remote machines using the /c switch.  I have tried lots of options, but couldn't get it work.  I can get it to work using PSEXEC and a VBScript.

This script is a self-calling file, accepting a parameter that tells it if it is has been called using administrator credentials.  So when you first run it, it has no arguments passed to it, so it asks you enter the name of a remote PC to install an All Users printer to.
Then the script runs PSExec against that computer, supplying Admin credentials.  The command that PSExec then runs (with Admin rights) is wscript to call the script again, this time passing an argument (parameter) of "AsAdmin" to the script knows it has admin rights.
Now the vbs script is running as Admin from the target pc, therefore, you can issue the add printer command as if you were at the target pc.
'============================================
Option Explicit

Dim strArgs, strAdminUser, strAdminPass
Dim objFSO, wshNetwork, strComputer, objShell, strCommand

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set wshNetwork = WScript.CreateObject("WScript.Network")
Set objShell = WScript.CreateObject("WScript.Shell")

strAdminUser = "YourDomainAdminAccount"
strAdminPass = "YourDomainPassword"

If WScript.Arguments.Count < 1 Then
      Call Normal_User_Commands
ElseIf WScript.Arguments(0) = "AsAdmin" Then
      Call Admin_User_Commands
Else
      MsgBox "Unknown Argument received"
End If

Sub Normal_User_Commands
      'MsgBox "Running as initiating user"
      strComputer = InputBox("Enter computer name to map a printer to:", "Enter Computer", "172.16.2.40")
      strCommand = "cmd /c \\server\share\temp\test\psexec.exe \\" & strComputer & " -i -u " & strAdminUser & " -p " & strAdminPass & " wscript.exe \\server\share\temp\test\SetDefaultPrinter_on_Remote_PC.vbs ""AsAdmin"""
      objShell.Run strCommand, 0, True
End Sub

Sub Admin_User_Commands
      'Now running as Administrator on the target macchine
      'MsgBox "Running as Admin"
      strCommand = "cmd /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /in /j ""LanMan Print Services"" /n ""\\MCCDC02\RingwoodCivic_Finance1"""
      objShell.Run strCommand, 0, True
End Sub
'==================================
0
 
aaronbgAuthor Commented:
I must be missing something here because nothing is happening, no errors just comes up with the input box for computer name and then nothing..Here is the script after I put my specifics in


'============================================
Option Explicit

Dim strArgs, strAdminUser, strAdminPass
Dim objFSO, wshNetwork, strComputer, objShell, strCommand

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set wshNetwork = WScript.CreateObject("WScript.Network")
Set objShell = WScript.CreateObject("WScript.Shell")

strAdminUser = "aaron.boyd.gardner"
strAdminPass = "!1r@Q2dB3t4!!"

If WScript.Arguments.Count < 1 Then
      Call Normal_User_Commands
ElseIf WScript.Arguments(0) = "AsAdmin" Then
      Call Admin_User_Commands
Else
      MsgBox "Unknown Argument received"
End If

Sub Normal_User_Commands
      'MsgBox "Running as initiating user"
      strComputer = InputBox("Enter computer name to map a printer to:", "Enter Computer")
      strCommand = "cmd /c \\carsdmwrwkx0504\scripts\psexec.exe \\" & strComputer & " -i -u " & strAdminUser & " -p " &

strAdminPass & " wscript.exe \\carsdmwrwkx0504\scripts\RemoteSetDefaultPrinterV6.vbs ""AsAdmin"""
      objShell.Run strCommand, 0, True
End Sub

Sub Admin_User_Commands
      'Now running as Administrator on the target macchine
      'MsgBox "Running as Admin"
      strCommand = "cmd /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /y /n ""\\Carsdmwrwkx0504\HPLaserJ"""
      objShell.Run strCommand, 0, True
End Sub
'==================================


Not sure if I did something wrong to jack it up or what...peruse at your leisure and let me know, big thanks Rob!!!



Aaron
0
 
RobSampsonCommented:
OK, where you have cmd /c, change it to cmd /k, and also change the wshShell.Run strCommand, 0, True, to wshShell.Run strCommand, 1, True.
This will give you some output on both your machine, and your target machine.
One thing you need to bare in mind is that when you run it on your computer, against a remote computer, the account that you run it under needs to be an account in the remote PCs local Administrator's group, otherwise you will get Access Denied on the PSExec command.
If your user account is a domain admin account, then Domain Users should be in the remote PCs local administrators group, and all should be good.

Rob.
0
 
RobSampsonCommented:
Oops, I meant "If your user account is a domain admin account, then Domain ADMINS should be in the remote PCs local admin group"
Also, I assume that as you are calling RemoteSetDefaultPrinterV6.vbs in your script, that the scripts file itself is called RemoteSetDefaultPrinterV6.vbs.  That must be the case for this script to work.  Also, I haven't fully tested this, but is PSExec.exe accessible by the target machine?  Would there be any permission problems accessing PSExec?

Rob.
0
 
aaronbgAuthor Commented:
still got an error...it happens when PSExec starts its actions


PsExec could not start wscript.exe on carsdmwrwkx0505:
Logon failure: unknown user name or bad password.

I have ensured that my username and password are correct so I am not sure why I get this.
0
 
RobSampsonCommented:
Try setting:
strAdminUser = "aaron.boyd.gardner"
To:
strAdminUser = "DOMAINNAME\aaron.boyd.gardner"

If you're on a domain, that should work, as long as, as I mentioned, your account exists as a local admin of the target machine.

Rob.
0
 
RobSampsonCommented:
You can also test your logon credentials from the target PC physically, by right-clicking a program, and clicking Run As... then see what credentials you have to specify to run under your account.

Rob.
0
 
aaronbgAuthor Commented:
logon is now working but when the script calls the cmd promt on the remote machine it just sits there on the remote machine at C:\WINDOWS\System32.....the command never goes through
0
 
aaronbgAuthor Commented:
it does set the printer but the cmd prompt does not exit....so it is working and you definetly get the credit Rob now I just need to make sure that the CMD prompt closes at the end....thanks
0
 
RobSampsonCommented:
OK, so now to make the command prompt close, change your cmd /k to cmd /c
Also, if you don't want the command boxes to "flash" up on the screen, change the wshShell.Run strCommand, 1, True, to wshShell.Run strCommand, 0, True.

Also, did you have to change anything in the code?  Is there something that I missed with it?  How did you fix your problems?

Rob.
0
 
aaronbgAuthor Commented:
the fix was adding the domain...when I accept the solution should I choose anyone you are associated with?  kinda new to EE
0
 
RobSampsonCommented:
When you accept, assign relevant points to me.  I'm a lone ranger, so I have no associations.  You can also give Assisted point to other members who gave you some relevant direction towards you solution, if any.

Glad you got it working.

Rob.
0
 
RobSampsonCommented:
Aaronbg, are you going to close this question?

Rob.
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
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now