Pages

21 December 2020

Download Files Through Authenticating Proxy with PowerShell

Download Files Through Authenticating Proxy with PowerShell

1st Method:
$source = "https://www.7-zip.org/a/7z1900-x64.exe";            
$dest = "C:\Temp\7z1900-x64.exe";            
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12            
$WebClient = New-Object System.Net.WebClient;            
$WebProxy = New-Object System.Net.WebProxy("http://proxy:8080",$true);            
$Credentials = (New-Object Net.NetworkCredential("USERNAME","PASSWORD","domain.com")).GetCredential("http://proxy","80", "KERBEROS");            
#$Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials            
$WebProxy.Credentials = $Credentials;            
$WebClient.Proxy = $WebProxy;            
$WebClient.DownloadFile($source,$dest);
Alternative Methods:
New-Item -ItemType Directory -Force -Path C:\Temp # Create Temp folder if it doesn't already exist            
$proxy="http://proxy:8080";            
$exclusionList="localhost;*.domain.local;10.*"
# Set winhttp proxy for PowerShell
netsh winhttp set proxy $proxy $exclusionList
# Prepare PowerShell to Use Default Credentials
[system.net.webrequest]::defaultwebproxy = New-Object system.net.webproxy($proxy)            
[system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials            
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true            
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Download using the most efficient method
$url = "https://www.7-zip.org/a/7z1900-x64.exe"             
$temp = "C:\Temp\7z1900-x64.exe"            
Import-Module BitsTransfer            
Start-BitsTransfer -Source $url -Destination $temp
Or:
$source = "https://www.7-zip.org/a/7z1900-x64.msi";            
$dest = "C:\Temp\7z1900-x64.msi";            
$browser = New-Object System.Net.WebClient            
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials;            
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12            
$browser.DownloadFile($source,$dest);
Or:
$source = "https://www.7-zip.org/a/7z1900-x64.msi";            
$dest = "C:\Temp\7z1900-x64.msi";            
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12            
(new-object System.Net.WebClient).DownloadFile($source,$dest)
Or:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12            
[Net.WebRequest]::DefaultWebProxy.Credentials = [Net.CredentialCache]::DefaultCredentials; `
iex ((New-Object Net.WebClient).DownloadString('https://www.7-zip.org/a/7z1900-x64.msi'))
Or:
$page = (new-object net.webclient)            
$page.UseDefaultCredentials = $True            
$Page.DownloadString('https://www.7-zip.org/a/7z1900-x64.msi')

No comments:

Post a Comment