0

Basically I'm writing an updater for my script. It's supposed to compare itself (Get-Content -Path $PSCommandPath) to the one on the web server (Invoke-WebRequest...). However, it always shows an updated is available.

My understanding (from other questions here) is that -raw will force it to return the file as a string which should be the same way the web request is returned. But I'm guessing the issue is somewhere in there. Hopefully a simple thing...

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
    }
}
  • 1
    Do both systems use the same EOL (CR, LF, or CRLF)? Does `Invoke-WebRequest` also return the HTTP headers, or return a structured object with the actual content being a member of the object? – Jeff Zeitlin May 18 '21 at 19:29
  • hmm, it probably is the http headers. My code was loosely based on this question: https://stackoverflow.com/questions/56970726/compare-local-text-file-with-online-file so I assumed it would not be a problem. What are my options? –  May 18 '21 at 19:31
  • 1
    For a start, you could just output `$item1` and `$item2`. You will immediately see the difference. After that, you could consult the documentation of [`Invoke-WebRequest`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.1) to get more details about what it returns. – stackprotector May 18 '21 at 20:00
  • While `Invoke-WebRequest` does return an object that wraps the response data and contains metadata, that object stringifies to its `.Content` property value. To narrow down the problem, use the line-by-line comparison technique from the linked answer, which will implicitly eliminate differences in newline styles. – mklement0 May 18 '21 at 20:09

1 Answers1

0

I was able to replace invoke-webrequest with curl and replace the comparison to accomplish what I wanted.

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
    }
}
  • 2
    It's commendable that you want to share a solution, but as of this writing it is confusing: You modified your `Get-Content` call to omit `-Raw` and you changed your equality comparison to use `Compare-Object` - without explanation. Similarly, you're not explaining why use of `curl` - which _in Windows PowerShell is simply an alias of `Invoke-WebRequest`_ - made a difference. Are you using PowerShell (Core) v6+, where `curl` refers to `curl.exe`? If so, what made the difference? – mklement0 May 18 '21 at 22:13
  • I don't know the answers to your question. If someone does and can post a new answer, I will accept that. –  Jun 02 '21 at 22:03