Last active
May 19, 2023 20:48
-
-
Save mujahidk/e22d7c27049235ab1d023577d61d3a4b to your computer and use it in GitHub Desktop.
PowerShell WebClient script with basic authentication to export content from a website. Simple alternative to curl in Windows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Uncomment to disable Certificate errors | |
# [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} | |
$url = "http://example.com/data.json" | |
$user = "data-user" | |
$password = "password" | |
# Output file path into current directory. | |
$file= ($pwd).path + "\export.json" | |
Write-Host "Exporting data to: $file" | |
# Create a WebClient | |
$webclient = New-Object System.Net.WebClient | |
# Basic authentication encoding. | |
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password)); | |
# Set Authorization HTTP header with Basic authentication information. | |
$webclient.Headers["Authorization"] = "Basic $basic" | |
# Export the contents to file. | |
$webclient.DownloadFile($url, $file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM Run the curl.ps1 in powershell. '-executionpolicy bypass' to run it on systems where running scripts is disabled. | |
powershell -executionpolicy bypass -file curl.ps1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment