I'm trying to call a report server report with parameters and save it as PDF. It works fine in a browser. The PDF file is saved, but I can't choose where to save it.
I'm trying to use Powershell to archieve it.
$URI = "http://servername/ReportServer/Pages/ReportViewer.aspx?%2fReportname%2fTemp&rs:Format=PDF&Guid=parameter1&StartDate=01-02-2018&StartTime=07:00&EndDate=01-02-2018&EndTime=17:00"
$Outputfile = "C:\temp\test.pdf"
(Invoke-WebRequest -Uri $URI -OutFile $Outputfile -UseDefaultCredentials -TimeoutSec 30)
The Powershell code downloads a PDF with the correct layout, but no data. It says "No data available". Any ideas?
Related
I have a file which I download using the REST API.
Just to emphasize that I tried with both commands: Invoke-RestMethod and Invoke-WebRequest
$uri = "https://bitbucket.org.dev/projects/TEST/repos/fa/browse/Packages/ATS.txt"
$API_KEY="ZTU2MT"
Invoke-WebRequest -Headers #{Authorization=$("Basic {0}" -f $API_KEY)} -Uri $uri -OutFile ATS.txt
If I access that URI in browser file or download it manually file can be viewd without any issue in clear way.
This is the content of the file (its begining)
#
# exported extension module.
# source ""
# timestamp (utc) "2020-03-30 12:06:23.3"
# ***** DO NOT EDIT! *****
But download file looks completely different (like it is in HTML format)
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-..."
Also I have some zip file which needs to be downloaded as well, but with it also I am getting invalid file which cannot be extracted (opened)
As written in MSDN, Invoke-WebRequest returns an object of type BasicHtmlWebResponseObject.
You need to select the Content property to get the text you are looking for.
Invoke-WebRequest -Headers #{Authorization=$("Basic {0}" -f $API_KEY)} -Uri $uri | Select-Object -Expand Content | Out-File -FilePath c:\somefile.txt
Update:
Check this post to find more info about downloading files from a private repository in BitBucket.
https://stackoverflow.com/a/50222671/13440610
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.
Need: A PowerShell script to call an SSRS report (SSRS 2012) such that the web-call appears as a regular interactive request when viewed in SSRS's ExecutionLog3 (SSRS's log DB view).
Problem: I've tinkered around w/ the following script to try to figure out what parameters are necessary to force the SSRS server to log the call as if it was coming from a human/browser. This script works fine. I'm connected & getting a response for all 3 methods, but SSRS isn't logging it at all. Were I to invoke the same report in IE/Chrome manually - SSRS logs it accordingly & correctly.
I've used the F12 tools in Chrome to see if I can find, perhaps, some JS code that is actually doing the log-registration-req, alas - too advanced for me.
Thoughts?
# My AdventureWorks Sales Order Detail (.rpl) requires no parameters.
$RS1 = $null
$RS2 = $null
$RS3 = $null
# Try using the direct URL of the report approach
$Uri1 = 'http://reportstest.domain.com/home/report/AdventureWorks-Test/Sales%20Orders/Sales%20Order%20Detail.rdl'
# Try using the ReportViewer.aspx followed by path & variables approach
$Uri2 = 'http://reportstest.domain.com/Service/Pages/ReportViewer.aspx?%2fAdventureWorks-Test%2fSales+Orders%2fSales+Order+Detail&rc:Command=Render'
# Try using the Service approach
$Uri3 = 'http://reportstest.domain.com/Service?/AdventureWorks-Test%2fSales+Orders%2fSales+Order+Detail'
# Try each Uri to see if we create an entry in SSRS's ExecutionLog3
$RS1 = Invoke-WebRequest -Uri $Uri1 -UseDefaultCredentials -DisableKeepAlive
$RS2 = Invoke-WebRequest -Uri $Uri2 -UseDefaultCredentials -DisableKeepAlive
$RS3 = Invoke-WebRequest -Uri $Uri3 -UseDefaultCredentials -DisableKeepAlive
# See the response for each attempt, check for '200'
$RS1.StatusCode
$RS2.StatusCode
$RS3.StatusCode
I am trying to download a google spreadsheet via an invoke-webrequestin powershell.
My link looks something like this and I can confirm that if I go to the link it prompts me to download the file I want...
$url = https://docs.google.com/spreadsheets/d/XXXXXXXXXXXXXXXX/export?format=csv
I have tried downloading the file via 2 ways:
Invoke-WebRequest $url -OutFile $saveLocation
(New-Object System.Net.WebClient).DownloadFile($url, $saveLocation)
Both of these just download the HTML for the page, and not the actual file.
The spreadsheet I am downloading is 100% public so it is not like I need to be logged into a google account to view it.
I made the test and I can quite safely say that your file is not public.
I have reproduced your issue before making my test sheet public, and I could not afterwards.
Tested with this :
$url = "https://docs.google.com/spreadsheets/d/1OOc5aiG3hh8mrGF4p7NJXKI8KqBSkS2ZjsBXtb4qpz8/export?format=csv"
Invoke-WebRequest $url -OutFile "C:\test.csv"
How can I use Invoke-WebRequest to download a file but automatically make the file name the same as if I downloaded via browser? I haven't found a way to make -OutFile work without manually specifying the file name. I'm fine with this involving a few other lines of code.
A good solution will:
Work even if the file name isn't in the request URL. For example, the URL to download the Visual Studio x64 Remote Debugging Tools is http://go.microsoft.com/fwlink/?LinkId=393217 but it downloads the file rtools_setup_x64.exe.
Not save the whole file to memory before writing to disk, unless that's what Invoke-WebRequest already does even with the -OutFile parameter (?)
Thanks!
For the example given you're going to need to get the redirected URL, which includes the file name to be downloaded. You can use the following function to do so:
Function Get-RedirectedUrl {
Param (
[Parameter(Mandatory=$true)]
[String]$URL
)
$request = [System.Net.WebRequest]::Create($url)
$request.AllowAutoRedirect=$false
$response=$request.GetResponse()
If ($response.StatusCode -eq "Found")
{
$response.GetResponseHeader("Location")
}
}
Then it's a matter of parsing the file name from the end of the responding URL (GetFileName from System.IO.Path will do that):
$FileName = [System.IO.Path]::GetFileName((Get-RedirectedUrl "http://go.microsoft.com/fwlink/?LinkId=393217"))
That will leave $FileName = rtools_setup_x64.exe and you should be able to download your file from there.
Try this method (may not always work because the file name may not be in the response header)
call Invoke-WebRequest to get the result. then you can inspect the result to look at what is in the headers.
get the file name from the response header (this could be in Headers.Location, or some other place. When I run my query for a url that i was troubleshooting, I found it in the Headers["Content-Disposition"] and it looks like inline; filename="zzzz.docx"
Create a new file based on the name and write the content to this file
Here is code sampe:
$result = Invoke-WebRequest -Method GET -Uri $url -Headers $headers
$contentDisposition = $result.Headers.'Content-Disposition'
$fileName = $contentDisposition.Split("=")[1].Replace("`"","")
$path = Join-Path $yourfoldername $fileName
$file = [System.IO.FileStream]::new($path, [System.IO.FileMode]::Create)
$file.write($result.Content, 0, $result.RawContentLength)
$file.close()
Thanks to Ryan I have a semi-usable function:
Function Get-Url {
param ( [parameter(position=0)]$uri )
invoke-webrequest -uri "$uri" -outfile $(split-path -path "$uri" -leaf)
}
A graphic file and xml file I have been able to download. When I try to download this webpage and open it with Edge it will work at times.
Essence of my code - this works.. using PS 5.1
I have commented out the normal -Outfile statement because that would imply I knew the filename in advance.
I have put this together based upon a number of sources, there are lots of ways of parsing the Content-Disposition headers so use whatever works for you.
$outpath = "C:\\temp\\"
The call:
$result = Invoke-WebRequest -method GET -Uri $resourceUrl -Headers
$resourceHeaders -Verbose #-OutFile $($outPath+"$nodeId.pdf")
parsing
$outFilename = $outpath+$result.Headers.'Content-Disposition'.Split("=")[1].Split('\\""')[1] #shaky parsing - might require improvement in time
Writing the file (this is mostly pdf's for me
[System.IO.File]::WriteAllBytes($outFilename, $result.content)
powershell.exe Invoke-WebRequest -Uri serverIP/file.exe -OutFile C:\Users\file.exe