PowerShell download exe from public Github - powershell

I'm unable to download a .exe file from Github. Script works for different sites and downloads files without issues.
https://github.com/ShareX/ShareX/releases/tag/v13.1.0
This is the .exe I'm trying to download:
https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe
>$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
>$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Invoke-WebRequest : The remote server returned an error: (403) Forbidden.
At line:2 char:16
+ $WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Full script:
[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"
$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Write-Output "Downloading $DownloadUrl"
Start-BitsTransfer -Source $WebResponse.BaseResponse.ResponseUri.AbsoluteUri.Replace("%20", " ") -Destination "C:\Users\Pegavo\Desktop\PS\"

You can just simply use Invoke-WebRequest with -OutFile parameter.
Invoke-WebRequest https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe -OutFile "ShareX-13.1.0-setup.exe"
This command will download the file from GitHub and store the result to a file in your current directory.
Or, you can use WebClient.
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile("https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe", "E:\your\path\ShareX-13.1.0-setup.exe")
I tested both on my local machine. Both worked.
Moreover, if you want to understand why Start-BitsTransfer won't work, here.
Edit:
You can get the filename automatically like this one, using Split-Path:
$url = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$file= Split-Path $url -Leaf #file is ShareX-13.1.0-setup.exe now
Invoke-WebRequest $url -OutFile $file

Is there a reason for using HEAD? GET seems to work

Related

How to use PowerShell script to download latest private GitHub release?

I recently wrote a PowerShell script that downloads the latest release from a public repo and that works as intended. However, I want to change my script so it can access my private repo. Here is the code I have tried so far:
# Download latest release from GitHub
$credentials="myPersonalAccessToken"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "token $credentials")
$repo = "myUserName/MyPrivateReleaseRepo"
$file = "MyBinaries.zip"
$releases = "https://api.github.com/repos/$repo/releases"
Write-Host Determining latest release
$tag = (Invoke-WebRequest $releases -Headers $headers | ConvertFrom-Json)[0].tag_name
$download = "https://github.com/$repo/releases/download/$tag/$file"
$name = $file.Split(".")[0]
$zip = "$name-$tag.zip"
$dir = "$name-$tag"
Write-Host Dowloading latest release
Invoke-WebRequest $download -Headers $headers -Out $zip
Write-Host Extracting release files
Expand-Archive $zip -Force
# Cleaning up target dir
Remove-Item "C:\MyOutPutFolder\$name" -Recurse -Force -ErrorAction SilentlyContinue
# Moving from temp dir to target dir
Move-Item $dir\$name -Destination "C:\MyOutPutFolder\$name" -Force
# Removing temp files
Remove-Item $zip -Force
Remove-Item $dir -Recurse -Force
I get the following error only when using my private repo:
Invoke-WebRequest : The remote server returned an error: (404) Not Found
At C:\Script\DownloadLatestGitHubRelease.ps1:25 char:1
+ Invoke-WebRequest $download -Headers $headers -Out $zip
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I've also tried providing bad credentials vs the correct credentials and got a "Bad Credentials" error when providing the incorrect ones as expected, so I believe I'm using the token correctly.
What am I doing wrong? Thanks in advance for any suggestions.
Landed here trying to do the same thing. After digging into some bash scripts that do similar, I came up with this to do the download:
$token = "<your token from https://github.com/settings/tokens>"
$downloadFolder = "C:\temp";
$ownerSlashRepo = "owner/reop";
$tag = "latest";
$json = Invoke-Webrequest -Uri "https://api.github.com/repos/$ownerSlashRepo/releases/$tag" -Headers #{'Authorization'='token '+$token; 'Accept'='application/json'}
$release = $json.Content | ConvertFrom-Json
$release.assets | %{
$asset = $_;
$x = Invoke-Webrequest -Uri $($asset.url) -OutFile "$downloadFolder\$($asset.name)" -Headers #{'Authorization'='token '+$token; 'Accept'='application/octet-stream'}
}
This downloads every file from the release, but you could filter it to just download one as needed, something like:
$release.assets | ?{$_.name -eq 'foo.zip'} | %{
To answer your question, in the original code you're attempting to use the token with https://github.com. I tried similar at first and got similar results to what you describe. I think it is only good for https://api.github.com and the REST API. I also found I had to specify Accept:application/octet-stream when downloading the asset.

Powershell Script to download a file from HTTPS website using

I need to download a file daily from a website where date will passed. I tried using following code -
$url = "https://www.theocc.com/webapps/threshold-securities?reportDate=20190730"
$output = "C:\Users\Himanshu.Vats\Downloads\"
Invoke-WebRequest -Uri $url -OutFile $output
But it is giving error -
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:3 char:1
+ Invoke-WebRequest -Uri $url -OutFile $output
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
It's because your path is not a path file (you have a '\' to end path)
try this:
$url = "https://www.theocc.com/webapps/threshold-securities?reportDate=20190730"
$output = "C:\Users\Himanshu.Vats\Downloads\result.csv"
Invoke-WebRequest -Uri $url -OutFile $output

Invoke-WebRequest command fails only when run as script

I have this function in a powershell script to deploy a war file to a tomcat server. Similar functions for the other tomcat commands (stop, undeploy, list, etc) work fine. When I run the script from a shell it works. When I run the script from the Jenkins Powershell plugin it fails.
Function DeployTomcatWebApp([PSCredential]$cred, [string]$server, [string]$app)
{
Write-Host "Deploying $app"
$uri = $server+"/manager/text/deploy?path=/$app&update=true"
$warpath = ($pwd).path + "/$app/build/dev/$app.war"
$r = Invoke-WebRequest -Uri $uri -Credential $cred -InFile $warpath -UseBasicParsing -Verbose -Debug -Method PUT -TimeoutSec 30000 -DisableKeepAlive
Write-Host $r.Content
Return $r
}
The Console Log shows
Deploying DERF
D:\Jenkins\workspace\JenkinsTest_2_AD_DERF\DERF\build\dev\DERF.war
http://mytomcat:8080/manager/text/deploy?path=/DERF&update=true
VERBOSE: PUT http://mytomcat:8080/manager/text/deploy?path=/DERF&update=true with -1-byte payload
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At C:\Users\<jenkinsuser>\AppData\Local\Temp\jenkins1024175502034764284.ps1:49 char:10
+ $r = Invoke-WebRequest -Uri $uri -Credential $cred -InFile $warpa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
The Jenkins Powershell plugin extracts the script from the jenkins job and create the .ps1 file in Local/Temp and executes the script by calling
powershell -NonInteractive -ExecutionPolicy Bypass <tempscriptname>
I am stumped and looking for any insight to debugging this.

Invoke-WebRequest Failure

The issue is that I got a script from a vendor to pull data from a DB.
The script is having a problem with the Invoke-WebRequest Section.
Here is the Script:
$url = #'
https://demo.liquidwarelabs.com/lwl/api?json={"inspector":"0","basis":"users","date":"yesterday","limit":"0","columns":"user_name,login_count","output":"1:html","header":"1"}
'#
$output = "c:\export\Tier1\view1.csv"
Invoke-WebRequest $url -OutFile $output
Here is the Error I get:
Invoke-WebRequest : The underlying connection was closed: An
unexpected error occurred on a send. At C:\Scripts\3.ps1:38 char:1
+ Invoke-WebRequest $url -OutFile $output
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest],
WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
You probably need to URL encode the JSON:
$url = 'https://demo.liquidwarelabs.com/lwl/api?json='
$json = '{"inspector":"0","basis":"users","date":"yesterday","limit":"0","columns":"user_name,login_count","output":"1:html","header":"1"}'
$encodedjson = [System.Web.HttpUtility]::UrlEncode($json)
$output = "c:\export\Tier1\view1.csv"
Invoke-WebRequest ($url+$encodedjson) -OutFile $output

