reefine
reefine

Reputation: 853

Trying to download a file to a directory on a remote server

I am getting this error

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."

From this script

$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

Running on Windows Web Server 2008

Script purpose is to download file.zip to a remote server (there are hundreds of servers so I can't be prompted for the password each time) and see the progress bar of the download.

Any ideas?

Upvotes: 0

Views: 2308

Answers (1)

Paul Rowland
Paul Rowland

Reputation: 8352

Try

$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

Upvotes: 2

Related Questions