0

I want to make a script to download files from a website protected with a username and password, but since the website asks for username and password first, I am stuck and do not know what do to make the script.

$iIp = Read-Host -Prompt 'eneter your ip'

foreach($line in Get-Content .\urls.txt) {
    if($line -match $regex){

    $url = "$Ip/$line"

    if (!(Test-Path .\descargados)){
        mkdir .\downloads | Out-Null
    }
    $path = ".\downloads\$line"


    if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) {
      $path = Join-Path $pwd (Split-Path -leaf $path)
    }


    "downloading and saving [$line]"
    $client = new-object System.Net.WebClient
    $client.DownloadFile($url, $path)


    $path
    }
}
oguz ismail
  • 1
  • 16
  • 47
  • 69
Mario
  • 1
  • 1
  • 4
    Your code looks like Powershell to me, so I am not sure why you used the bash, shell and batch-file tags. – Squashman Apr 04 '19 at 13:36
  • yes, i am open to everything, i just posted that as an example but i can use other things. if the the example of powershell i post it, is leading to confusion you can delete it because i cant edit my question because i am new here – Mario Apr 04 '19 at 13:40
  • Nope. That is not how you ask questions on StackOverFlow. Your questions must be specific to the programming language you are using. – Squashman Apr 04 '19 at 13:43
  • thank you for editing it – Mario Apr 04 '19 at 13:45
  • I think [answer for using WebClient authentication](https://stackoverflow.com/a/1883697/3344991) will help. It's in C#, but the class is the same in both languages. – AlwaysLoadingData Apr 04 '19 at 19:03

1 Answers1

0

I would take a look at Invoke-WebRequest as it has the ability for you to use credentials or fill out forms via POST. The examples there show several use cases. You may need to use -AllowUnencryptedAuthentication as you don't seem to have any ssl in place.

$credential = Get-Credential

Invoke-WebRequest -Uri $url `
              -AllowUnencryptedAuthentication `
              -Credential $credential `
              -OutFile $path
StephenP
  • 3,895
  • 18
  • 18