2013-03-27 88 views
0

我收到此错误尝试将文件下载到一个目录在远程服务器上

异常调用“DownloadFile”与“2”参数(S):“一个Web客户端请求期间发生异常。”

从这个脚本

$username = "Administrator" 
$password = "PASSWORD" 
$secstr = New-Object -TypeName System.Security.SecureString 
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)} 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr 
$url = "http://www.website.com/file.zip" 
$path = "C:\file.zip" 
$client = new-object System.Net.WebClient 
$client.DownloadFile($url, $path) 
Invoke-Command -ComputerName 69.69.69.69 -ScriptBlock { $client } -credential $cred 

的Windows Web服务器上运行2008

脚本目的是file.zip下载到远程服务器(也有数百台服务器,所以我不能每次提示输入密码)并查看下载的进度条。

任何想法?

回答

2

尝试

$username = "Administrator" 
$password = "PASSWORD" 
$secstr = New-Object -TypeName System.Security.SecureString 
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)} 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr 

$command = { 
    $url = "http://www.website.com/file.zip" 
    $path = "C:\file.zip" 
    $client = new-object System.Net.WebClient 
    $client.DownloadFile($url, $path) 
} 

    Invoke-Command -ComputerName 69.69.69.69 -ScriptBlock $command -credential $cred 
+2

+1不过,我会做'$ url'和'$ path'脚本块参数,并通过调用' - 命令-ArgumentList ...'通过他们。使脚本更易于维护。 – 2013-03-27 09:23:32

+0

您可能需要在$ command block的末尾调用$ client.dispose() – Cirem 2014-06-10 19:33:20

相关问题