0

I am having am trying to write a script to get the latest version of citrix receiver by scraping the website with PowerShell. I am to a point where I have the latest versions, but I can't sort them descending correctly because the version minor is prioritizing 9-1 over 99-10.

Here is my code

$url = "https://www.citrix.com/downloads/citrix-receiver/"
$html = Invoke-WebRequest -Uri "$url"
$versionLinks = $html.Links | where innerHTML -Match "Receiver \d+(\.\d+)+.* for Windows$" | Sort-Object -Property innerHTML -Descending

$versionArray = @()
foreach ($version in $versionLinks){
    [version]$VersionNumber = $version.innerHTML -split " " | Select -First 2 | select -Last 1
    $versionArray += $VersionNumber
}

$versionArray = Sort-Object -InputObject $versionArray -Descending -Property minor
$LatestAppVersion = $versionArray[0]
$LatestAppVersion

What it outputs is 4.9. $versionArray looks like

Major  Minor  Build  Revision
-----  -----  -----  --------
4      9      -1     -1      
4      8      -1     -1      
4      7      -1     -1      
4      6      -1     -1      
4      5      -1     -1      
4      3      100    -1      
4      12     -1     -1      
4      11     -1     -1      
4      10     1      -1      

I would like it to be

Major  Minor  Build  Revision
-----  -----  -----  --------
4      12     -1     -1      
4      11     -1     -1      
4      10     1      -1      
4      9      -1     -1      
4      8      -1     -1      
4      7      -1     -1      
4      6      -1     -1      
4      5      -1     -1      
4      3      100    -1    

This question is similar to mine. I am using sorting version though so I am not sure why I am getting a different result. I did try using [System.Version] in case [version] was not specific enough.

2
  • Not sure if this would work or not in this scenario or not but possibly saving the data to a temporary table and using -as [Int] to make them an integer and then sort them. Commented Oct 3, 2018 at 18:59
  • I couldn't store the versions as integers or decimals because some version could have more than one "." Commented Oct 3, 2018 at 20:55

3 Answers 3

1

Are you sure you want to sort on minor? Why not sort on the entire version?

Instead of:

$versionArray = $versionArray | Sort-Object -Descending -Property minor

Consider

$versionArray = $versionArray | Sort-Object -Descending

I submit this as an answer and something to ponder... Notice the v3 and v5 test cases

<#
HAS

Major  Minor  Build  Revision
-----  -----  -----  --------
4      9      -1     -1      
4      8      -1     -1      
4      7      -1     -1      
4      6      -1     -1      
4      5      -1     -1      
4      3      100    -1      
4      12     -1     -1      
4      11     -1     -1      
4      10     1      -1      

WANTS
Major  Minor  Build  Revision
-----  -----  -----  --------
4      12     -1     -1      
4      11     -1     -1      
4      10     1      -1      
4      9      -1     -1      
4      8      -1     -1      
4      7      -1     -1      
4      6      -1     -1      
4      5      -1     -1      
4      3      100    -1    
#>

$v = @()

$v += [version]::new(4,9)
$v += [version]::new(4,8)
$v += [version]::new(4,7)
$v += [version]::new(4,6)
$v += [version]::new(4,5)
$v += [version]::new(4,3,100)
$v += [version]::new(4,12)
$v += [version]::new(4,11)
$v += [version]::new(4,10,1)
$v += [version]::new(5,1)
$v += [version]::new(3,99)

"======== MINOR ===================="
$v | Sort -Descending -Property Minor

"======== WHAT YOU MAY REALLY WANT ===================="
$v | Sort -Descending
Sign up to request clarification or add additional context in comments.

3 Comments

I was only sorting by minor as a troubleshooting step. I should not have included that in my submitted question. I want something sorted by the full version info.
Hopefully you can see from my example, how you can isolate the problem, repro, debug, and fix.
yeah, I had a misconception on how the sort-object -inputobject cmdlet worked. I just had to pip it in or pre-unwrap my array.
0

Seems that Sort-Object doesn't actually do anything when you pass an array to -InputObject by name, so pipe it instead:

$versionArray = $versionArray |Sort-Object -Descending -Property minor

2 Comments

technically, this answers the question, but I disagree with there being any valid application of sorting a version number this way by only one of the four components.
This is the answer. Also, I was only sorting by property minor as a troubleshooting method. I don't want want to do this for my final code.
0

What output do you get if you use?

$versionArray | ForEach-Object {[System.Version]$_} | Sort-Object -Property minor -Descending

It should produce your desired output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.