1
 $Uri = 'https://aka.ms/pbiSingleInstaller'
   $web = Invoke-WebRequest -Uri $uri 
   $url=$web.Links |Where-Object href -Like "*confirmation*" | Select-Object -expand href 
 ( $downloadurlid = "https://www.microsoft.com/en-us/download/" + $url )
   $idcontent = Invoke-WebRequest -Uri $downloadurlid 
 ( $downloadurl = $idcontent.Links |Where-Object href -Like "*x64.exe" | Select-Object -First 1 -expand href )
   $a = $web.Content
(  $a -match "\d*.\d*.\d*.\d* " )
 ( $latestversion = "$($Matches[1])")
 ( $FileName = $downloadurl.Split('/')[-1])

here is my script I want to get the version number which is here(https://www.microsoft.com/en-us/download/details.aspx?id=58494) like Version:

2.110.1161.0 , the match returns true but I can not see the result can someone help me how I can get the version number? thanks in advance

1 Answers1

0

Regex \d*.\d*.\d*.\d* is a problem here. \d*. matches 0 or more digits followd by any character, so full regex matches any three characters before space, even if there were no digits.
First tag in $web.Content was <!DOCTYPE html >, so $Matches now contains YPE .

Try $web.Content -match 'd\+.\d+\.\d+\.\d+'. \d+ means at least one digit, and \. a dot (. without escape \ means any character).
It now finds version number, but please note that using regex to parse html document can yield incorrect results if page content changes in the future. It'd be safer to use xPath for that.

AdamL
  • 12,421
  • 5
  • 50
  • 74
  • thanks a lot for your response, but if I use `-match '\d+\.\d+\.\d+\.\d+'` it returns true but still does not show the version when I run `( $latestversion = "$($Matches[1])")` or if I use `-match 'd\+.\d+\.\d+\.\d+'` as you said it returns false . – atefeh kasiri Nov 21 '22 at 22:26
  • 1
    @atefehkasiri It's because you're checking `$Matches[1]`, but you have not specified capture group in regex. Try `$Matches[0]` or use regex `'(\d+\.\d+\.\d+\.\d+)'` to have it in `$Matches[1]`. – AdamL Nov 22 '22 at 09:32