locked
copy-item script changes file payload RRS feed

  • General discussion

  • Hi All

    i have a simple copy-item script as follow:

    #customize log file
    $date = (Get-Date -Format d).ToString() | foreach {$_ -replace "/","_"} 
    $time = (Get-Date)
    
    $scriptDir = "D:\Scripts\ServerBuildToolkitT1\SingleFileUpfate\"
    $logDir = "D:\Scripts\ServerBuildToolkitT1\Logs\SingleFileUpfate\"
    
    $logFileName = "SingleFileUpfate $date.log"
    
    #Delete old log file
    #Remove-Item -Path "$logDir\$logFileName" -ErrorAction Ignore
    
    
    
    #this file contains the list of destination server that you want copy file/folder to
    
    $computers = Get-Content "D:\Scripts_PS\ServerList.txt"
    
    #this variable contains the source file/folder you want to copy from
    
    $listOfSources = Get-Content "D:\Scripts_PS\ListOfSources.txt"
    
    #this variable is the destination that you want to copy to
    
    $ListOfTargets = Get-Content "D:\Scripts_PS\ListOfTargets.txt"
    
    #this is the main function for copying file/folder to servers
    
    function main 
    
       { 
    
       foreach ($computer in $computers) {
    
       foreach ($source in $listOfSources) {
    
        foreach($target in $ListOfTargets){
    
            Write-Output "$([datetime]::Now) Copying files update to $computer  now" | Out-File -FilePath "$logDir\$logFileName" -Append
    
            copy-item $source -destination "\\$computer\$target"  -Verbose:$false 
            }
          } 
        }
    }
    main
    
    Write-Output "$([datetime]::Now) the operation SingleFileUpdate completed successefuly" | Out-File -FilePath "$logDir\$logFileName" -Append

    when running this with .config or .js files it changes the file payload and changes the file completely.

    any ideas why?

    • Changed type EDolev Sunday, July 29, 2018 6:59 AM
    Thursday, May 3, 2018 11:37 AM

