Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 获取一个结果集(例如listofcomputers.txt),并对结果集的每个项运行copyitem命令_Variables_Powershell_Pipe_Token_Copy Item - Fatal编程技术网

Variables 获取一个结果集(例如listofcomputers.txt),并对结果集的每个项运行copyitem命令

Variables 获取一个结果集(例如listofcomputers.txt),并对结果集的每个项运行copyitem命令,variables,powershell,pipe,token,copy-item,Variables,Powershell,Pipe,Token,Copy Item,大家好:这就是我想做的。。。。 获取一个结果集(例如listoffailedcomputers.txt),并对结果集中的每个项目运行复制命令。 逻辑是在该failedlistofcomputers.txt中的所有计算机上运行复制命令,以便最终结果将该文件夹复制到该列表中所有计算机的本地。 我可以通过在所有这些计算机上使用远程控制台来实现这一点,但这样做效率不高。 多谢各位 这是我到目前为止写的代码 $failedcomputers = gc c:\listoffailedcomputers.tx

大家好:这就是我想做的。。。。 获取一个结果集(例如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
Copy Item:无法将输入对象绑定到命令的任何参数
要么是因为命令不接受管道输入,要么是因为输入及其
属性与接受管道输入的任何参数都不匹配。
在copypatchtofailedcomputers.ps

+$failedcomputer | copy item如果查看ISE中的
Get Help copy item-full
,它会告诉您可以在管道上接受什么。可以通过管道传递包含复制ItemProperty路径的字符串。在这个实例中,您实际上是在管道传输一个主机名,这就是为什么会出现这个错误

试试这个:

    copy-item \\server\patch\*.* -destination \\$failedcomputer\C$\patch

如果您查看ISE中的
Get Help Copy Item-full
,它会告诉您它可以在管道上接受什么。可以通过管道传递包含复制ItemProperty路径的字符串。在这个实例中,您实际上是在管道传输一个主机名,这就是为什么会出现这个错误

试试这个:

    copy-item \\server\patch\*.* -destination \\$failedcomputer\C$\patch

您不能只是将computername导入任何cmdlet并期望它了解如何使用它<代码>复制项
甚至不包括
-ComputerName
参数。你可以尝试两种方法

您可以在每台计算机上远程执行
复制项
,如下所示:

$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\"
}
或者,如果您可以从计算机访问所有远程计算机的文件,则可以尝试直接从一个网络共享复制到另一个(目标计算机),如下所示:

$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\"
}

您不能只是将computername导入任何cmdlet并期望它了解如何使用它<代码>复制项甚至不包括
-ComputerName
参数。你可以尝试两种方法

您可以在每台计算机上远程执行
复制项
,如下所示:

$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\"
}
或者,如果您可以从计算机访问所有远程计算机的文件,则可以尝试直接从一个网络共享复制到另一个(目标计算机),如下所示:

$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\"
}
另一种可能性:

$failedcomputers = gc c:\listoffailedcomputers.txt

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


Invoke-CimMethod @CmdParams -ComputerName $failedcomputers
这应该是多线程的,而不需要为了复制文件而启动一堆远程PS实例的开销。

另一种可能性:

$failedcomputers = gc c:\listoffailedcomputers.txt

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


Invoke-CimMethod @CmdParams -ComputerName $failedcomputers

这应该是多线程的,而不需要为了复制文件而旋转一堆远程PS实例的开销。

Gotcha!现在对我来说更有意义了。谢谢,明白了!现在对我来说更有意义了。谢谢。如果您认为这是正确的答案,请在投票下使用支票来确认。你前面的问题也是如此你们俩的回答都很好。Graimer帮助我认识到,尽管powershell在我看来更像是简单的英语,但您不能期望cmdlet动态地理解不属于它的参数。成功的解析要求每个步骤都能直接理解所有其他相邻步骤。Mjolinor帮助我理解了一个有效的选择。我不必求助于使用bits或robocopy来控制速度和检查完整性;即使他们似乎玩得很好的PS。如果你认为这是正确的答案,请使用选票下的复选标记来确认它。你前面的问题也是如此你们俩的回答都很好。Graimer帮助我认识到,尽管powershell在我看来更像是简单的英语,但您不能期望cmdlet动态地理解不属于它的参数。成功的解析要求每个步骤都能直接理解所有其他相邻步骤。Mjolinor帮助我理解了一个有效的选择。我不必求助于使用bits或robocopy来控制速度和检查完整性;尽管他们似乎玩得很好。