我是 powershell 的新手,这个问题将证明这一点。我正在尝试从命令行执行一个简单的任务,其中我有一个 txt 文件,其中包含以分号分隔的文件名,例如...
fnameA.ext;fnameB.ext;fnameC.ext;....
我正在尝试运行一个命令来解析这个文件,用分号分割内容,然后将每个文件的复制命令运行到所需的目录。
这是我正在运行的命令:
gc myfile.txt |% {$_.split(";") | copy $_ "C:\my\desired\directory"}
但是对于列表中的每个项目,我都收到了这样的错误...
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 line:1 char:36
+ gc bla.txt |% {$_.split(";") | copy <<<< $_ "C:\my\desired\directory"}
+ CategoryInfo : InvalidArgument: (fileA.txt:String) [Copy-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.CopyItemCommand
最佳答案
抵制制作单线的冲动,尤其是在您刚开始时。也就是说,问题是您需要将拆分的内容通过管道传输到另一个 ForEach-Object
。
试试这个:
$File = Get-Content .\MyFile.txt
$File | ForEach-Object {
$_.Split(';') | ForEach-Object {
Copy-Item -Path "$_" -Destination 'C:\destination'
}
}
关于powershell - 需要帮助使用 foreach 进行简单的 powershell 命令复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6433567/