「终端引力」——把 Windows 命令行里的每一个命令,变成你的超能力
2026/8/31 8:57:01 网站建设 项目流程

Windows 上的irm终端命令详细介绍

irm是 Windows PowerShell 5.0+ 和 PowerShell Core 中Invoke-RestMethod命令的内置别名。它是一个功能强大的命令行工具,用于向 RESTful API 发送 HTTP/HTTPS 请求并直接解析返回的数据。近年来,随着 Windows 生态中包管理器(如 Winget、Chocolatey)和远程脚本部署的普及,irm因其“一行命令下载并执行脚本”的便捷性而频繁出现在各类官方安装文档中,成为 Windows 高级用户和开发者的必备技能。


1.irm是什么?

从本质上看:

irm=Invoke-RestMethod= PowerShell 版的curl(但更侧重于结构化数据处理)

属性说明
全称Invoke-RestMethod
别名irm
所属环境Windows PowerShell 5.0+ / PowerShell 7+(跨平台)
核心功能发送 HTTP/HTTPS 请求,自动解析 JSON/XML 响应为对象
典型用途调用 REST API、下载文件、远程执行脚本、与 Web 服务交互

Invoke-WebRequest(别名iwr)的区别

对比项irm(Invoke-RestMethod)iwr(Invoke-WebRequest)
返回内容自动解析后的数据对象(PSCustomObject)完整的HTTP 响应对象(含 Headers、StatusCode、Content 等)
JSON 处理自动将 JSON 转为 PowerShell 对象需要手动Content | ConvertFrom-Json
适用场景调用 API、获取结构化数据需要检查响应头、状态码、下载原始内容
使用复杂度更简洁,直接可用更底层,需手动解析

简单记忆:调 API 拿数据用irm,看完整 HTTP 细节用iwr


2. 基本语法

irm [-Uri] <String> [-Method <String>] [-Headers <Hashtable>] [-Body <Object>] [-ContentType <String>] [-OutFile <String>] [-TimeoutSec <Int32>] [-Proxy <Uri>] [-Credential <PSCredential>] [其他参数]

最简用法

# GET 请求,自动解析 JSON irm https://api.github.com/users/microsoft

输出示例:

login : microsoft id : 6154722 type : Organization name : Microsoft blog : https://www.microsoft.com location : Redmond, WA followers : 10000

可以看到,返回的 JSON 已被自动转换为 PowerShell 对象,可以直接通过.访问属性。


3. 核心参数详解

3.1-Uri(位置参数,可省略)

指定请求的目标 URL。支持 HTTP 和 HTTPS。

irm https://api.example.com/data

3.2-Method

指定 HTTP 方法,可选值:GET(默认)、POSTPUTDELETEPATCHHEADOPTIONS等。

# POST 请求 irm -Uri https://api.example.com/users -Method Post -Body $jsonBody

3.3-Headers

以哈希表形式设置请求头,常用于传递 API Key、Token、User-Agent 等。

$headers = @{ "Authorization" = "Bearer $token" "Accept" = "application/json" } irm -Uri https://api.example.com/secure -Headers $headers

3.4-Body

请求体,用于 POST/PUT/PATCH 请求。可接受字符串、哈希表、PSObject 等。

# 方式一:哈希表自动序列化 $body = @{ name = "Alice"; age = 30 } irm -Uri https://api.example.com/users -Method Post -Body $body -ContentType "application/json" # 方式二:手动构造 JSON 字符串 $jsonBody = '{"name":"Alice","age":30}' irm -Uri https://api.example.com/users -Method Post -Body $jsonBody -ContentType "application/json"

3.5-ContentType

指定请求体的 MIME 类型,常见值:application/jsonapplication/xmlapplication/x-www-form-urlencoded

3.6-OutFile

将响应内容直接保存到文件(类似于下载)。

irm -Uri https://example.com/file.zip -OutFile C:\Downloads\file.zip

3.7-TimeoutSec

设置请求超时时间(秒),默认 100 秒。

irm -Uri https://slow-api.example.com -TimeoutSec 30

3.8-Proxy-ProxyCredential

通过代理服务器发送请求。

irm -Uri https://api.example.com -Proxy http://proxy.company.com:8080

3.9-SkipCertificateCheck(PowerShell 7+)

跳过 SSL 证书验证(仅用于测试环境,不推荐生产使用)。

irm -Uri https://self-signed.example.com -SkipCertificateCheck

4. 与管道符| iex的组合:一行命令执行远程脚本

这是irm在 Windows 上最广为人知的用法,也是各大开发工具官方安装文档中的常见模式:

irm https://get.example.com/install.ps1 | iex

4.1 执行原理

  1. irm https://...向远程服务器发送 HTTPS 请求,获取脚本文件内容。

  2. 脚本内容作为字符串通过管道传递给iexInvoke-Expression的别名)。

  3. iex将字符串作为 PowerShell 代码在当前会话中执行。

4.2 实际应用案例

工具安装命令
Chocolatey(软件包管理器)Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Scoop(便携软件管理器)irm get.scoop.sh | iex
Oh My Posh(终端美化)irm https://ohmyposh.dev/install.ps1 | iex
Rust(Rustup 安装器)irm https://sh.rustup.rs | iex(需安装 Rust 后)
Winget UI 等第三方工具各类开源项目常提供类似一键安装脚本

4.3 安全风险与最佳实践

