powershell 使用 Copy_Item cmdlet 将文件复制到远程计算机问题

Posted

技术标签:

【中文标题】powershell 使用 Copy_Item cmdlet 将文件复制到远程计算机问题【英文标题】:powershell copy files into remote machine issue using Copy_Item cmdlet 【发布时间】:2022-01-16 18:52:48 【问题描述】:

我试图将几个文件复制到我的远程盒子中,但我看到路径格式不正确的问题,有人可以检查一下吗?

for($hostname in Get-Content C:\folder1\machine.txt)
Copy-Item -Path "C:\folder1\ramen-0.1-web-1.3.6.jar" -Destination "\\hostname\C:\folder1\folder2\" -Recurse -Force


错误信息是: Copy-Item : 不支持给定路径的格式。

【问题讨论】:

【参考方案1】:

您丢失的 '$' 符号。您正在使用 'c:' 作为目标路径的一部分。

for($hostname in Get-Content C:\folder1\machine.txt)
Copy-Item -Path "C:\folder1\ramen-0.1-web-1.3.6.jar" -Destination "\\hostname\C$\folder1\folder2\" -Recurse -Force


【讨论】:

【参考方案2】:

如果启用了 powershell 远程,这是另一种方式(foreach 不是 for)(-recurse 对文件没有意义):-ToSession 不能是数组。

$list = Get-Content C:\folder1\machine.txt
$sessions = new-pssession $list
foreach($session in $sessions)
Copy C:\folder1\ramen-0.1-web-1.3.6.jar C:\folder1\folder2 -ToSession $session -For

【讨论】:

以上是关于powershell 使用 Copy_Item cmdlet 将文件复制到远程计算机问题的主要内容,如果未能解决你的问题,请参考以下文章

D盘打开powershell安装numpy为啥安装到了C盘

使用C#+PowerShell进行Windows系统间文件传输

C#中的Powershell - 访问Exchange

如何使用c#winforms将参数传递到Powershell脚本?

如何用C语言执行powershell命令

如何用C语言执行powershell命令