powershell - 将本地文件 (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
}
}
解决方案
我能够替换invoke-webrequest
并curl
替换比较来完成我想要的。
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
}
}
推荐阅读
- javascript - 在数组中查找通常彼此相邻出现的元素
- python - 如何用查找排除所有标题?
- sql-server - 在环回数据模型中定义数据类型为 varbinary 的字段
- google-cloud-platform - 错误:4 DEADLINE_EXCEEDED:Object.exports.createStatusError 已超过最后期限 - GCP
- kubernetes - 如何确保 GKE 中我的节点上只运行一个 pod?
- java - 为邻居过滤数组
- php - PHP Loop html组合具有相同值的表行
- python - 有没有一种简单的方法来扩展现有的激活函数?我的自定义 softmax 函数返回:一个操作对梯度有 `None`
- mysql - Laravel where 和 orWhere 函数
- ubuntu - 无法在 ubuntu 18.04 中安装 web3