Translate curl commands into PowerShell
Solution 1
Here are a few ways to do so. 1. download curl.exe and point a variable to it then pass the command to it
$curlExe = "C:\download\curl-7.59.0\src\curl.exe"
& $curlExe -i -k -X POST -d '"{\"groupId\": xxxx}"' "https://site/site?access_token=zzzzzz" -s
Another would be
Invoke-RestMethod -uri https://site Get -Headers @{"x-api-key" = "mykey"} -ContentType "application/vnd.api+json" -Credential $adminCredential
I hope this help
Solution 2
I am not going to write the interpreter for you but it is most certainly possible...or you could just run curl.exe
from PowerShell (obviously, you'll need to install curl.exe
on the machine you are using to run the curl commands from PowerShell)..
Look into Invoke-WebRequest
or Invoke-RestMethod
- both of those functions exhibit curl-like-behavior.
Edit:
So, the curl
command in PowerShell is nothing more than an alias for Invoke-WebRequest
.. run this: Get-Alias -Definition Invoke-WebRequest
and review the output. Each parameter in Invoke-WebRequest
matches up to a curl
switch. All you have to do is review the curl
documentation, and match their params up to Invoke-WebRequest
params. A lot of what you're asking depends upon the API/site you are sending your HTTP request to (both curl
and Invoke-WebRequest
send HTTP requests).. For example, authenticating to an API/site.. if the site uses Basic Auth you would do something like this in PowerShell:
Invoke-WebRequest -Method Post -Uri "https://<ps_ip_address>:<port>/api/web/v1/categories/start" -Headers @{"Authorization" = "Basic %your_encoded_credentials_here%" }
.. The curl -X
switch represents the HTTP method.. the Invoke-WebRequest -Method
parameter serves the same purpose.. It sounds like you really just need to read up on Invoke-WebRequest
or Invoke-RestMethod
- the only difference in the two is the return they give you.
I think your confusion on how to differentiate, and ultimately interpret, curl
to Invoke-WebRequest
, has to do with the fact that you seem to be fresh into HTTP requests. Both of those commands send HTTP Requests - they just use different switches - if you learn how each command handles HTTP requests, you can interpret from curl
to Invoke-WebRequest
..
More here: Invoke-WebRequest and Invoke-RestMethod
There are also a TON of examples on how to interpret curl
to Invoke-WebRequest
/Powershell
on this site alone.. you just need to brush up on HTTP requests and how curl
handles them and the differences in how Invoke-WebRequest
handles them.
Aaron Martin
Updated on June 04, 2022Comments
-
Aaron Martin almost 2 years
Does anyone have experience with using the Websense API through PowerShell? I need to translate the curl commands (which I have working) into Powershell so they can be scripted. Anyone have any resources that would be useful? Examples of curl commands that I need to translate are below:
Start a new transaction:
curl -k -u <username>:<password> -X POST https://<ps_ip_address>:<port>/api/web/v1/categories/start
Add an API-managed category (all data in the HTTPS request):
curl -k -u <username>:<password> -X POST https://<ps_ip_address>:<port>/api/web/v1/categories -d "{\"Transaction ID\":\"<transaction_ID_string>\",\"Categories\": [ {\"Category Name\": \"<name_string>\",\"Category Description\":\"<description_string>\",\"Parent\": <numeric_category_ID>}]}"
Add a URL (all data in the HTTPS request):
curl -k -u <username>:<password> -X POST https://<ps_ip_address>:<port>/api/web/v1/categories/urls -d "{\"Transaction ID\": \"<transaction_ID_string>\",\"Category ID\": <numeric_ID>,\"URLs\":[\"https://www.new_url.com/\"]}" --cacert PolApiServer.crt
Commit the transaction:
curl -k -u <username>:<password> -X POST https://<ps_ip_address>:<port>/api/web/v1/categories/commit?TransactionID="<id_string>"
-
Aaron Martin over 5 yearsThis is helpful. Translating the commands successfully into that format is my stumbling block presently. I may need to update my question with current approach using the Invoke-RestMethod. I am doing more testing and will either mark as answer or update my question with more detail. Thanks.
-
Bacon Bits about 3 yearsThe JSON in your second example doesn't have correctly escaped double quotes for Powershell.