PowerShell で exe ファイルを実行する方法
Windows オペレーティングシステムは、自動化に非常に役立ついくつかのコマンドラインツールをサポートしています。
curl は、HTTP、HTTPS、FTP、FTPS、SMTP などのサポートされているプロトコルを介してサーバーにリクエストを送信するために使用できる、これらの便利なツールの一つです。
このコマンドラインツールは、FTP アップロード、プロキシサポート、転送の再開、制限された帯域幅などの追加機能をサポートしています。
Windows の公式ビルド 1804 から、curl はツールチェーンに追加されました。これを確認するには、Windows コマンドプロンプトを開き、次のコマンドを実行します。
curl --version
出力:
curl 7.55.1 (windows)
出力は、あなたの curl インストールに基づいて変わる可能性があります。
Windows PowerShell における curl
Windows PowerShell では、curl コマンドを Windows コマンドプロンプトとは少し異なる方法で使用する必要があります。なぜなら、curl コマンドは Invoke-WebRequest コマンドレットへのエイリアスとしてマッピングされているからです。これを確認するには、PowerShell ウィンドウで次のコマンドを実行します。
Get-Alias -Name curl
出力:
CommandType Name Version Source
----------- ---- ------- ------
Alias curl -> Invoke-WebRequest
これらのコマンドはすべて、PowerShell コマンド実行プロセスで解決されます。通常、エイリアスが最も優先されます。したがって、PowerShell では curl の代わりに curl.exe 実行可能ファイルを直接使用する必要があります。これらの 2つのコマンドがランタイムでどのように解決されるかを知るには、Get-Command コマンドレットを使用できます。
Get-Command curl
出力:
CommandType Name Version Source
----------- ---- ------- ------
Alias curl -> Invoke-WebRequest
Get-Command curl.exe
出力:
CommandType Name Version Source
----------- ---- ------- ------
Application curl.exe 7.55.1.0 C:\Windows\system32\curl.exe
結論として、PowerShell で curl(Windows コマンドプロンプトと同じように)を使用する必要がある場合は、curl 実行可能ファイル(curl.exe)を直接呼び出す必要があります。それ以外の場合、内部的に Invoke-WebRequest コマンドレットに解決される PowerShell の curl エイリアスを使用するべきです。
PowerShell における curl 構文
curl.exe [options] <url>
curl コマンドおよび -a、-C などのオプションに関する詳細情報を取得するには、次のコマンドを実行できます。
curl.exe --help
出力:
Usage: curl [options...] <url>
-a, --append Append to target file when uploading
.
.
.
-C, --continue-at <offset> Resumed transfer offest
例のシナリオ
HTML ウェブページをレスポンスとして表示します。
curl.exe https://www.google.com
レスポンス、リクエストヘッダー、レスポンスヘッダーを表示します。
curl.exe -v https://www.google.com
ヘッダー情報を表示します。
curl.exe -I https://www.google.com
ヘッダー情報を file1.txt というファイルに保存します。
curl.exe -I https://www.google.com -o file1.txt
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.
