Invoke-WebRequest pass url as script argument (PowerShell) - powershell

I am trying to download file from the web using following command
Invoke-WebRequest $url -OutFile $filePath -Headers $Headers
I have argument, which contains this url and it is passed as parameter
[string]$artifactHttpAddress = $args[2]
Currently its value is
http://10.45.48.26/httpAuth/repository/downloadAll/TeamCityTest_Build/529:id/artifacts.zip
So, when I try to invoke WebRequest with following command
Invoke-WebRequest $artifactHttpAddress -OutFile c:/test.zip -Headers $Headers
it is downloading empty zip file .
but when I try to assign this url to the variable and invoke web request
$url = "http://10.45.48.26/httpAuth/repository/downloadAll/TeamCityTest_Build/529:id/artifacts.zip"
Invoke-WebRequest $url -OutFile c:/test.zip -Headers $Headers
It is working correctly, downloads zip file, which have some content in it.
I tried following script
Write-Host([string]$url -eq [string]$artifactHttpAddress)
Write-Host([string]$url)
Write-Host([string]$artifactHttpAddress)
It outputs
False
http://10.45.48.26/httpAuth/repository/downloadAll/TeamCityTest_Build/528:id/artifacts.zip
http://10.45.48.26/httpAuth/repository/downloadAll/TeamCityTest_Build/531:id/artifacts.zip
What is happening and why?
p.s. this script is inside ScriptBlock

It looks to me, based on your output, that $url and $artifactHttpAddress are not the same value. Does the ZIP file exist at the URL with 531 in it?

Related

Using PowerShell to download file from private GitHub repository (using OAuth)

I have searched extensively for a solution but have yet to find success. I just want to be able to download files from my private GitHub repo using PowerShell. I want to use OAuth, not basic auth, so I have generated a token. But from here, none of the examples I've referenced worked for me. Best I could do was get a "Not Found" response.
An example of code I've tried is:
Invoke-WebRequest https://api.github.com/repos/MyAccount/MyRepo/contents/MyFile.txt -Headers #{"Authorization"="token 123456789012345678901234567890"} -OutFile C:\Temp\MyFile.txt
Result:
Invoke-WebRequest : {"message":"Not
Found","documentation_url":"https://docs.github.com/rest/reference/repos#get-repository-content"}
I'm fairly confident that I have the authentication right. I believe I just have the path wrong path to my file. Any help would be greatly appreciated.
Potential Duplicate use case relative to this SO discussion ...
PowerShell: retrieve file from GitHub
$url = 'https://github.com/mycompany/myrepo/blob/master/myscript.ps1'
$wc = New-Object -TypeName System.Net.WebClient
$wc.Headers.Add('Authorization','token your_token')
iex ($wc.DownloadString($url))
.. without Invoke-WebRequest of course.
See also:
Using PowerShell and oAuth
# Modified article code
Invoke-RestMethod https://api.github.com/repos/MyAccount/MyRepo/contents/MyFile.txt -Method Get -Headers #{"Authorization" = "Bearer $accessToken"}
I had to change the script in Powershell to get it working:
$credentials="<github_access_token>"
$repo = "<user_or_org>/<repo_name>"
$file = "<name_of_asset_file>"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "token $credentials")
$headers.Add("Accept", "application/json")
$download = "https://raw.githubusercontent.com/$repo/main/$file"
Write-Host Dowloading latest release
Invoke-WebRequest -Uri $download -Headers $headers -OutFile $file

PowerShell Invoke RestMethod response as file to PDF

I am using invoke-restmethod GET in Powershell which produces a PDF file as a response.
I can see that file as an encoded format in the PowerShell window.
I need to create a PDF file from the response.
How to achieve?
Use the -OutFile parameter:
Invoke-RestMethod $uri -Method Get -OutFile output.pdf

How to download a file accepting license using powershell

