Invoke-WebRequest error when running ps1 script - powershell

I am trying to write a ps 3.0 script that will post a list of 200 words to a web server. After I have prepared the list, I use:
Invoke-WebRequest -Uri $url -Method POST -Body $PostParams -OutFile C:...\upload_log.tmp.txt
When I run this script from the ISE editor it works fine. However, when I run it from outside the editor (cmd, ps shell, task scheduler) I get this error message:
Invoke-WebRequest : You must write ContentLength bytes to the request stream
Any suggestions?

Related

Powershell equivalent to curl -F to upload a file

I need to upload files via commandline / powershell.
It works fine with
curl -F "file=#test.txt" https://api.anonfiles.com/upload
However, curl does not exist on Windows Server 2016 and I do not want to tell my clients to set it up. So I am looking for a powershell alternative to accomplish this task. I tried various solutions, but none of them worked. What I tried so far:
(1)
$postParams = #{file='C:\users\user\testfile.txt'}; Invoke-WebRequest -Uri https://api.anonfiles.com/upload -Method POST -Body $postParams
(2)
Invoke-WebRequest -UseBasicParsing https://api.anonfiles.com/upload -ContentType "application/json" -Method POST -InFile "C:\users\user\testfile.txt"
(3)
$file=[io.file]::readallbytes('c:\users\user\testfile.txt')
Invoke-WebRequest -UseBasicParsing https://api.anonfiles.com/upload -ContentType "application/json" -Method POST -Body "{ '#file':'"$file"'}"
None of it works. I canot believe it's so hard to replace an curl oneliner in powershell... The error in each case is a 400 http error, the request is wrong.
How do I send the above mentioned curl-request equivalent in powershell? The site is https://anonfiles.com/docs/api
I gave up. It's not possible in Powershell.
My App will just download curl.exe as standalone from now on, if it's not there.

Download Artifactory Folder as an Archive using PowerShell

I'm trying to download an Artifactory folder as a tar.gz file using the PowerShell command line. In Bash, the following command does the job:
curl -i -v -XGET -H 'X-JFrog-Art-Api:<my API key>' "https://artifactory.<myrepo.com>/path/to/files?archiveType=tar.gz" --output ./file_location/filename.tar.gz
In PowerShell, however, this doesn't seem to work:
Invoke-RestMethod -uri "https://artifactory.<myrepo.com>/path/to/files?archiveType=tar.gz" -Method 'Get' -OutFile .\file_location\filename.tar.gz -H #{'X-JFrog-Art-Api' ='<my API key>';'Content-Type'='multipart/form-data'}
This operation creates filename.tar.gz, but the file is just a HTML file giving the directory structure rather than the actual files.
If I only want an individual file, I can use PowerShell to download files (not an archive) with:
Invoke-RestMethod -uri "https://artifactory.<myrepo.com>/path/to/files/filename.xyz" -Method 'Get' -OutFile .\file_location\filename.xyz -H #{'X-JFrog-Art-Api' ='<my API key>';'Content-Type'='multipart/form-data'}
And filename.xyz is transferred to the local machine. So I'm a bit confused with why the tool to make an archive fails.
I figured it out!
Invoke-RestMethod -Uri "https://my-artifactory-server/artifactory/api/archive/download/path/to/some/folder?archiveType=zip -Method "Get" -Headers #{"Authorization"="Basic $my_artifactory_api_token"; 'Content-Type'='multipart/form-data'} -Outfile c:\destination_location\folder.zip
You need to add "api/archive/download" to the Uri, after the hostname.

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.

Invoke-WebRequest pass url as script argument (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?

Powershell Invoke-WebRequest casting issue on PS3 not PS4

I run the following command in my PS script:
$apiResult = Invoke-WebRequest -Uri $myURL -Body $body -DisableKeepAlive -Headers $headers -Method Post -TimeoutSec 120 -ContentType $contentType -UseBasicParsing
This fails on the following error:
Unable to cast object of type \u0027System.Management.Automation.PSObject\u0027 to type \u0027System.String\u0027
Here is the catch:
The types of the supplied variables are System.String (for $body, $myURL, $contentType) and System.Collection.Hashtable (for $headers)
This fails without calling the actual service behind the URL (verified in service logs)
Running this script with the same parameters on the machine with Powershell version 4 does not create this error and actually returns expected result
I believe this is a bug but i have not been able to find any mention of it on google or any other areas including documentation.
Well...After long time digging I found that the issue was one of the values in my headers hashtable was not a string! not sure why this passes on powershell 4...maybe the casting there is more flexable than in PS3