[英]Translate curl commands into PowerShell
有沒有人有通過 PowerShell 使用 Websense API 的經驗? 我需要將 curl 命令(我正在使用)轉換為 Powershell,以便編寫腳本。 任何人都有任何有用的資源? 我需要翻譯的 curl 命令示例如下:
開始一個新的交易:
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>"
我不會為你編寫解釋器,但它肯定是可能的......或者你可以從 PowerShell 運行curl.exe
(顯然,你需要在你用來運行的機器上安裝curl.exe
來自 PowerShell 的 curl 命令)。
查看Invoke-WebRequest
或Invoke-RestMethod
- 這兩個函數都表現出類似 curl 的行為。
編輯:
因此,PowerShell 中的curl
命令只不過是Invoke-WebRequest
的別名。運行以下命令: Get-Alias -Definition Invoke-WebRequest
並查看輸出。 Invoke-WebRequest
中的每個參數都匹配一個curl
開關。 您所要做的就是查看curl
文檔,並將它們的參數與Invoke-WebRequest
參數匹配。 您要問的很多問題取決於您將 HTTP 請求發送到的 API/站點( curl
和Invoke-WebRequest
發送 HTTP 請求)。例如,對 API/站點進行身份驗證。如果站點使用 Basic Auth 你會在 PowerShell 中做這樣的事情:
Invoke-WebRequest -Method Post -Uri "https://<ps_ip_address>:<port>/api/web/v1/categories/start" -Headers @{"Authorization" = "Basic %your_encoded_credentials_here%" }
.. curl -X
開關代表 HTTP 方法.. Invoke-WebRequest -Method
參數用於相同的目的.. 聽起來你真的只需要閱讀Invoke-WebRequest
或Invoke-RestMethod
- 唯一的區別二是他們給你的回報。
我認為您對如何區分並最終解釋curl
到Invoke-WebRequest
的困惑與您似乎對 HTTP 請求不熟悉這一事實有關。 這兩個命令都發送 HTTP 請求 - 它們只是使用不同的開關 - 如果您了解每個命令如何處理 HTTP 請求,您可以將curl
解釋為Invoke-WebRequest
..
更多信息: Invoke-WebRequest和Invoke-RestMethod
還有很多關於如何將curl
解釋為Invoke-WebRequest
/ Powershell
僅在這個站點上的示例..您只需要復習 HTTP 請求以及curl
如何處理它們以及Invoke-WebRequest
如何處理它們的差異。
這里有幾種方法可以做到這一點。 1. 下載 curl.exe 並將變量指向它然后將命令傳遞給它
$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
另一個是
Invoke-RestMethod -uri https://site Get -Headers @{"x-api-key" = "mykey"} -ContentType "application/vnd.api+json" -Credential $adminCredential
我希望這有幫助
您可以使用splatting將其轉換為 PowerShell 語法。
PS> curl.exe '-k',
'-u', '<username>:<password>',
'-X', 'POST ',
'https://<ps_ip_address>:<port>/api/web/v1/categories/start'
PS> curl.exe '-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>}]}"
PS> curl.exe '-k',
'-u', '<username>:<password>',
'-X', 'POST ',
'https://<ps_ip_address>:<port>/api/web/v1/categories/commit?TransactionID="<id_string>"'
此命令將從 Windows 操作系統調用 curl.exe。 要知道這個文件在哪里運行
PS> Get-Command curl.exe
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.