Jump to content

How to copy a file to a remote computer with WMI?


 Share

Recommended Posts

Hello everyone:
 
I want to adapt this script to copy a local file to a remote computer but does not work.
 
any ideas please.
 
thanks
 
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
Set colFolders = objWMIService.ExecQuery _
 "SELECT * FROM Win_32 Directory WHERE Name = 'c:Scripts'")
For Each objFolder in colFolders
 errResults = objFolder.Copy("D:Archive")
 Wscript.Echo errResults
Next
 
The script is adapted
 
;  remote computer PC23
;  local file for copy c:WindowstempFile.bat
;  remote folder C:windowsArchive
 
$strComputer = "PC23"
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!" & $strComputer & "rootcimv2")
$colFolders = $objWMIService.ExecQuery( _
"SELECT * FROM Win_32 Directory WHERE Name = 'c:WindowstempFile.bat'")
 
For $objFolder in ·colFolders
$errResults = objFolder.Copy("C:windowsArchive")
MsgBox(0,"",""&$errResults)
Next
 
It does not work
 
 
Link to comment
Share on other sites

  • 5 months later...

Hi

I'm brand new to the forum and a real scripting noob. I've decided to not depend on others anymore and try to become decent at creating my own scripts. Here is my first post and question. Thanks for any help you all can give.

How do I take the basic filecopy function and copy to a list of machines? I know there is the fileopen and fileread functions but I'm not sure how to combine the functions to read computer name, copy file/directory and repeat. 

Here is what I have put together. Feel free to laugh since I have little clue what I'm doing there is no pride to be bruised.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.10.2
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


; Read a list of computer names from a text file on local machine.

ReadList()

Func ReadList()
Local Const $CompList = @MyDocumentsDir & "\NewComputers.txt"

Local $hFileOpen = FileOpen($CompList, $FO_READ)
If $hFileOpen = -1 Then
   MsgBox(6, "File Error", "Could Not Open File", 5)
Return False
EndIf


Local $sFileRead = FileReadLine($hFileOpen, 1)

MsgBox($MB_SYSTEMMODAL, "Machine List", "First Line of the File:" & @CRLF & $sFileRead)

Local $sFileRead = FileReadLine($hFileOpen, -1)

MsgBox($MB_SYSTEMMODAL, "Machine List", "Last Line of the File:" & @CRLF & $sFileRead)

FileClose($hFileOpen)

Return True
EndFunc



; Copy files to computer names listed in text file read previously.

CopyFiles()

Func CopyFiles()

    ; Copy Class files in the working directory to a new folder/directory called Scripts.
    DirCopy(@MyDocumentsDir & "\scripts" , "\\" & $CompList &  "\scripts",1)

    ; Display the temporary directory.
    ShellExecute(@TempDir & "\scripts")
EndFunc   ;==>Example
Link to comment
Share on other sites

Try this and see if it makes sense to you.

#cs ----------------------------------------------------------------------------
    
    AutoIt Version: 3.3.10.2
    Author:         myName
    
    Script Function:
    Template AutoIt script.
    
#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


; Read a list of computer names from a text file on local machine.

Global Const $sCompListFile = @MyDocumentsDir & "\NewComputers.txt"
Global $aCompList = FileReadToArray($sCompListFile)
If @error Then
    MsgBox(6, "File Error", "Could Not Open File", 5)
    Exit
EndIf
For $I = 0 To UBound($aCompList) - 1
    CopyFiles($aCompList[$I])
Next

; Copy files to computer names listed in text file read previously.


Func CopyFiles($CompList)
    ; Copy Class files in the working directory to a new folder/directory called Scripts.
    DirCopy(@MyDocumentsDir & "\scripts", "\\" & $CompList, $FC_OVERWRITE) ; I wasn't sure if you wanted a folder structure of Scripts\Scripts or just Scripts\
    If @error Then Return False
    ; Display the temporary directory.
    ShellExecute("\\" & $CompList & "\scripts")
    Return True
EndFunc   ;==>CopyFiles

As you can see, in your CopyFiles function I altered the DirCopy command because I wasn't sure if you wanted a folder of Scriptsscripts, or just scripts. The way it's written it gives you computernamescripts.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BrewManNH

That's awesome, many thanks for the help!!! 

As far as folder structure it will be slightly different in the final copy. In the final copy I will copy one folder called ClassFiles (w/sub-folders and files) from @mydocumentsir on local machine to @mydocumentsdir on all of the remote machines. Something like:

Dircopy(@mydocumentsdir & "ClassFiles, "" & $CompList & "mydocuments", $FC_OVERWRITE)    OR maybe replace mydocuments with C$usershesadminmydocuments

Would that create a folder called ClassFiles in the mydocuments folder of the remote machine?

Thanks again for all the help!!

Link to comment
Share on other sites

Actually the way I wrote it, it wouldn't copy anything, because it would need a drive to place the files on. computernameC$usershesadminmydocuments would create the folder inside the mydocuments folder in the user's profile folder. If you want it in the user's My Documents folder, you'd need to use computernameC$usershesadminDocuments

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...