Search code examples
variablespowershellpipetokencopy-item

Take a result set (eg. listofcomputers.txt) and run a copy-item command on every item of result set


Hi all: This is what I'm trying to do.... Take a result set (eg. listoffailedcomputers.txt) and run a copy command on every item inside the result set. The logic is to run a copy command on all the computers in that failedlistofcomputers.txt so that the end result will have that folder copied down locally on all computers on that list. I can do this by using a remote console on all those computers but that would not be efficient. Thank You.

Here is the code that I wrote so far.......

$failedcomputers = gc c:\listoffailedcomputers.txt
foreach ($failedcomputer in $failedcomputers)
{
$failedcomputer | copy-item \\server\patch\*.* -destination c:\patch\
}

And this is the error I'm getting......

Copy-Item : The input object cannot be bound to any parameters for the command 
either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At copypatchtofailedcomputers.ps
+ $failedcomputer | copy-item <<<<  \\server\patch\ -destination c:\patch\
    + CategoryInfo          : InvalidArgument: (mycomputername:PSObject) [Copy- 
   Item], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Command 
   s.CopyItemCommand

If I remove the pipe between the $failedcomputer variable and the copy-item command on my statement, I'll get a unexpected token error.


Solution

  • Another possibility:

    $failedcomputers = gc c:\listoffailedcomputers.txt
    
    $CmdParams = 
    @{ 
       ClassName  = 'Win32_Process'
       MethodName = 'Create'
       Arguments  = @{ CommandLine = 'copy \\server\patch\*.* c:\patch\' }
     }
    
    
    Invoke-CimMethod @CmdParams -ComputerName $failedcomputers
    

    That should multi-thread it, without the overhead of spinning up a bunch of remote PS instances just to do a file copy.