I am trying to download package from the below link using powershell.
https://www.tenable.com/downloads/nessus-agents
i do not have direct link for these package also when i click on download it ask to agree. I was able to do it on Linux using command shown below. Kindly advise how can i do it in windows.
"wget --no-check-certificate --post-data='accept="I accept the terms of this license"&x=""&sid=5mcia8gchg28attkc9oarah153&p=NessusAgent-7.4.2-amzn.x86_64.rpm' 'https://www.tenable.com/downloads/nessus-agents' -O NessusAgent-7.4.2-amzn.x86_64.rpm"
could not find anything tried option with invoke-webrequest
Invoke-RestMethod -Uri 'https://www.tenable.com/downloads/nessus-agents'
There's a GET query string parameter that indicates acceptance.
Simply add i_agree_to_tenable_license_agreement=true to your query string parameters.
Invoke-WebRequest -Uri 'https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents/downloads/9762/download?i_agree_to_tenable_license_agreement=true' -OutFile 'NessusAgent-7.4.2-x64.msi'
You can easily get the IDs of the other files from their API endpoint like so:
(Invoke-WebRequest -Uri 'https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents' | ConvertFrom-Json).downloads | Format-Table -AutoSize
This is similar syntax in Powershell, but it's just downloading a file with contents "OK".
$body = 'accept="I accept the terms of this license"&x=""&sid=5mcia8gchg28attkc9oarah153&p=NessusAgent-7.4.2-amzn.x86_64.rpm'
$uri = 'https://www.tenable.com/downloads/nessus-agents'
$resp = Invoke-WebRequest -Method Post -Body $body -Uri $uri -OutFile .\NessusAgent-7.4.2-amzn.x86_64.rpm
Maybe the "sid" variable needs to change per request.

How to HTTP PUT a file in Powershell and capture response body?

I'd like to upload a file using powershell and capture the resulting response body. How can one do this?
I've tried:
$env:DEPLOY_URL = & curl.exe https://make.mudlet.org/snapshots/Mudlet-$env:VERSION$env:MUDLET_VERSION_BUILD-windows.zip --upload-file Mudlet-$env:VERSION$env:MUDLET_VERSION_BUILD-windows.zip 2>&1
But that does not work and says Command executed with exception: without listing the exception.
Explore using the Invoke-RestMethod command. You can save the response body using the -OutFile parameter.
An example PUT would be as follows where $uri is the destination, $inFile is the path to the local file, and $outFile is the path to where to save the response body:
Invoke-RestMethod -Uri $uri -Method PUT -InFile $inFile -OutFile $outFile

Cannot unzip zip file downloaded with Powershell

I have a zip file hosted at a URL. I'm writing a Powershell script to get that zip file using Invoke-RestMethod and save it to disk. When I run the Powershell script, the zip file is downloaded and saved to disk, however, I cannot open or extract it. Does anyone know what I'm doing wrong? Here is my code that handles the zip download/save. Here is my code (I've changed the URL for the zip for security reasons. If I type in the actual URL into a browser it will be downloaded by the browser after which I can locate it on disk and extract it).
$username = "uname"
$password = "pword"
$boundary = [guid]::NewGuid().ToString().Replace("-", "").Substring(0, 16)
$enc = [System.Text.Encoding]::GetEncoding("utf-8")
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$headers = #{}
$headers.Add("Host", "api.bitbucket.org")
$headers.Add("Authorization", "Basic $base64AuthInfo")
$repoResult
$repoResult = Invoke-WebRequest -Uri "https://bitbucket.org/teamAccount/repoName/get/master.zip" -Headers $headers -Method GET -Verbose -OutFile .\output.zip
}
EDIT/UPDATE:
Thanks to TesselatedHeckler's comment, I've discovered that the downloaded zip is actually an HTML login page. I also realized that I didn't have my headers (which contains the login info) in Invoke-RestMethod line. I've updated the post and my script. Now, whenever I run my script, I get a (500) Internal Server Error.