Streaming output from Invoke-WebRequest to screen - powershell

I am trying to invoke a web request which will print out a stream of plain-text live. It currently works, but doesn't produce any output until the request is complete. I'm trying to find a way to see the content as soon as comes down the wire. I can currently do with with a curl command from a Linux box and it shows be each line of output as soon as it's produced. The PowerShell command I'm trying to use it something like this:
Invoke-WebRequest -Body #{id=123} -Method POST -Uri http://server/run_command/
The corresponding curl command on Linux is:
curl -d id=123 -X POST http://server/run_command/ --no-buffer
The --no-buffer option is only needed when sending the output to another command like grep. How can I accomplish this using just PowerShell?

I am not able to test this myself right now, but have you tried expanding the content?
(Invoke-WebRequest -Body #{id=123} -Method POST -Uri http://server/run_command/).Content

There are other APIs that allow reading response stream without buffering it in full.
For example https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.getresponsestream?view=net-5.0
PowerShell version of the same could look like this:
$webrequest = [System.Net.HttpWebRequest]::Create("https://en.wikipedia.org/wiki/Internet_Archive")
$responseStream = $webrequest.GetResponse().GetResponseStream()
$streamReader = [System.IO.StreamReader]::new($responseStream)
[char[]]$buffer = [char[]]::new(256)
while (($count = $streamReader.Read($buffer, 0, $buffer.Length)) -gt 0)
{
# output count characters of the buffer
}
$response.Close() > $null
$responseStream.Close() > $null

Related

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.

Need curl command to ElasticSearch for PowerShell

My application is spamming my log with disk threshold messages. I already found out (here low disk watermark [??%] exceeded on) what I need to do. I have attached the a curl command below, which should solve my problem. Unfortunately I am in Windows, so no curl.
I have already tried building my own "Invoke-RestMethod" command, which all did not work (and I also forgot to preserve them for reference here). I looked at parse-curl on github, but did not understand how it'll help me. SO I'm a bit lost in documentation... The errors form Invoke-RestMethod in the shell also were not very helpful in the end.
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"persistent" : {
"cluster.routing.allocation.disk.threshold_enabled" : "false"
}
}
'
So... I just need a working command for PowerShell to be happy.
$body = #{
persistent = #{
"cluster.routing.allocation.disk.threshold_enabled" = $false
}
} | ConvertTo-Json
Invoke-WebRequest -Uri "http://localhost:9200/_cluster/settings" -Method Put -Body $body -ContentType "application/json"
Try the above code. Maybe remove the "http://"-part but I don't think so.
Hope this helps!

Powershell Post API Request

I'm able to post the request using bash with the following:
#!/bin/bash
APIKey="apikeyhere"
content="{\"accessToken\":\"$APIKey\",\"elements\":[{\"serialnumber\":\"AAAAAAAAA\",\"name\":\"EXAMPLENAME\",\"tags\":\"EXAMPLETAG\"}]}"
curl -s -k -X POST -d 'content='$content 'https://apiaccess.example.com/v2/devices'
I tried to use powershell but get an error "INVALID REQUEST":
$body = #{
"accessToken"="APIKeyhere"
"elements" = #{
"serialnumber"="AAAAA"
"name"="DeviceName"
"tags"="tag1,tag2"
}} | ConvertTo-Json
$header = #{
"Accept"="application/json"
"Content-Type"="application/json"
}
Invoke-RestMethod -Uri "https://apiaccess.example.com/v2/devices" -Method 'Post' -Body $body -Headers $header | ConvertTo-HTML
Any pointed regarding how I can fix the powershell script?
Don't do this: | ConvertTo-Json
Your headers can be replaced with this: -ContentType application/json
You should probably not convert the results to HTML until you've experimented with the data you get back. But that's up to you.
I did something similar once and had to translate a CURL request into Powershell. Maybe the following article will help you:
CURL to Powershell example
i got also the Error
"INVALID REQUEST":...
In my case, the API was weird. CURL made a simple fallback of a GET request to the POST method... it took hours to realize that I had to do a POST instead of a GET in Powershell.

What is the curl equivalent command in powershell for uploading the apk file?

I am trying to perform CI/CD using Perfecto and hence I am trying to upload a file to perfecto when my Bamboo build is finished.
I was trying with the following cURL command when we have a Linux server.
curl -X POST --upload-file test.apk 'https://****.perfectomobile.com/services/repositories/media/PRIVATE:test.apk?operation=upload&user=<email>&password=<password>&overwrite=true'
Now our server is changed to Windows and hence I want a powershell script which I can use as an Inline Scripts in Bamboo.
Can you please tell me what is an equivalent script in Powershell for windows.
Many thanks in advance.
# Gather your information.
$email = "myEmail#website.com";
$password = "powershellR0cks!";
$subDomain = "****";
$url = "https://$subDomain.perfectomobile.com/services/repositories/media/PRIVATE:test.apk?operation=upload&user=$email&password=$password&overwrite=true";
$filePath = ".\test.apk";
# Make the request.
$response = Invoke-WebRequest -Uri $URL -Method Post -InFile $filePath -ContentType "application/octet-stream";
# Check for success.
if (-not ($response.StatusCode -eq 200)) {
throw "There was an error uploading the APK manifest.";
}
You may want to check the value of -ContentType, but I think that's correct. You don't necessarily need to include the scheme (HTTPS) if you don't want to, and semicolons in PowerShell are optional, but you can include them if you want.
The $response variable is an HtmlWebResponseObject that has the content of the response, the status code, and a bunch of other useful info. You can check out the available properties and methods on the object by running $response | Get-Member.
Finally, the Invoke-WebRequest cmdlet also has other parameters that may be useful to you, such as -Credential, -Headers, and more.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-5.1
As a side-note, if you run Get-Alias -Name "curl", you can see that anytime you use curl in PowerShell, you're really just calling Invoke-WebRequest. You can use the curl alias if you want, but it's generally not a good idea to use aliases in automation since they can be modified or deleted.

How do I upload an attachment to a JIRA issue via powershell?

I have been searching online for a while and I've not found a solid answer to this (lots of partial answers, though). Still nothing I do works.
I'm trying to write a powershell script to send attachments to JIRA using cURL (have not found another way that I can get to work).
My cURL command is:
C:\opt\curl\curl.exe -u user:pa$$word -X POST -H "X-Atlassian-Token: nocheck" -F "file=#C:\opt\attachments\75391_testingPNG.png" http://jira.ourURL.com/rest/api/2/issue/75391/attachments
This works perfectly from the command line. Anytime I try to run it via powershell it bombs out. Seems like it should be very easy to do, though. Just want to grab the files from a directory and send them to JIRA.
Anyone have any thoughts about this??? Thanks!
I suspect that the characters $ and # in the arguments could be causing you problems (In case that is what you are using). Try escaping them using the backtick symbol.
To start curl.exe using the specified parameters, try the following command:
Start-Process C:\opt\curl\curl.exe -argumentList "-u", "user:pa`$`$Word", "-X", "POST", "-H", "`"X-Atlassian-Token: nocheck`"", "-F", "`"file=`#C:\opt\attachments\75391_testingPNG.png`"", "http://jira.ourURL.com/rest/api/2/issue/75391/attachments"
Basically it means that where you would separate arguments with a space in a command prompt, you would send each argument as an element in a powershell string Array and use it as the value in the -argumentlist parameter to Start-Process.
If you're using PowerShell 3+, you can use the native method Invoke-RestMethod to talk to JIRA, and not have to worry about escaping characters to shell out to a command:
# Build a basic auth header:
$headers = #{
'Authorization' = "Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $UserName, $Password))))"
'X-Atlassian-Token' = 'nocheck'
}
Invoke-RestMethod -Uri "http://jira.ourURL.com/rest/api/2/issue/75391/attachments" -Method Post -InFile "C:\opt\attachments\75391_testingPNG.png" -ContentType "multipart/form-data" -Headers $headers
I'm actually not sure what the nocheck header you're adding does though; I've not needed it when talking to JIRA over REST.