Copy folder/files from a specific path to shared path

Welcome Forums General PowerShell Q&A Copy folder/files from a specific path to shared path

Viewing 10 reply threads
  • Author
    Posts
    • #226518
      Participant
      Topics: 7
      Replies: 16
      Points: 109
      Rank: Participant

      Hi All,

      I am trying to copy either entire log folder or files inside the log folder from a specific path to a shared path for about 50 servers. I am a little confused how do i get the server name on my destination if i have to copy entire files from the folder..

      Any help is appreciated.

      Thank You

    • #226524
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Have you tried to search for a solution? Your task sounds like a very common task and there are probably thousands of examples available in the internet or even here in this forum.
      Anyway … it will be hard to help with meaningful advises with that little information you gave.

    • #226566
      Participant
      Topics: 7
      Replies: 16
      Points: 109
      Rank: Participant

      Yes, i did try searching on the internet, unfortunately i didnt find anything to what i was looking for.

    • #226572
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Hmmm … if you expected to find something perfectly fitting for your special needs you probably expected too much. 😉 But you can find something to start with at least. Don’t be lazy. Start with a few lines of code and when you get stuck you come back and we will try to help you. But we will not write ready to use code from scratch for you on request.

      Here is something you can start with:

      https://lmgtfy.com/?q=powershell+Copy+folder%2Ffiles+from+a+specific+path+to+shared+path

    • #226758
      Participant
      Topics: 7
      Replies: 16
      Points: 109
      Rank: Participant

      i tried the below, however i need to copy the same thing for about 50 servers

      Copy-Item -Path “C:\temp\ccmsetup.log” -Destination (New-item -Name “ccmsetup” -Type Folder -Path “c:\temp”) -Force -Verbose

    • #226788
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      That’s a local copy job. You told something about 50 server and a shared path?

      And please – when you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

    • #226836
      Participant
      Topics: 7
      Replies: 16
      Points: 109
      Rank: Participant
      # This file contains the list of servers you want to copy files/folders to
      $computers = gc "C:\scripts\servers.txt"
      # This is the file/folder(s) you want to copy to the servers in the $computer variable
      $source = "C:\Software\EMC\Networker\NWVSS.exe"
      # The destination location you want the file/folder(s) to be copied to
      $destination = "C$\temp\"
      foreach ($computer in $computers) {
      if ((Test-Path -Path \\$computer\$destination)) {
      Copy-Item $source -Destination \\$computer\$destination -Recurse
      } else {
      "\\$computer\$destination is not reachable or does not exist"
      }
      }

      \\GBNVRWAPP17\\\cpwplc.com\data\everyone\wintel\QRadar_logs is not reachable or does not exist

    • #226866
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      $computerList = Get-Content -Path 'C:\scripts\servers.txt'
      $source = 'C:\Software\EMC\Networker\NWVSS.exe'
      $targetDirectory = 'C$\temp\'
      foreach ($ComputerName in $computerList) {
          if (Test-Connection -ComputerName $ComputerName  -Count 1 -Quiet ) {
              $Destination = Join-Path  -Path "\\$ComputerName" -ChildPath $targetDirectory
              if (-not (Test-Path -Path $Destination)) {
                  New-Item -Path $Destination -ItemType Directory | Out-Null
                  Copy-Item $source -Destination $Destination
              }
              else {
                  Copy-Item $source -Destination $Destination
              }
          }
          else {
              "$ComputerName unreachable"
          }
      }

      Just to mention it: You need to have administrative rights on the target computers.

      The last row you posted seems like you have too much backslashs in your original code. Either in your file with computernames or in your variable $destination.

    • #227737
      Participant
      Topics: 7
      Replies: 16
      Points: 109
      Rank: Participant

      $servers = Get-Content -Path “C:\Temp\Servers.txt”
      foreach ($server in $servers) { Copy-Item -Path “\\$server\c$\Program Files\xxxlogs” -Destination “\\sharepath\$server\” -Recurse -Force
      }

      Finally, managed to get something….Thank you very much for your help..!

    • #227740
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Just to mention it … when you copy files from a remote computer to a remote share and neither of those are on the local computer the files get copied from the remote computer to the local computer the script runs on and then to the destination share. If you care about speed and less stress for your network you should run this from the computer the destination share is on.

    • #227746
      Participant
      Topics: 7
      Replies: 16
      Points: 109
      Rank: Participant

      True, Point noted..!

Viewing 10 reply threads
  • You must be logged in to reply to this topic.