macOS 下检测移动硬盘速度脚本

脚本

  • 自动判断当前插入的移动硬盘的盘符,
  • 如果有多个移动硬盘, 显示列表, 由使用者选择测试的移动硬盘
  • 从 关于本机 | 系统报告 | USB 信息 中读取当前协商速度
  • 有时速度太慢, 可能与移动硬盘插入速度有关, 快速速度过慢可能会被协商为 2.0 的速度
# 1. 权限预热
Write-Host "--- 权限验证 ---" -ForegroundColor Yellow
sudo -v
if ($LASTEXITCODE -ne 0) { exit }

# 2. 自动获取磁盘
$volumes = Get-ChildItem /Volumes | Where-Object { 
    $_.Attributes -match "Directory" -and 
    $_.Name -ne "Macintosh HD" -and $_.Name -ne "Preboot" -and $_.Name -ne "VM"
}

if ($volumes.Count -eq 0) { Write-Host "未检测到磁盘。" -ForegroundColor Red; exit }
$targetVolume = if ($volumes.Count -eq 1) { $volumes[0] } else {
    Write-Host "`n检测到多个磁盘:" -ForegroundColor Cyan
    for ($i = 0; $i -lt $volumes.Count; $i++) { Write-Host "$($i+1). $($volumes[$i].Name)" }
    $choice = Read-Host "选择序号"
    $volumes[[int]$choice - 1]
}

$targetPath = $targetVolume.FullName
$diskName = $targetVolume.Name

# 3. 终极物理速度抓取 (XML 解析法)
Write-Host "正在检索硬件物理链路速度 (XML 解析)..." -NoNewline
$physicalInfo = "还未实现"
# try {
#     # 导出存储设备的 XML
#     [xml]$storageXml = system_profiler SPStorageDataType -xml
#     # 寻找当前卷名对应的节点
#     $volNode = $storageXml.SelectNodes("//dict[string[contains(., '$diskName')]]")

#     # 尝试寻找物理物理层描述 (如 'USB' 或 'Thunderbolt')
#     $protocol = $volNode.SelectNodes("./string[preceding-sibling::key[1] = 'protocol']") | Select-Object -ExpandProperty '#text' -First 1

#     # 获取连接速度描述 (在 Storage 分类中通常隐藏在 physical_drive 属性中)
#     # 如果 Storage 里拿不到具体速度,我们就显示协议类型
#     $physicalInfo = if ($protocol) { "$protocol 协议设备" } else { "外部驱动器" }
# }
# catch {
#     $physicalInfo = "解析异常, $($_.Exception.Message)"
# }

Write-Host " [完成]" -ForegroundColor Green

# 4. 空间检查
$driveInfo = Get-PSDrive -PSProvider FileSystem | Where-Object { $targetPath -match $_.Root }
if ($driveInfo.Free -lt 2GB) { Write-Host "空间不足 2GB。" -ForegroundColor Red; exit }

# 5. 读写测试
$testFile = Join-Path $targetPath "speedtest_tmp.bin"
Write-Host "`n--- 正在进行深度测试 (2GB) ---" -ForegroundColor Cyan

Write-Host "正在测试写入..." -NoNewline
sudo purge
$writeRaw = dd if=/dev/zero of=$testFile bs=1m count=2048 conv=sync 2>&1 | Out-String
if ($writeRaw -match "\((?<speed>\d+)\s+bytes/sec\)") {
    $wNum = [double]$Matches['speed'] / 1MB
    $writeMB = "{0:N2} MB/s" -f $wNum
}

Write-Host " [完成]" -ForegroundColor Green
Write-Host "正在测试读取..." -NoNewline
sudo purge
$readRaw = dd if=$testFile of=/dev/null bs=1m 2>&1 | Out-String
if ($readRaw -match "\((?<speed>\d+)\s+bytes/sec\)") {
    $rNum = [double]$Matches['speed'] / 1MB
    $readMB = "{0:N2} MB/s" -f $rNum
}
Write-Host " [完成]" -ForegroundColor Green

# 6. 清理
if (Test-Path $testFile) { Remove-Item $testFile }

# 7. 汇总报表
$status = "正常"
if ($physicalSpeed -match "480 Mb") { $status = "警告:当前处于 USB 2.0 降级模式" }
elseif ($wNum -gt 300) { $status = "优秀:符合 USB 3.0 高速标准" }

$results = [System.Collections.Generic.List[PSCustomObject]]::new()
$results.Add([PSCustomObject]@{ "指标" = "磁盘卷名"; "数值" = $diskName })
$results.Add([PSCustomObject]@{ "指标" = "系统报告速度"; "数值" = $physicalInfo })
$results.Add([PSCustomObject]@{ "指标" = "实测写入速度"; "数值" = $writeMB })
$results.Add([PSCustomObject]@{ "指标" = "实测读取速度"; "数值" = $readMB })
$results.Add([PSCustomObject]@{ "指标" = "性能评估"; "数值" = $status })

Write-Host "`n--- 测试结果清单 ---" -ForegroundColor Green
$results | Format-Table -AutoSize
上一篇
下一篇