All replies

  • Because you changed the code that worked back to the code that doesn't work.

    You can copy a list of files to a destination by supplying an array of files to the copy command and a destination.  Enumerating both will not do what you are asking.

    No PowerShell command that you are using is capable of changing the contents of any file except if the destination is a file name and not a folder name. 

    If you take the time to learn PowerShell and learn how the Windows file system works you will see why your code is causing you so many issues.


    \_(ツ)_/


    • Edited by jrv Thursday, May 3, 2018 12:07 PM
    Thursday, May 3, 2018 12:04 PM
  • If you want to educate me please feel free to do so some where else.

    your assistance would be greatly appreciated.

    if you do no want to help it is very good as well.

    but please do not patronize me.

    many thanks

    E

    Sunday, May 6, 2018 5:58 AM
  • Sorry but this is not a forum for personal instruction in Windows or PowerShell.  You can look into any of the well written books on these subjects or enroll in one of the Microsoft courses to learn Windows technologies.

    Just as a simple hint.  How can you expect that a copy command can replace the contents of an existing file.  Copy can only create a new copy of an existing file using the original file name or a new name.  The most obvious thing to assume is that you are copying a file over an existing file.

    It is likely that your source files are being copied over existing files declared in your destination list.

    The correct mehod for copying was given to you here: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/3e3cb8c0-05d6-4f9c-a6c5-78a84749c6d6/copying-multiple-files-to-multiples-destinations?forum=ITCG#a4522fba-77cf-40db-b5bc-e7f982d6a9dc

    You have obviously not understood what was being posted.

    Start by trying to understand that the claim, "the script changes the contents of the file", is completely impossible.  This leaves only that you are overwriting existing files with the wrong file.

    "Copy" is a fundamental Windows API used by PowerShell and all other languages and scripts in Windows.  Once you understand the Windows file system API this will become clear.  What you claim is NOT possible.


    \_(ツ)_/

    Sunday, May 6, 2018 6:08 AM
  • thanks for the lesson

    have a nice day.

    bye

    Sunday, May 6, 2018 6:26 AM
  • It is not a lesson.  My suggestion that you are copying files over exisiting files needs to be understood.  You are asking to copy some files.  You have the two lists.  Look at your lists to see what you are asking the code to do.  We cannot do this for you.

    If you code to copy FileA to FIleB then FileB will contain the contents of FileA.  The contents have not been replaced.  The file has been overwritten by the source file. 

    You need to take the time to understand what the code is doing.  In order to understand the code you need to understand PowerShell and the file system.  You have to make the effort to learn these things as we can only point you in the correct direction.


    \_(ツ)_/

    Sunday, May 6, 2018 6:33 AM
  • by the way

    eve you are expert and i am rookie in PS.

    even when copying file to clean folder (not overwrite) the payload changes.

    so you theory that it is not possible is not relevant.

    and yes i do understand what is posted but i guess that you are so expert you are not able to accept new faults that you have never seen.

    i want to thank you for opening my eyes.

    and for future notice, if you do not feel of answering newbies question just ignore them.

    there are lot of other people here that can assist beside you.

    many thanks

    Sunday, May 6, 2018 6:46 AM
  • I should have also noted that the original question you asked is quite ambiguous.  YOU need to carefully and correctly define what you want to copy and where it is to be copied.  After you can clarify that you will be one step closer to being able to write  a script that does what you need.


    \_(ツ)_/

    Sunday, May 6, 2018 6:47 AM
  • by the way

    eve you are expert and i am rookie in PS.

    even when copying file to clean folder (not overwrite) the payload changes.

    so you theory that it is not possible is not relevant.

    and yes i do understand what is posted but i guess that you are so expert you are not able to accept new faults that you have never seen.

    i want to thank you for opening my eyes.

    and for future notice, if you do not feel of answering newbies question just ignore them.

    there are lot of other people here that can assist beside you.

    many thanks

    Copying a file to a folder cannot change the file contents.

    Without an exact example of the code you claim does this there is no way to analyze why you think this can happen.

    The "copy" API call just creates a file with the destination name and copies the contents of the "source" file into the destination file.  In Windows the copy is protected so that it will never fail short of some major system issue.

    I have been answering your questions but I have to take issue with your claims as they are not realistic.  Window does not work the way you are claiming it is working.  This either indicate a failure in the code you are writing or a misunderstanding of what is going to happen.

    We can copy any file anywhere and it will either copy exactly or the copy will fail and no changes will be made.

    File copy does not change the contends of the file being copied.


    \_(ツ)_/

    Sunday, May 6, 2018 6:53 AM
  • Dear Sir

    You can keep Fortifying in your position but the facts are showing other wise.

    i will not publish any source code, you can believe me or not i do not care really.

    instead of arguing with me google this and you will see that are many articles about this issue.

    lets part ways as friends.

    i wish you well

    Sunday, May 6, 2018 7:16 AM
  • Post a link to one article that says that copy can change the contents of a file.


    \_(ツ)_/

    Sunday, May 6, 2018 7:18 AM
  • I am going to take a wild guess at what your issue is and why you are having problems with the queston.

    If the "$ListOfTargets" contains a list of file names then the source files will all be copied into each file in the destination list..  The detonation must be folder names.

    If you really want to copy a list of source files to a list of destination files on a one-to-one basis then you need to create a two column CSV that contains both the source nd destination on each row.

    If this is what you are trying to do then you will have to do as I suggest earlier which is to think about how to pose your question so that it is not ambiguous.


    \_(ツ)_/

    Sunday, May 6, 2018 8:38 AM
  • To copy a list of file to a folder so this:

    copy .\test1.txt, .\test2.txt, .\test3.txt c:\temp -verbose

    This will show three files copied to a target folder.

    copy .\test1.txt, .\test2.txt, .\test3.txt c:\temp\all.txt -verbose

    This will show three files copied to the target file but only the last file in the list will be the final contents as each  file will overwrite the target file.  This is by design.


    \_(ツ)_/

    Sunday, May 6, 2018 8:50 AM
  • so that is the issue

    it copying files with the wrong name and therefor i have a file (appsetting.config) with the content of the last file in the list.

    i need to do a different style of copying so it will copy source file to dest file location and will replace the old file and stop there.

    i am not expecting from you to write the script for me but a hint in the right direction will be appreciated.

    i hope that now i have explained this correctly

    Sunday, May 6, 2018 9:18 AM
  • To copy a list of file to a folder so this:

    copy .\test1.txt, .\test2.txt, .\test3.txt c:\temp -verbose

    This will show three files copied to a target folder.

    copy .\test1.txt, .\test2.txt, .\test3.txt c:\temp\all.txt -verbose

    This will show three files copied to the target file but only the last file in the list will be the final contents as each  file will overwrite the target file.  This is by design.


    \_(ツ)_/

    what you wrote here is exactly what is happening to us.

    i only came up on this 30 mins ago (as i thought the code changes to jibrish)

    Sunday, May 6, 2018 9:20 AM
  • So my guess was right.  I also posted before that you need both source and destination file names in the same file.  We normally use a CSV to do this.

    \_(ツ)_/

    Sunday, May 6, 2018 9:21 AM
  • So my guess was right.  I also posted before that you need both source and destination file names in the same file.  We normally use a CSV to do this.

    \_(ツ)_/

    i missed that post.

    i have created a CSV that looks like this:

    Source Location Destination Location
    D:\Sites\Orange\Config\appSettings.config D$\Sites\Orange\Config\appSettings.config
    D:\Sites\Orange\SelfService\APPS\International\service\globalFunctions.service.d.ts D$\Sites\Orange\SelfService\APPS\International\service\globalFunctions.service.d.ts
    D:\Sites\Orange\SelfService\APPS\International\service\globalFunctions.service.js D$\Sites\Orange\SelfService\APPS\International\service\globalFunctions.service.js
    D:\Sites\Orange\SelfService\APPS\International\service\globalFunctions.service.js.map D$\Sites\Orange\SelfService\APPS\International\service\globalFunctions.service.js.map
    D:\Sites\Orange\SelfService\APPS\International\service\globalFunctions.service.ts D$\Sites\Orange\SelfService\APPS\International\service\globalFunctions.service.ts

    and my thoughts is to run a comperisment of some sort so it will copy each row and stop there.

    am i on the ballpark?

    Sunday, May 6, 2018 9:25 AM
  • Which brings us back to my posting that you need to learn PowerShell first.  If you learn PowerShell then this will be a cinch.  Just enumerate the CSV and pass each row to the copy command.  It is just a basic PowerShell issue now.

    Import-Csv filename | ForEach-Object{ copy $_.Source $_.Destination }

    Of course you need to add the computer loop in and add the computer name.


    \_(ツ)_/

    Sunday, May 6, 2018 9:39 AM
  • Hi jrv

    what do you think about this script?

    # customize log file
    $date = (Get-Date -Format d).ToString() | foreach {$_ -replace "/", "_"}
    $time = (Get-Date)
    
    $scriptDir = "D:\Scripts\ServerBuildToolkitT1\SingleFileUpfate\"
    $logDir = "D:\Scripts\ServerBuildToolkitT1\Logs\SingleFileUpfate\"
    
    $logFileName = "SingleFileUpfate $date.log"
    
    $sources = @()
    $destinsyions = @()
    
    function CSV {
        Import-Csv D:\Scripts_PS\SD.csv | ForEach-Object {
            $sources += $_."Source Location"
            $destinations += $_."Destination Location"
        }
    }
    
    # this file contains the list of destination server that you want copy
    # file/folder to
    $computers = Get-Content "D:\Scripts_PS\ServerList.txt"
    
    function main 
    {
        foreach ($computer in $computers) {
            foreach ($destination in $destinations) {
    
            Write-Output "$([DateTime]::Now) Copying files update to $computer now" |
                Out-File -FilePath "$logDir\$logFileName" -Append
            Copy-Item $sources -Destination "\\$computer\$destination" -Force -Recurse -Verbose:$false
        }
    }
    }
    
    csv
    main
    
    Write-Output "$([DateTime]::Now) the operation SingleFileUpdate completed successefully" |
        Out-File -FilePath "$logDir\$logFileName" -Append

    am i on the right path?

    Monday, May 7, 2018 8:08 AM
  • Not even close.  I gave you the example.  You just merged it with your old and unusable code.

    This is why I recommended that you actually take the time to learn PowerShell.  Without learning the fundamentals of PowerShell, programming ad data we will just keep going around in circles.

    Please study the following until you understand how to write a simple script with a simple loop on a CSV.  My example is all you need but you need to learn the following to understand it.

    Learn PowerShell  
    PowerShell Documentation 
    PowerShell Style Guidelines


    \_(ツ)_/

    Monday, May 7, 2018 8:13 AM
  • This is all you need to do as I posted earlier.

    foreach($computer in $computers){
        Import-Csv filename | ForEach-Object{ copy $_.Source "\\$computer\$($_.Destination)" }
    }


    \_(ツ)_/

    Monday, May 7, 2018 8:38 AM