将curl命令转换为PowerShell [英] Translate curl commands into PowerShell
问题描述
任何人都具有通过PowerShell使用Websense API的经验吗?我需要将curl命令(我正在使用的命令)转换为Powershell,以便可以编写脚本.任何人都有任何有用的资源吗?我需要翻译的curl命令示例如下:
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:
开始新交易:
curl -k -u <username>:<password> -X POST https://<ps_ip_address>:<port>/api/web/v1/categories/start
添加API管理的类别(HTTPS请求中的所有数据):
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>}]}"
添加URL(HTTPS请求中的所有数据):
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
提交交易:
curl -k -u <username>:<password> -X POST https://<ps_ip_address>:<port>/api/web/v1/categories/commit?TransactionID="<id_string>"
推荐答案
我不会为您编写解释器,但是肯定有可能...或者您可以运行 curl.exe
从PowerShell(显然,您需要在用于从PowerShell运行curl命令的计算机上安装 curl.exe
).
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)..
查看 Invoke-WebRequest
或 Invoke-RestMethod
-这两个函数都表现出类似curl的行为.
Look into Invoke-WebRequest
or Invoke-RestMethod
- both of those functions exhibit curl-like-behavior.
因此,PowerShell中的 curl
命令只不过是 Invoke-WebRequest
的别名.运行此命令: Get-Alias -Definition Invoke-WebRequest
并查看输出. Invoke-WebRequest
中的每个参数最多匹配一个 curl
开关.您所需要做的就是查看 curl
文档,并将其参数与 Invoke-WebRequest
参数进行匹配.您要问的很多内容取决于您向其发送HTTP请求的API/站点( curl
和 Invoke-WebRequest
发送HTTP请求).例如,向API/网站进行身份验证.如果该网站使用基本身份验证,则可以在PowerShell中执行以下操作:
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-方法发布-Uri"https://< ps_ip_address>:< port>/api/web/v1/categories/start"-标头@ {授权" ="基本%your_encoded_credentials_here%}
.. curl -X
开关表示HTTP方法.. Invoke-WebRequest -Method
参数起着相同的作用.来阅读 Invoke-WebRequest
或 Invoke-RestMethod
-两者的唯一区别是它们给您的回报.
.. 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.
我认为您对如何区分并最终解释 curl
到 Invoke-WebRequest
感到困惑,这与您似乎对HTTP似乎很熟悉有关.要求.这两个命令都发送HTTP请求-它们只是使用不同的开关-如果您了解每个命令如何处理HTTP请求,则可以将 curl
解释为 Invoke-WebRequest
..
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
..
更多信息在这里:调用RestMethod
仅在此站点上,还存在大量有关如何将 curl
解释为 Invoke-WebRequest
/ Powershell
的示例.需要重新了解HTTP请求以及 curl
如何处理它们以及 Invoke-WebRequest
处理它们的方式上的差异.
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.
这篇关于将curl命令转换为PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!