Running PowerShell code in powershell works, executing as .ps1 doesn't

I wrote some code which tries to Get a value from one Rest API and post it to another.
I have the code saves in a .ps1 file. If I edit it and run it (or just copy and paste it into an empty PowerShell terminal) it does what I expect. However when I try to run the same .ps1 file directly I get an error on the 2nd Invoke-RestMethod.
Don't understand why I'm getting a different result and the error message not giving me many clues.
What am I doing wrong?
The code I am using is (with modified API key):
$encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($APIkey+":"))
$headers = #{"Content-Type" = "application/json"; "AUTHORIZATION" = "Basic $encoded"}
$APIkey = "123456789"
$metricId = "123"
$r = Invoke-RestMethod -Uri https://coinbase.com/api/v1/currencies/exchange_rates
$metric = [PSCustomObject]#{
value = [Math]::Round($r.btc_to_eur, 2)
}
$baseuri = "https://api.numerousapp.com/v1/metrics/$metricId/events"
$data = ConvertTo-Json $metric
Invoke-RestMethod -Uri $baseuri -Body $data -Headers $headers -Method Post
And the error message I get when running the .ps1 file is:
Invoke-RestMethod : :
At C:\NumerousBitcoinEur.ps1:13 char:1
+ Invoke-RestMethod -Uri $baseuri -Body $data -Headers $headers -Method Post
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I'm using PowerShell 4.0
$APIkey is being set after it is being used, which must be wrong. It probably works in the console because $APIkey happens to already be set.
If you like (I think it's a good idea), you can add the following to the top of your scripts to catch errors like this one.
Set-StrictMode -Version Latest