首页 > 解决方案 > 将本地文件 (Get-Content) 与 Web 文件 (Invoke-WebRequest) 进行比较总是导致不相等

问题描述

基本上我正在为我的脚本编写一个更新程序。它应该将自己 ( Get-Content -Path $PSCommandPath) 与 Web 服务器上的 ( Invoke-WebRequest...) 进行比较。但是,它总是显示有可用的更新。

我的理解(来自此处的其他问题)-raw将强制它将文件作为字符串返回,这应该与返回 Web 请求的方式相同。但我猜问题出在某个地方。希望是一个简单的事情...

function checkUpdates() {
    Write-Host "Checking for updates... " -NoNewline -ForegroundColor $clrMain
    $item1 = Get-Content -Path $PSCommandPath -raw
    $item2 = Invoke-WebRequest -Uri "$($config.remote)/launcher/launch.ps1"
    if ($item1 -eq $item2) {
        Write-Host "Up to date" -ForegroundColor $clrSuccess
    } else {
        Write-Host "New version available" -ForegroundColor $clrWarn
        invoke-expression "./update.ps1 $($config.remote)"
        exit
    }
}

标签: powershellpowershell-7.0

解决方案


我能够替换invoke-webrequestcurl替换比较来完成我想要的。

function checkUpdates() {
    Write-Host "Checking for updates... " -NoNewline -ForegroundColor $clrMain
    $item1 = Get-Content -Path $PSCommandPath
    $item2 = curl "$($config.remote)/launcher/launch.ps1"
    if (!(Compare-Object -ReferenceObject $item1 -DifferenceObject $item2)) {
        Write-Host "Up to date" -ForegroundColor $clrSuccess
    } else {
        Write-Host "New version available" -ForegroundColor $clrWarn
    }
}

推荐阅读