需要有关网络驱动器的Powershell Copy-Item的帮助

Gee*_*eth 17 powershell copy-item powercli

我试图使用命令Copy-Item从远程机器到另一台远程机器:

Copy-Item -Path "\\machine1\abc\123\log 1.zip" -Destination "\\machine2\\c$\Logs\"
Run Code Online (Sandbox Code Playgroud)

我经常得到错误" Cannot find Path "\\machine1\abc\123\log 1.zip"

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

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

Kev*_*inD 23

这似乎与PowerShell v3一样有效.我没有v2方便测试,但有两个选项,我知道,应该工作.首先,你可以映射PSDrives:

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
Run Code Online (Sandbox Code Playgroud)

如果你要做很多事情,你甚至可以将它包装在一个函数中:

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
}
Run Code Online (Sandbox Code Playgroud)

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

Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\machine1\abc\123\log 1.zip" -Destination "Microsoft.PowerShell.Core\FileSystem::\\machine2\\c$\Logs\"
Run Code Online (Sandbox Code Playgroud)

  • 最后一点,前面是"Microsoft.PowerShell.Core\FileSystem ::`",对我有用.谢谢. (6认同)
  • 仅供参考,您可以通过运行`powershell.exe -version 2`在v2模式下运行Powershell 3,您可以通过检查`$ Host.Version`属性来验证这一点. (2认同)