I’m new to VBScript. I cannot find a way to copy files from one XP host to another using WMI in a VBS. The usual way of copying files (RPC – Remote Procedure Call, SMB, UNC) are not available to several hosts but WMI is available to all hosts, and I need to copy files from my admin host to a target Windows host. I thought I’d find some sample code out there but I’ve found no info on it. Haven’t found anything telling me it can’t be done, either.
The source files are an executable and ‘test1.txt’ in my admin computer’s ‘F:\TEMP’ folder. I want to put the files on remote host HOST1’s ‘C:\TEMP’ folder. I have full admin rights on both hosts. Here is what I have so far, just for one file (to keep the testing simple):
strComputer = "HOST1"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery( _
"Select * from Win32_Directory where Name = 'c:\\temp'")
For Each objFiles in colFiles
errResults = objFolder.Copy("f:\temp\test1.txt")
Wscript.Echo errResults
Next
I learned that WMI cannot create files on a remote host, and it cannot copy files over a network connection:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa389288%28v=vs.85%29.aspx
However, it can run a cmd process. Here’s Frank White’s code in C sharp, followed by his example:
https://stackoverflow.com/a/8913231/1569434
You will need four things to use all the following scriptlets, which build on each other to use psexec to run a “normal” VBScript or batch script on the remote host:
Important Note: Do NOT use DFS to map the network share! It will fail if you use Distributed File System for your network share. An error code you might get depending on how you try is “System error 1312”, no matter which operating system (e.g., XP, Win 7) you use.
When RPC is not available on a remote host but WMI is, then the following method will create a local ASCII file on the remote host’s c:\temp folder, containing the text “myTextCommands”, without the quotes.
Notice the important limitation in the script above: it can only create ASCII files – not binary.
Let’s use that technique to map a drive letter:
where “strRemoteLog” is set to something like “c:\temp\MyLog.txt”, “strPassword” is prompted (see full script example and reference at bottom), and “errProcess” is a subroutine that runs the following process using the “cmd /c” trick mentioned above:
With a network drive mapped, copy your script to the host:
SCRIPT1.bat is ready, so start psexec against it on the remote host, passing your script a variable strUserID that would be obtained earlier and is here for example:
Once psexec finishes, you might want to save the results. So you rename the log file, upload it, unmap your drive, and clean up residual files:
You’re done. You’ve successfully mapped a drive, run a routine script against the remote host, and uploaded its output.
Note this method also works on Windows 7 and Windows 2008 with UAC.
Here’s the full ‘sample’ integrated script. Feel free to suggest fixes, improvements, etc.