相当于 curl 的 PowerShell

Bor*_*ard 193 windows powershell curl

curl在 PowerShell 中是否有等价物?它有一些类似的内置功能,还是有3聚会cmdlet的?

小智 130

PowerShell 3.0 有新命令Invoke-RestMethod

http://technet.microsoft.com/en-us/library/hh849971.aspx

更多详情:

https://discoposse.com/2012/06/30/powershell-invoke-restmethod-putting-the-curl-in-your-shell/

  • 现在它在 Powershell 中也被称为 `curl` 或 `wget`。 (40认同)
  • 是的,他们给它们取别名很奇怪,因为语法完全不同。如果 MS 不想发布包管理器并使其易于获得通用的基本工具,则将其隐藏在假别名后不会使情况好转。 (37认同)
  • 您可能需要 Invoke-WebRequest 命令,具体取决于您要完成的任务。 (9认同)
  • Invoke-WebRequest 最大的问题是,如果 IE 没有“初始化”,它就不起作用;因此,在编写管道或 DSC 脚本时,您必须先做一些额外的跑腿工作,或者您可以使用 Invoke-RestMethod。 (3认同)

Joh*_*ley 72

从 Powershell 5.0 开始,如果不是之前的话,curlInvoke-WebRequest.

PS> Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize

CommandType Name                      Version Source
----------- ----                      ------- ------
Alias       curl -> Invoke-WebRequest
Alias       iwr -> Invoke-WebRequest
Alias       wget -> Invoke-WebRequest
Run Code Online (Sandbox Code Playgroud)

要使用无别名命令...

PS> Invoke-WebRequest -Uri https://localhost:443/
PS> Invoke-WebRequest -Uri https://www.google.com
Run Code Online (Sandbox Code Playgroud)

所以返回请求的几个属性如下...

PS> Invoke-WebRequest -Uri https://www.google.com

StatusCode        : 200
StatusDescription : OK
Content           : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-AU"><head><meta content="text/html; charset=UTF-8"
                    http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/...
RawContent        : HTTP/1.1 200 OK
                    X-XSS-Protection: 1; mode=block
                    X-Frame-Options: SAMEORIGIN
                    Vary: Accept-Encoding
Run Code Online (Sandbox Code Playgroud)

......或者只是内容......

PS> Invoke-WebRequest -Uri https://www.google.com | Select-Object -ExpandProperty Content

<!doctype html><html itemscope="" itemtype="http://schem[etc ...]
Run Code Online (Sandbox Code Playgroud)

等效的别名命令是...

PS> curl -Uri https://www.google.com
PS> curl -Uri https://www.google.com | Select-Object -ExpandProperty Content
Run Code Online (Sandbox Code Playgroud)

利用 Powershell 默认值和其他别名,您可以将命令缩短为

PS> curl https://www.google.com 
ps> curl https://www.google.com | Select -ExpandProperty Content
Run Code Online (Sandbox Code Playgroud)

......但我不会推荐它。详细命令可以帮助其他人阅读您的代码。

更新:

Powershell 6.x

不鼓励使用别名

Powershell 6.x 开始,“Core” curl不再是 的别名Invoke-WebRequest(别名wget也被删除)。而是Invoke-WebRequest直接使用。

PS> Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize

CommandType Name                     Version Source
----------- ----                     ------- ------
Alias       iwr -> Invoke-WebRequest
Run Code Online (Sandbox Code Playgroud)

Curl 不再是 Invoke-WebRequest 的别名(在 Powershell 6.2.3 上测试),尽管在RFC 中明显拒绝了“从 Windows PowerShell 中删除别名 curl 和 wget”的动议。

该 RFC 指出“wget/curl 别名已经从 PowerShell Core 中删除,因此[拥有这些别名的] 问题仅限于 Windows PowerShell。”

最后,Powershell 团队还鼓励用户“不要依赖脚本中的别名”。

正如@v6ak 在评论中所指出的curlwget在 PowerShell(5.0 或更低版本)中使用和可能存在以下问题:如果并排安装,会无意调用真正的 curl 或 wget;并且在任何情况下都会引起混乱。

新编码

建议您升级 Powershell“核心”(6.x 或更高版本),以便utf8NoBOM在使用Invoke-WebRequest(以及许多其他文本输出命令)时利用默认编码。如果有人明确这样做,您可以执行以下操作:

Invoke-WebRequest ` 
-Uri https://raw.githubusercontent.com/fancyapps/fancybox/master/dist/jquery.fancybox.min.js `
| Select-Object -ExpandProperty Content `
| Out-File jquery.fancybox.min.js `
-Encoding utf8NoBOM 
Run Code Online (Sandbox Code Playgroud)

但是,即使使用更短的、隐式的命令......

Invoke-WebRequest `
-Uri https://raw.githubusercontent.com/fancyapps/fancybox/master/dist/jquery.fancybox.min.js `
-OutFile jquery.fancybox.min.js
Run Code Online (Sandbox Code Playgroud)

...utf8NoBOM将完成编码(您可以验证这一点,例如,通过在 Visual Studio Code 中打开保存的文件并在状态栏中观察“UTF-8”)。

utf8NoBOM在各种生态系统中旅行时,保存的文件往往会导致较少的问题。当然,如果您需要一些其他编码,您可以显式设置一些替代方案。

在 Powershell 5.0 及更低版本中,utf8NoBOM编码不可用,更不用说默认了。

细节:

  • 在 powershell 中,某些参数名称可以从参数值的顺序隐式派生。您可以通过查看“get-help curl”来了解这一点。您将看到“Invoke-WebRequest [-Uri] &lt;Uri&gt; ...”,其中括号“[]”表示可以省略“-Uri”(因此隐式调用)。正如我在主帖中提到的:一般来说,您应该是明确的(您或其他人将来将阅读您的代码)。 (2认同)

ipr*_*101 31

优秀的命令行功夫博客有一篇文章比较了 curl、wget 和相关的 PowerShell 命令

简而言之:

(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html","C:\hello-world.html")
Run Code Online (Sandbox Code Playgroud)

或者,如果您的 Powershell/.Net 版本不接受以下 2 个参数DownloadString

(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html") > "C:\hello-world.html"
Run Code Online (Sandbox Code Playgroud)


小智 15

您也可以安装Git for Windows,然后将 Git bin 文件夹放在您的路径中。Git 安装包括 curl.exe 等。安装后,只需放入%programfiles(x86)%\git\bin您的PATH。然后,您将能够从 Windows 命令提示符或 PowerShell 控制台使用 curl 命令。


Spo*_*ike 14

您可以使用 Chocolatey 安装 cURL,并在 PowerShell CLI 或cmd.


归档时间:

查看次数:

451881 次

最近记录:

5 年,2 月 前