Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while trying to download Files using Power Shell

Tags:

powershell

I have been getting the below error when i try to download file from a https URL. I am using Power Shell 3.0

Shell Script

$Username = 'user'
$Password = 'password'
$Url = "url"
$Path = "path"
$WebClinet = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
$WebClient.DownloadFile( $Url, $Path )

Error :

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request." At line:7 char:1 + $WebClient.DownloadFile( $Url, $Path )

Any suggestions?

like image 789
Dhruv Avatar asked Sep 06 '25 03:09

Dhruv


1 Answers

Wrap you existing powershell code in a try catch block and write out the exception details in order to understand what actual error is causing the problem.

Try
{
    $Username = 'user'
    $Password = 'password'
    $Url = "url"
    $Path = "path"
    $WebClient = New-Object System.Net.WebClient
    $WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
    $WebClient.DownloadFile( $Url, $Path )
}
Catch [Exception]
{
    Write-Host $_.Exception | format-list -force
}

And also there is a typo in your code. Check your variable names.

like image 144
ZiggZagg Avatar answered Sep 07 '25 19:09

ZiggZagg