Skip to content

fix: Windows PowerShell 5.1 compatibility for install.ps1#108

Merged
largeoliu merged 2 commits into
masterfrom
fix/install-ps1-compatibility
May 10, 2026
Merged

fix: Windows PowerShell 5.1 compatibility for install.ps1#108
largeoliu merged 2 commits into
masterfrom
fix/install-ps1-compatibility

Conversation

@largeoliu
Copy link
Copy Markdown
Owner

问题描述

在 Windows PowerShell 5.1 环境下执行 irm https://raw.githubusercontent.com/largeoliu/redmine-cli/master/scripts/install.ps1 | iex 时,安装脚本的校验和验证功能完全失效(静默跳过),且在管道模式下 Read-Host 可能导致脚本挂起。

根本原因

Bug 1: Invoke-WebRequest 返回 byte[] 而非 string(核心问题)

Windows PowerShell 5.1 中,Invoke-WebRequesttext/plain 类型响应的 .Content 属性返回 System.Byte[](字节数组),而非字符串。

原代码:

$checksumsContent = Invoke-WebRequest -Uri $checksumsUrl -UseBasicParsing | Select-Object -ExpandProperty Content
$expectedHash = ($checksumsContent -split "`n" | Where-Object { ... })

-split 作用于 byte[] 时,PowerShell 将每个字节作为独立元素,导致正则匹配完全失败,校验和验证被静默跳过。

Bug 2: Read-Hostirm | iex 管道中不可靠

irm | iex 管道模式下 stdin 被占用,Read-Host 无法正确读取用户输入,可能导致脚本挂起或异常退出。

Bug 3: Get-Arch 缺少回退方案

[System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture 在 .NET Framework 4.7.1 之前的版本中不可用,会导致脚本在旧版 Windows 上直接崩溃。

解决方案

  1. 新增 Get-WebContent 辅助函数:检测 Invoke-WebRequest 返回的 Content 是否为 byte[],若是则用 [System.Text.Encoding]::UTF8.GetString() 解码为字符串
  2. 规范化 CRLF:在分割前统一移除 \r 字符
  3. Get-Arch 增加回退:当 RuntimeInformation.OSArchitecture 不可用时,使用 $env:PROCESSOR_ARCHITECTURE 作为回退
  4. 管道输入检测:使用 [Console]::IsInputRedirected() 检测管道模式,自动跳过 Read-Host 并输出手动添加 PATH 的说明
  5. 新增 -SkipPathUpdate 开关:供非交互场景使用

验证结果

修复前:

[WARN] Archive not found in checksums file, skipping verification

修复后:

[INFO] Verifying checksum...
[INFO] Checksum verified

安装的二进制文件 redmine.exe --version 输出 redmine version 0.5.0,功能正常。

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

- fix Invoke-WebRequest returning byte[] instead of string in PS 5.1
- add Get-WebContent helper that decodes byte[] to string when needed
- normalize CRLF in checksums content before splitting
- add fallback for Get-Arch using env:PROCESSOR_ARCHITECTURE when unavailable
- handle Read-Host in piped irm|iex sessions by detecting redirected input
- add -SkipPathUpdate switch for non-interactive use
@largeoliu largeoliu force-pushed the fix/install-ps1-compatibility branch from bbe6931 to 3e6eb92 Compare May 10, 2026 07:20
@largeoliu largeoliu merged commit 0d94f8a into master May 10, 2026
17 checks passed
@largeoliu largeoliu deleted the fix/install-ps1-compatibility branch May 10, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants