locked
Copying Files From Server to Text File of Remote Deskops RRS feed

  • Question

  • So I am trying to map a network drive using the New-PSDrive command (sucessful) and then use a list of Desktop Hostnames from a text file to copy a directory  from the Network Drive to each Desktops C:\temp folder.


    I've been able to copy it to a system when I specify the path, as in: Copy-Item $PSDSource -Force -Recurse -Destination \\hostname\C$\temp. But I'd like to be able to do it in one batch.


    Here is my code below, it all works except for the last line

    ForEach-Object {Copy-Item $PSDSource -Force -Recurse -Destination \\c$\$Destination}

    #This Accepts input to declare variable PSDRoot, which will be the parent server or remote directory we want to work from 
    $PSDRoot = Read-Host Enter Path to Root Directory you wish to copy from to remote Machine. This will further be called with the Driver Letter Arbitrarily assigned in the next prompt.
    
    
    
    
    #This Accepts input to declare variable PSDrive Letter, an arbitrary Drive Letter that will be important when script reads from a UNC path
    $PSDriveLetter = Read-Host Enter Driver Letter [Aribitrary, but your C and D, and H Drives are probably already taken]
    New-PSDrive -Name $PSDriveLetter -PSProvider FileSystem -Root $PSDRoot 
    
    $PSDSource = Read-Host Enter the full network path to directory or file youd like to copy, for example, enter \\nasprod\labtechs\labtech\Julius\file.bat
    
    
    
    #Declares Computers Variable, which gets a list of values from a text file
    $Computers= Read-Host Enter Path to Text File with List of PC Hostnames (EX: C:\temp\file.txt) 
    
    
    
    # sets the varible for the file destination
    $Destination = Read-Host Enter the Destination on Remote Machine EX temp\file.bat
    
    # displays the computer names on screen
    
    #$computer = Get-Content $Computers | ForEach-Object {Copy-Item $PSDRoot\$PSDSource -Force -Recurse -Destination \\$computer\c$\$Destination}
    
    ForEach-Object {Copy-Item $PSDSource -Force -Recurse -Destination \\c$\$Destination}




    Wednesday, March 2, 2016 4:59 AM

Answers

  • Hi,

    The foreach-object is a loop, you need to specify the object which needs to be looped.

    You could try as the below:

    Foreach-Object($computer in computers)
    {
    Copy-Item $PSDRoot\$PSDSource -Force -Recurse -Destination \\$computer\c$\$Destination
    }

    Best Regards,

    Elaine


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Elaine Jing Friday, March 25, 2016 3:24 PM
    • Marked as answer by Elaine Jing Friday, March 25, 2016 3:24 PM
    Wednesday, March 2, 2016 8:32 AM

All replies

  • Here's the error I get:


    Copy-Item : The network path was not found.
    At line:1 char:17
    + ForEach-Object {Copy-Item $PSDSource -Force -Recurse -Destination \\c$\$Destinat ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (Enable cs436 Account_March9_216PM.xml:FileInfo) [Copy-Item], IOException
        + FullyQualifiedErrorId : CopyDirectoryInfoItemIOError,Microsoft.PowerShell.Commands.CopyItemCommand

    Wednesday, March 2, 2016 5:00 AM
  • Start with:

    help ForEach-Object -Full

    pay close attention to the example..


    \_(ツ)_/

    Wednesday, March 2, 2016 5:14 AM
  • Hi,

    The foreach-object is a loop, you need to specify the object which needs to be looped.

    You could try as the below:

    Foreach-Object($computer in computers)
    {
    Copy-Item $PSDRoot\$PSDSource -Force -Recurse -Destination \\$computer\c$\$Destination
    }

    Best Regards,

    Elaine


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Elaine Jing Friday, March 25, 2016 3:24 PM
    • Marked as answer by Elaine Jing Friday, March 25, 2016 3:24 PM
    Wednesday, March 2, 2016 8:32 AM