使用 powershell 从网站获取最新下载 url 和软件版本

getting the latest download url and version of a software from a website with powershell

I want to take the take the latest version of the software and check with the version that is installed on system if it is newer install the new version .

''' $web = Invoke-WebRequest -Uri "https://www.webex.com/downloads/jabber/jabber-vdi.html" ( $latest = $web.AllElements | Where-Object {$_.TagName -eq "li"} | Select-String "Jabber Windows client" | Select -第一个 )'''

for taking the version number and url I've wrote these but does not work

''' ( $latestversion = $latest.Context | Select-String -pattern "\d\d.\d") ( $downloadUrl=$latest.Context | Select-String -pattern "\w.msi" )'''

also I have tried this way but does not work

'''$latestversion = $latest.links.href'''

您可以使用链接 属性 查看所有检索到的链接,然后将其过滤为 select 只有那些以“msi”结尾的链接

(Invoke-WebRequest -Uri "https://www.webex.com/downloads/jabber/jabber-vdi.html").Links | Where-Object href -like '*msi' | select -First 1 | select -expand href

编辑:要获得两者,可以像这样使用 ParsedHtml:

(Invoke-WebRequest -Uri "https://www.webex.com/downloads/jabber/jabber-vdi.html").ParsedHtml.body.getElementsByClassName('vdi-links')[0].innerHTML -match "<LI>(\d{1,2}\.\d).*(https.*msi)"
write-host "Version $($Matches[1]) available at $($Matches[2])"

$Matches 是一个自动变量,它包含 -match 正则表达式的结果。匹配中的括号定义了我们的匹配组,因此对于我们的正则表达式 "<LI>(\d{1,2}\.\d).*(https.*msi)":

我们的第一个匹配项是 (\d{1,2}\.\d),其中 \d 是任意数字,{1,2} 表示匹配 1 或 2(因此我们可以匹配“9”或“10”),\. 按字面意思匹配点字符

我们的第二个匹配项是 (https.*msi),其中 . 匹配任何字符,* 表示匹配任意次数。