2

I am running the following command to download a file using the Powershell System.Net.WebClient method:

powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://domain.name/file.name','C:\file.name')"

Is there a way to customize the user-agent and to also retain the one-line command format?

Reason I'm asking is because the website recognizes the request as coming from a bot, and I'm getting an HTTP 403 forbidden error. When I use Internet Explorer, I can download the file without issues. I'd like to keep the one-line format because this command is being called from a batch (.bat) file in Windows.

Ryan Wu
  • 5,963
  • 2
  • 36
  • 47
slantalpha
  • 515
  • 1
  • 6
  • 18
  • I can understand the one liner part. But what I want to know is that have you thought of any approach for the BOT recognition? – Ranadip Dutta Jan 24 '17 at 04:48
  • The BOT recognition is based on the user-agent, according to the host of the website where I'm trying to download the file from. – slantalpha Jan 24 '17 at 05:47

1 Answers1

5

This code snippet will perform the custom user agent part along with webclient :

powershell -command {
    $cli = New-Object System.Net.WebClient;
    $cli.Headers['User-Agent'] = 'myUserAgentString';
    $cli.DownloadFile('https://domain.name/file.name', 'C:\file.name')
}
Phoenix
  • 1,045
  • 1
  • 14
  • 22
ClumsyPuffin
  • 3,909
  • 1
  • 17
  • 17
  • 2
    Thanks, this worked: `powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = 'myUserAgentString';$cli.DownloadFile('https://domain.name/file.name', 'C:\file.name')"` – slantalpha Jan 24 '17 at 05:44