BradV
(Seasoned Scripter)
2012-09-10 06:25 PM
copying files to remote server

Hi Guys,

I'm trying to write a script to copy files to a group of servers. I apologize if there already is a UDF for this, but the proxy server here at work apparently renders the UDF list as blank. \:\(

I have a group of Windows 2008 R2 servers. I have placed the files in c:\temp. I also have two ini files in the same location. One has a list of all the computers in the local group and the other has a list of the files to be copied.

Examples:
computers.ini
 Code:
[computers]
comp1=server-name1
comp2=server-name2
comp3=another-server


files.ini
 Code:
[Files]
file1=jdk-6u35-windows-i586.exe
file2=visioviewer.exe


I'm trying to use:
 Code:
Break On
Dim $SO
$SO = SetOption('Explicit',          'On')
$SO = SetOption('NoMacrosInStrings', 'On')
Dim $strWks, $strFile, $colFiletoSend
Dim $strComps, $colComps, $objComp, $objFSO
Dim $intErr, $objFile, $strFileName, $strRemotePath
$strFile  = "c:\temp\files.ini"
$strComps = "c:\temp\computers.ini"
$colComps = Split(ReadProfileString($strComps,"computers",""),chr(10))
For each $objComp in $colComps
   If $objComp <> ""
      $strWks = ReadProfileString(strComps,"computers",$objComp)
      $colFiletoSend = Split(ReadProfileString($strFile,"Files",""),chr(10))
      cd "c:\temp"
      $strRemotePath = "\\" + $strWks + "\c$\temp"
      $objFSO = CreateObject("Scripting.FileSystemObject)
      For each $objFile in $colFiletoSend
         If $objFile <> ""
            $strFileName = ReadProfileString($strFile,"Files",$objFile)
            $intErr = $objFSO.CopyFile($strFileName,$strRemotePath)
         EndIF
      Next
   EndIf
Next


When attempting the copy, I'm getting:
 Code:
COM exception error "CopyFile" <<null> - <null>> [-2147352567/80020009]


If I just do from a command prompt in c:\temp:
 Code:
copy jdk-6u35-windows-i586.exe \\server-name1\c$\temp
it works. Any ideas what I am doing wrong, or a better approach?

Thanks


ShaneEP
(MM club member)
2012-09-10 07:11 PM
Re: copying files to remote server

Have you tried just using the built in kix version of COPY rather than the FSO com object?

ShaneEP
(MM club member)
2012-09-10 07:17 PM
Re: copying files to remote server

Also...This line has a typo (missing a $)

 Code:
$strWks = ReadProfileString(strComps,"computers",$objComp)

Should be...
 Code:
$strWks = ReadProfileString($strComps,"computers",$objComp)


BradV
(Seasoned Scripter)
2012-09-10 07:28 PM
Re: copying files to remote server

I'll give that a try tomorrow. It's been a while and the manual didn't say I could use copy to copy to a remote system. \:\) (missing $ was just a typo - I can't cut and paste; everything has to be re-typed)

ShaneEP
(MM club member)
2012-09-10 07:33 PM
Re: copying files to remote server

This seemed to work ok for me.

 Code:
Break On

$strFile  = "c:\temp\files.ini"
$strComps = "c:\temp\computers.ini"
$colComps = Split(ReadProfileString($strComps,"computers",""),chr(10))

For each $objComp in $colComps
   If $objComp <> ""
      $strWks = ReadProfileString($strComps,"computers",$objComp)
      $colFiletoSend = Split(ReadProfileString($strFile,"Files",""),chr(10))
      $strRemotePath = "\\" + $strWks + "\c$\temp"
      For each $objFile in $colFiletoSend
         If $objFile <> ""
            $strFileName = "C:\Temp\"+ReadProfileString($strFile,"Files",$objFile)
            Copy $strFileName $strRemotePath
         EndIF
      Next
   EndIf
Next


ShaneEP
(MM club member)
2012-09-10 07:35 PM
Re: copying files to remote server

Oh and make sure to turn on NOVARSINSTRINGS as well. The dollar sign in the share path might be giving you problems. I just tested to a non-hidden \\server\share type directory and it worked fine.

Les
(KiX Master)
2012-09-10 07:51 PM
Re: copying files to remote server

Shane appears to hard code the paths whereas BradV infers from current working directory. I see a CD but the current working drive might be different.

ShaneEP
(MM club member)
2012-09-10 08:34 PM
Re: copying files to remote server

Oh yes, forgot to mention that one.

BradV
(Seasoned Scripter)
2012-09-12 12:37 PM
Re: copying files to remote server

Yes, I kept the cd in. Worked perfectly. Maybe we should suggest a change to the manual to add that copy to a remote system also works?

Thanks Shane! \:\)