2011-10-10 11:19:21 +0000 2011-10-10 11:19:21 +0000
155
155

PowerShell の curl と同等の

PowerShell には curl と同等のものはありますか?何か似たような組み込み機能を持っているのか、それともサードパーティのコマンドレットがあるのか?

回答 (8)

104
104
104
2013-05-04 00:18:17 +0000

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/

46
46
46
2015-10-30 23:21:20 +0000

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

アンエイリアスコマンドを使用するには …

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

次のようにリクエストのいくつかのプロパティを返します。またはちょうど内容…

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

等価なaliasedコマンドは…

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

<!doctype html><html itemscope="" itemtype="http://schem[etc ...]
PS> curl -Uri https://www.google.com
PS> curl -Uri https://www.google.com | Select-Object -ExpandProperty Content

…にコマンドを短くすることができるLeveraging Powershellのデフォルトおよび他のaliasesを…しかし、私はそれを推薦しません。冗長なコマンドは、あなたのコードを読むときに他の人の助けになります。

更新:

Powershell 6.x

エイリアスの使用は推奨されません

[ Powershell 6.x “Core” ]の時点では、(https://github.com/PowerShell/PowerShell) curlInvoke-WebRequest のエイリアスではなくなりました (エイリアスの wget も削除されています)。

PS> curl https://www.google.com 
ps> curl https://www.google.com | Select -ExpandProperty Content

Curl は Invoke-WebRequest (Powershell 6.2.3 でテスト済み) のエイリアスではなくなりました (RFC “to remove the aliases curl and wget from the Windows PowerShell” ](https://github.com/PowerShell/PowerShell-RFC/blob/master/X-Rejected/RFC0007-Weak-Aliases.md#powershell-committee-decision) のモーションが拒否されたにもかかわらず。

そのRFCは「wget/curlのエイリアスはすでにPowerShell Coreから削除されていたので、[それらのエイリアスを持つことの問題]はWindows PowerShellに限定されていた」と指摘しています。

@v6ak さんがコメントで指摘されているように、PowerShell (5.0 以下) で Invoke-WebRequestcurl を使用すると、次のような問題が発生する可能性があります: サイドバイサイドでインストールされた場合、意図せずに本物の curl や wget を起動してしまいます。新しいエンコーディング

新しいエンコーディング

を使用する際には、デフォルトのエンコーディング wget を利用するために、Powershell の “core” (6.x 以上) をアップグレードすることをお勧めします。しかし、より短い暗黙のコマンド …

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

CommandType Name Version Source
----------- ---- ------- ------
Alias iwr -> Invoke-WebRequest

… を使う場合でも、utf8NoBOM でエンコーディングが行われます (これは、例えば、Visual Studio Code で保存されたファイルを開いてステータスバーに “UTF-8” と表示されていることで確認できます)。もちろん、他のエンコーディングが必要な場合は、明示的に代替エンコーディングを設定することができます。

Powershell 5.0 以下では、デフォルトはおろか、007 エンコーディングは利用できませんでした。 PowerShell.Utility, Out-File, Parameters, -Encoding ](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-6#parameters) - Powershell 6, What’s new, What’s new in Powershell Core 6.x, Breaking changes in Powershell Core 6.0, “Change $OutputEncoding to use UTF-8 NoBOM encoding instead ASCII #5369”

29
29
29
2011-10-10 11:26:27 +0000

優れたコマンドラインカンフーブログは、彼らがcurl、wgetと関連するPowerShellコマンド

を比較する記事を持っています 一言で言えば:

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

または、Powershell/.Netのあなたのバージョンは、DownloadStringのための2つのパラメータを受け入れない場合:

(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html") > "C:\hello-world.html"
15
15
15
2013-04-04 01:20:37 +0000

また、 Git for Windows をインストールして、パスにGitのbinフォルダを入れます。Git のインストールには、とりわけ curl.exe が含まれています。インストールしたら、%programfiles(x86)%\git\binをPATHに置くだけです。そうすれば、Windows のコマンドプロンプトや PowerShell コンソールから curl コマンドが使えるようになります。

14
14
14
2013-01-22 15:07:59 +0000

ChocolateyでcURLをインストール](http://chocolatey.org/packages/curl)で、PowerShell CLIやcmdでcurlが利用できるようになります。

1
1
1
2019-06-04 03:10:31 +0000

curl

cmd, bash

curl -H "Host: whoami.local" 192.168.4.4

powershell

Invoke-WebRequest -H @{"Host"="whoami.local"} -UseBasicParsing 192.168.4.4

# or 
# $(Get-Command curl) return "curl -> Invoke-WebRequest"
curl -H @{"Host"="whoami.local"} -UseBasicParsing 192.168.4.4
1
1
1
2011-12-11 16:38:28 +0000

windows上のwgetcurlに最も近いものは、 bits (Background Intelligent Transfer Service)であり、これには powershell用のスニペットがいくつか用意されています。

0
0
0
2018-02-13 12:01:17 +0000

このコマンドは PowerShell 3.0 以降の Microsoft.PowerShell.Utility に含まれています。Powershellでテキストファイルに書き込むために$webclient.downloadstringを取得する ](https://stackoverflow.com/q/9365000/55075)も参照してください。

関連する質問

7
16
19
2
8