locked
Copy file to multiple machines in the Domain (Credentials required) RRS feed

  • Question

  • # This file contains the list of servers you want to copy files/folders to
    $computers = Get-Content "C:\copy\ufpcs.txt"

    # This is the file/folder(s) you want to copy to the servers in the $computer variable
    $source = "C:\file\allfiles"

    # The destination location you want the file/folder(s) to be copied to
    $destination = "c$\temp\"

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

    =============================================================

    I get the error below: 

    t C:\scripts\ufscopy.ps1:10 char:35
    + foreach ($computer in $computers) {
    +                                   ~
    Missing closing '}' in statement block or type definition.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEndCurlyBrace

    The machines are the domain. Is that the reason for failure?



    Thursday, January 30, 2020 10:21 AM

All replies

  • The error clearly states that you are missing a closing } in your script.  Please learn to read the error messages.  re displayed for a reason.  If you have trouble reading technical information then start by asking a coworker to help you read the error as it is highly likely that another person can read the error and explain it to you.

    You would also have less trouble if you posted the code correctly formatted and check what you have written as you would likely see your errors.



    \_(ツ)_/


    • Edited by jrv Thursday, January 30, 2020 10:30 AM
    Thursday, January 30, 2020 10:30 AM
  • Hi Durodolu

    Just a double Foreach and a missing }

    # This file contains the list of servers you want to copy files/folders to
    $computers = Get-Content "C:\copy\ufpcs.txt"
    
    # This is the file/folder(s) you want to copy to the servers in the $computer variable
    $source = "C:\file\allfiles"
    
    # The destination location you want the file/folder(s) to be copied to
    $destination = "c$\temp\"
    
    #The command below pulls all the variables above and performs the file copy
    foreach ($computer in $computers) 
    {
    Copy-Item $source -Destination "\\$computer\$destination" -Recurse
    }

    Tips : when you use [({" or ', always put immediatly the ])}" or ' to close.

    Regards

    Olivier

    Thursday, January 30, 2020 1:00 PM