Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

powercli - Need help on Powershell Copy-Item from network drives

I am trying to use Copy-Item from remote machine to another remote machine with the command:

Copy-Item -Path "\machine1abc123log 1.zip" -Destination "\machine2\c$Logs"

I am constantly getting Error "Cannot find Path "\machine1abc123log 1.zip"

I can access that path and copy manually from there.

I am opening PowerCLI as administrator and running this script... I am absolutely stuck here and not sure how to resolve it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This seems to work as is on PowerShell v3. I don't have v2 handy to test with, but there are two options that I'm aware of, which ought to work. First, you could map PSDrives:

New-PSDrive -Name source -PSProvider FileSystem -Root \machine1abc123 | Out-Null
New-PSDrive -Name target -PSProvider FileSystem -Root \machine2c$Logs | Out-Null
Copy-Item -Path source:log_1.zip -Destination target:
Remove-PSDrive source
Remove-PSDrive target

If this is something you're going to do a lot, you could even wrap this in a function:

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
}

Alternately, you can explicitly specify the provider with each path:

Copy-Item -Path "Microsoft.PowerShell.CoreFileSystem::\machine1abc123log 1.zip" -Destination "Microsoft.PowerShell.CoreFileSystem::\machine2\c$Logs"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...