如何将文件从远程服务器(位于另一个域上)复制到本地?

时间:2019-04-29 12:28:23

标签: powershell

我要将文件从远程服务器(在另一个域上)复制到本地

我是PowerShell的新手,并从技术论坛获得了此代码,但是它不起作用

$Source = "\\xx.xxx.xxx.xx\Users\test\test_1.txt"
$Dest   = "D:\Demo\"
$Username = "domainname\username"
$Password = "xxx"

$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)

$WebClient.DownloadFile($Source, $Dest)

得到以下错误

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:9 char:1
+ $WebClient.DownloadFile($Source, $Dest)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

1 个答案:

答案 0 :(得分:0)

很确定您的问题是您需要指定完整的文件路径。不只是目标文件夹。看着WebClient.DownloadFile,我们可以看到

  

fileName 字符串

     

用于接收数据的本地文件的名称。

所以也许您要做的就是...

$Source = "\\xx.xxx.xxx.xx\Users\test\test_1.txt"
$Dest   = [io.path]::Combine("D:\Demo\", Split-Path $source -Leaf)

它可能被拒绝访问写入文件夹的权限。

相关问题