⚠️重要警告:irm ... | iex相当于“从互联网下载代码并立即执行”,存在重大安全风险。

潜在风险:

  • 远程服务器被劫持,脚本被篡改后植入恶意代码

  • 中间人攻击(MITM)替换脚本内容

  • 脚本本身存在漏洞或后门

安全建议:

  1. 始终检查 URL 来源:确认是官方域名。

  2. 在执行前先查看脚本内容

    irm https://example.com/install.ps1 # 只看内容,不执行
  3. 保存后审查再执行

    irm https://example.com/install.ps1 -OutFile install.ps1 Get-Content install.ps1 # 人工审查 .\install.ps1 # 确认安全后执行
  4. 使用 HTTPS:确保加密传输,防止中间人篡改。

  5. 注意执行策略:PowerShell 默认禁止运行脚本,许多安装命令会先修改ExecutionPolicy,需了解其影响。


5. 实际应用场景

5.1 调用 REST API 获取数据

# 获取 GitHub 用户信息 $user = irm https://api.github.com/users/octocat $user.name $user.followers # 获取天气数据 $weather = irm "https://api.openweathermap.org/data/2.5/weather?q=Beijing&appid=YOUR_KEY" $weather.main.temp $weather.weather[0].description

5.2 批量下载文件

$urls = @( "https://example.com/file1.zip", "https://example.com/file2.zip" ) $urls | ForEach-Object { $filename = $_ -split '/' | Select-Object -Last 1 irm -Uri $_ -OutFile "C:\Downloads\$filename" }

5.3 与 Webhook 交互

# 发送消息到 Slack $body = @{ text = "部署完成!" } | ConvertTo-Json irm -Uri https://hooks.slack.com/services/xxx -Method Post -Body $body -ContentType "application/json"

5.4 检测 API 可用性

try { $response = irm -Uri https://api.example.com/health -TimeoutSec 10 if ($response.status -eq "ok") { Write-Host "API 正常" -ForegroundColor Green } } catch { Write-Host "API 不可用: $($_.Exception.Message)" -ForegroundColor Red }

6. 与传统工具对比

工具特点适用场景
irm(PowerShell)自动解析 JSON/XML,与 PS 对象无缝集成,支持管道PowerShell 脚本、自动化运维、API 交互
curl(Windows 10+ 内置)跨平台经典工具,输出原始数据快速调试、非 PowerShell 环境
wget专注下载,支持断点续传、递归下载大规模文件下载
Invoke-WebRequestiwr完整 HTTP 响应,可访问 Headers/Cookies需要检查 HTTP 细节、会话管理

选择建议:

  • 在 PowerShell 脚本中处理 API 数据 → 用irm

  • 需要在 CMD/Bash 中跨平台使用 → 用curl

  • 只需下载大文件 → 用wgetirm -OutFile


7. 常见错误与排查

7.1 错误:The response content cannot be parsed because the content type is not valid

原因:服务器返回的不是合法的 JSON/XML,或者 Content-Type 头不正确。
解决:改用iwr查看原始响应,或添加-ContentType参数。

7.2 错误:Unable to connect to the remote server

原因:网络不通、DNS 解析失败、防火墙拦截。
解决:检查网络连接、尝试Test-NetConnection诊断。

7.3 错误:The remote server returned an error: (403) Forbidden

原因:缺少认证信息或 API Key 无效。
解决:检查-Headers中的认证信息。

7.4 错误:The underlying connection was closed: Could not establish trust relationship

原因:SSL 证书验证失败(自签名证书或证书过期)。
解决:PowerShell 7+ 可使用-SkipCertificateCheck;Windows PowerShell 5 需通过 .NET 设置。

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

7.5 中文乱码

原因:服务器返回的编码不是 UTF-8。
解决

$response = iwr -Uri https://example.com -UseBasicParsing $response.Content # 原始字符串,可手动处理编码

8. Windows PowerShell 5 与 PowerShell 7 的差异

功能Windows PowerShell 5.1PowerShell 7+
irm可用性✅ 支持✅ 支持
-SkipCertificateCheck❌ 不支持✅ 支持
默认 TLS 版本1.0/1.1(需手动升级)1.2/1.3
跨平台❌ 仅 Windows✅ Win/Mac/Linux
性能较慢(.NET Framework)更快(.NET Core/5+)

建议:优先安装并使用PowerShell 7+,获得更好的irm体验和安全性。


9. 总结

irmInvoke-RestMethod)是 Windows PowerShell 中一个轻量但强大的 HTTP 客户端命令。它的核心优势在于:

  1. 自动解析:JSON/XML 响应直接转为 PowerShell 对象,省去手动反序列化。

  2. 管道友好:可与| iex组合实现一键安装,也可与其他 cmdlet 无缝衔接。

  3. 原生集成:无需安装额外软件,Windows 内置即用。

  4. 安全可控:支持 HTTPS、Headers、认证等完整 HTTP 功能。

使用金句总结:

irm是 PowerShell 世界里“最会聊天的人”——它知道怎么向 REST API 提问(发送请求),也听得懂对方的回答(自动解析 JSON/XML),还能把聊天内容直接转交给下一个伙伴(管道传递)。无论是调用 API、下载文件,还是官方文档里常见的irm ... | iex一键安装,它都是 Windows 自动化脚本中的关键角色。用好irm,你的 Windows 终端就多了一把瑞士军刀。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询