variables - 取得一个结果集(例如listofcomputers.txt),并对结果集的每个项目运行一个copy-item命令

标签 variables powershell pipe token copy-item

大家好:这就是我想要做的...
取结果集(例如listoffailedcomputers.txt),并对结果集中的每个项目运行复制命令。
逻辑是在 failedlistofcomputers.txt 中的所有计算机上运行复制命令,以便最终结果是将该文件夹本地复制到该列表中的所有计算机上。
我可以通过在所有这些计算机上使用远程控制台来做到这一点,但这并不高效。
谢谢。

这是我到目前为止编写的代码。

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

这就是我得到的错误...
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

如果删除语句中$ failedcomputer变量和copy-item命令之间的管道,则会收到意外的 token 错误。

最佳答案

另一种可能性:

$failedcomputers = gc c:\listoffailedcomputers.txt

$CmdParams = 
@{ 
   ClassName  = 'Win32_Process'
   MethodName = 'Create'
   Arguments  = @{ CommandLine = 'copy \\server\patch\*.* c:\patch\' }
 }


Invoke-CimMethod @CmdParams -ComputerName $failedcomputers

那应该对它进行多线程处理,而不必花费大量的远程PS实例来复制文件的开销。

关于variables - 取得一个结果集(例如listofcomputers.txt),并对结果集的每个项目运行一个copy-item命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20298664/

相关文章:

Python-满足不同条件时如何从变量中选择

传递到各个动画的各个元素的 javascript 变量

azure - 如何通过调用 api 设置 Key Vault key 的过期日期

bash - 如何在没有 stdbuf 和类似工具的情况下取消缓冲遗留运行二进制文件的标准输出

c - 在进程之间传递值

variables - XSLT 中的条件变量选择

Javascript 字符串变量未加引号?

PowerShell : Divide Files by Line in Faster Way

azure - 使用 azcopy 从带有 SAS 链接的 blob 下载 zip 文件

linux - 我将如何创建代表 linux grep 命令的流程图?