20

我正在尝试使用Copy-Item以下命令从远程机器到另一台远程机器:

Copy-Item -Path "\\machine1\abc\123\log 1.zip" -Destination "\\machine2\\c$\Logs\"

我不断收到错误“ Cannot find Path "\\machine1\abc\123\log 1.zip

我可以访问该路径并从那里手动复制。

我以管理员身份打开 PowerCLI 并运行此脚本......我完全被困在这里,不知道如何解决它。

4

3 回答 3

26

这似乎在 PowerShell v3 上工作。我没有方便测试的 v2,但我知道有两个选项应该可以工作。首先,您可以映射 PSDrive:

New-PSDrive -Name source -PSProvider FileSystem -Root \\machine1\abc\123 | Out-Null
New-PSDrive -Name target -PSProvider FileSystem -Root \\machine2\c$\Logs | Out-Null
Copy-Item -Path source:\log_1.zip -Destination target:
Remove-PSDrive source
Remove-PSDrive target

如果这是您要做的很多事情,您甚至可以将其包装在一个函数中:

Function Copy-ItemUNC($SourcePath, $TargetPath, $FileName)
{
   New-PSDrive -Name source -PSProvider FileSystem -Root $SourcePath | Out-Null
   New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath | Out-Null
   Copy-Item -Path source:\$FileName -Destination target:
   Remove-PSDrive source
   Remove-PSDrive target
}

或者,您可以为每个路径显式指定提供程序:

Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\machine1\abc\123\log 1.zip" -Destination "Microsoft.PowerShell.Core\FileSystem::\\machine2\\c$\Logs\"
于 2013-02-01T20:26:04.123 回答
3

这对我来说一整天都有效:

$strLFpath = "\\compname\e$\folder"
$strLFpath2 = "\\Remotecomputer\networkshare\remotefolder"  #this is a second option that also will work
$StrRLPath = "E:\localfolder"  
Copy-Item -Path "$StrRLPath\*" -Destination "$strLFpath" -Recurse -force -Verbose

需要注意的事项: Copy-item 将 LAST 项定义为对象。要复制文件夹的内容,您需要 \*

如果要将文件夹本身复制到新位置,则无需声明内容。

于 2016-11-03T20:11:05.117 回答
0

我每天都用这个:

Robocopy /E \\\SOURCEIP\C$\123\  \\\DESTIP\C$\Logs\   
                                             

中间有一块空地。对于 ROBCOPY,/E 进行复制。如果你需要搬家,你可以谷歌。

或者:

$SourceIP = Read-Host "Enter the Source IP"

$DESTIP = Read-Host "Enter the Destination IP"

Robocopy /E \\\\$SourceIP\C$\123\  \\\\$DESTIP\C$\Logs\ 

####Just adjust the C$ path on both#####
于 2021-10-16T03:38:50.743 回答