简体   繁体   English

如何从链接中获取特定值? 电源外壳

[英]How to get a specific value from a link? Powershell

let's imagine I have a link - https://chromedriver.storage.googleapis.com/LATEST_RELEASE where I can get the latest Chrome release version.假设我有一个链接 - https://chromedriver.storage.googleapis.com/LATEST_RELEASE我可以在其中获取最新的 Chrome 发布版本。 I need to retrieve this value (101.0.4951.41) and write it to the variable.我需要检索此值 (101.0.4951.41) 并将其写入变量。 For example, I create variable例如,我创建变量

$LatestChromeRelease = https://chromedriver.storage.googleapis.com/LATEST_RELEASE $LatestChromeRelease = https://chromedriver.storage.googleapis.com/LATEST_RELEASE

so the value of this variable would be '101.0.4951.41'所以这个变量的值是'101.0.4951.41'

and I can use it in my further actions.我可以在我的进一步行动中使用它。

Please advise how to achieve it in PowerShell script.请告知如何在 PowerShell 脚本中实现它。 Thanks!谢谢!

$LatestChromeRelease = (wget https://chromedriver.storage.googleapis.com/LATEST_RELEASE).Content

Since the given endpoint returns just the version and nothing else, this is simply a question of sending a web request:由于给定的端点只返回版本而不返回任何其他内容,这只是发送 Web 请求的问题:

$response = Invoke-WebRequest -Uri 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE' -UseBasicParsing
$version = if($response.StatusCode -eq 200){ $response.Content }

If the request succeeds, $version now holds the string value "101.0.4951.41" (or whatever the current latest version number is)如果请求成功, $version现在保存字符串值"101.0.4951.41" (或任何当前最新版本号)

Use Invoke-WebRequest使用Invoke-WebRequest

function Get-LatestChromeReleaseVersion{
    $Response = Invoke-WebRequest -URI https://chromedriver.storage.googleapis.com/LATEST_RELEASE
    return $Response.Content    
}

$LatestChromeRelease = Get-LatestChromeReleaseVersion

Write-Host "LatestChromeRelease:"  $LatestChromeRelease

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM