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!
Related
I have a working cURL statement that takes the contents of a .csv file and posts them to a REST endpoint:
curl \
'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys' \
-H 'Authorization: apikey <APIKEY>' \
-F 'csv_file=#<FILENAME>.csv'
I am trying to convert it to PowerShell format, but can't seem to get there.
I am fairly new to APIs, so I've been using the Curl2PS tool for translating cURL commands to PowerShell, and it worked fairly well with other types, but it struggles with the -F (--form) parameter. This is what I get when I input the above cURL statement:
WARNING: The parameter '-F' is not yet supported. Please refer to curl man pages: https://curl.se/docs/manpage.html
Invoke-RestMethod -Method Get -Uri 'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys' -Verbose:$false -Headers #{
'Authorization' = 'apikey <APIKEY>'
}
It basically only does a partial job at converting the statement and skips the -F part completely, leaving it blank.
After some googling I tried using the -Infile method, but I probably did it wrong (again, noob here):
Invoke-RestMethod -Method POST -Uri 'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys'-Verbose -Headers #{
'Authorization' = 'apikey <APIKEY>'
'Content-Type' = 'multipart/form-data'
} -Infile '<FILENAME>.csv'
Unfortunately, I get a Bad Request (400) error.
I tried googling for a solution, but couldn't really find one, so any input here would be much appreciated.
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.
I'm trying to post to a Wazuh server using the following guidance:
https://documentation.wazuh.com/3.x/user-manual/api/reference.html#add-agent
This is the CURL equivalent:
curl -u foo:bar -k -X POST -d '{"name":"NewHost","ip":"10.0.0.9"}' -H 'Content-Type:application/json' "https://127.0.0.1:55000/agents?pretty"
This is what I've come up using Powershell:
Invoke-WebRequest -Body "{name:'newhost',ip:'10.0.0.9'}" -Uri "http://$($WAZUHSERVER):55000/agents?pretty" -Method Post -Credential $MyCredential -ContentType 'application/json'
I keep getting "Invalid Request Body".
I tried alternating with a hash table. No dice. I should mention that I dont have SSL configured so that part is irrelevant.
Found it.
'{"name":"newhost","ip":"10.0.0.0"}'
Using this one as a body worked out perfectly.
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
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.