Can someone help to create a new folder by tweaking script below?

Welcome Forums General PowerShell Q&A Can someone help to create a new folder by tweaking script below?

Viewing 2 reply threads
  • Author
    Posts
    • #221556
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Hi All,

      I have this script below that uses a text file with a list of servers I want to copy a folder named “sqlcupatch” to multiple servers.  It works if the folder already exists on the target server.  But I can’t get it to work trying to create the named folder to copy the files to the list of servers in first line of code shown below.  Can someone help tweaking this ps script below?

      # This file contains the list of servers you want to copy files/folders to
      $computers = gc “D:\servers.txt”

      # This is the file/folder(s) you want to copy to the servers in the $computer variable
      $source = “C:\sqlcupatch\*.*”

      # The destination location you want the file/folder(s) to be copied to
      $destination = “C$\sqlcupatch\”

      #The command below pulls all the variables above and performs the file copy
      foreach ($computer in $computers) {Copy-Item $source -Destination “\\$computer\$destination”}

      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”
      }
      }

    • #221559
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      David, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code or error messages or 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“.
      Thanks in advance.

      If the destination folder does not exist you have to create it first.

      $computerNameList = Get-Content -Path 'D:\servers.txt'
      $source = 'C:\sqlcupatch\*.*'
      $destination = 'C$\sqlcupatch\'
      
      foreach ($computerName in $computerNameList) {
          if(Test-Connection -ComputerName $computer -Quiet -Count 1){
              if (-not (Test-Path "\\$computerName\$destination")) {
                  New-Item -Path "\\$computerName\$destination" -ItemType Directory | Out-Null
              }
              Copy-Item $source -Destination \\$computer\$destination -Recurse
          }
      }
    • #221835
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Mr. Olaf,

      Thanks for providing the proper methods to post code, error messages, or sample as I’m new to PS.org.  I will do this moving forward.  The code you provided worked like a charm!

      Many thanks!!

      David

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