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

 PHP猎人 发布于 2023-02-13 17:48

大家好:这就是我要做的......拿一个结果集(例如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命令之间的管道,我将收到意外的令牌错误.

1 个回答
  • 您不能将计算机名管道传输到任何cmdlet中,并期望它了解如何使用它.Copy-Item甚至没有包含-ComputerName参数.你可以尝试两种方法

    您可以copy-item在每台计算机上远程执行,如下所示:

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

    或者,如果您可以从计算机访问所有远程计算机,则可以尝试直接从一个网络共享复制到另一个网络共享(目标计算机),如下所示:

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

    2023-02-13 17:51 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有