Copy-Item Overwriting files RRS feed

  • Question

  • Hi Guys

    i have the following script which suppose to copy and update over files in dest servers.

    for some reason (i guess that i am doing something wrong) the content of the files that we are copying is being changed.

    #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 ($target in $ListOfTargets) {
            Write-Output "$([datetime]::Now) Copying files update to $computer  now" | Out-File -FilePath "$logDir\$logFileName" -Append
            copy-item $listOfSources -destination "\\$computer\$target" -Force -recurse -Verbose:$false
    Write-Output "$([datetime]::Now) the operation SingleFileUpdate completed successefuly" | Out-File -FilePath "$logDir\$logFileName" -Append

    any one have idea why is that?

    many thanks


    Sunday, May 6, 2018 8:10 AM


All replies