为什么这个PowerShell代码在PS中单独工作,但在脚本中失败?

我在构build机器上运行的部署脚本中有这个块。 它应该将登台计算机上的文件从“安装”文件夹移至应用程序文件夹。
在这个块之前,还有几个成功执行Invoke-Session的动作

write-host "Deploying" -foregroundcolor red -backgroundcolor yellow invoke-command -session $s { Get-ChildItem -Path "C:\Install\pre_master" | Copy-Item -Destination "C:\inetpub\wwwroot\pre_master" -force -Recurse } 

如果我在PS中手动创build一个会话并粘贴这个块,它就可以正常工作。

如果我运行这个脚本,这个步骤使我陷入数百万(对于每个文件,我试图移动我想?)

input对象不能绑定到该命令的任何参数,因为该命令不会将stream水线放入put中,或者input及其属性与进行stream水线input的任何参数都不匹配。 + CategoryInfo:InvalidArgument:(0201-KS.xml:PSObject)[Copy-Item],ParameterBindingException + FullyQualifiedErrorId:InputObjectNotBound,Microsoft.PowerShell.Commands.CopyItemCommand

所以我的问题是: 为什么这个代码块在PS手动执行时工作。 但不能在脚本中工作?

这两台机器都有PowerShell 2.0

我仍然不知道为什么,但通过使用远程会话运行某些cmdlet时,不能使用pipe道。 您可以先将cmdlet的结果存储在variables中,然后编写一个foreach循环来将cmdlet的结果传递给另一个cmdlet。

例如,您可以先将cmdlet的结果存储在variables中,然后编写一个foreach循环来复制这些文件:

 invoke-command -session $s { $files = Get-ChildItem -Path "C:\Install\pre_master"; Foreach($file in $files) { Copy-Item -Path $file -Destination "C:\inetpub\wwwroot\pre_master" -force Recurse} }