Powershell排序版本对象降序

时间:2018-10-03 18:55:05

标签: powershell sorting

我正在尝试编写脚本以通过使用PowerShell刮擦网站来获取最新版本的citrix接收器。我已经到了最新版本,但是我无法正确地对它们进行降序排列,因为次要版本将9-1优先于99-10。

这是我的代码

$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

它输出的是4.9。 $ versionArray看起来像

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      

我希望成为

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与我的相似。我正在使用排序版本,所以我不确定为什么会得到不同的结果。我确实尝试过使用[System.Version],以防[version]不够具体。

3 个答案:

答案 0 :(得分:0)

如果使用,您会得到什么输出?

var source = {
  localdata: [
    ["https://www.samplelink.com", "Maria Anders", "Sales Representative", "Obere Str. 57", "Berlin", "Germany"],
    ["https://www.samplelink.com", "Ana Trujillo", "Owner", "Avda. de la Constitucin 2222", "Mxico D.F.", "Mexico"],
    ["https://www.samplelink.com", "Antonio Moreno", "Owner", "Mataderos 2312", "Mxico D.F.", "Mexico"],
    ["https://www.samplelink.com", "Thomas Hardy", "Sales Representative", "120 Hanover Sq.", "London", "UK"],
    ["https://www.samplelink.com", "Christina Berglund", "Order Administrator", "Berguvsvgen 8", "Lule", "Sweden"],
    ["https://www.samplelink.com", "Hanna Moos", "Sales Representative", "Forsterstr. 57", "Mannheim", "Germany"],
    ["https://www.samplelink.com", "Roland Mendel", "Sales Manager", "Kirchgasse 6", "Graz", "Austria"]
  ],
  datafields: [{
      name: 'link',
      type: 'string',
      map: '0'
    },
    {
      name: 'ContactName',
      type: 'string',
      map: '1'
    },
    {
      name: 'Title',
      type: 'string',
      map: '2'
    },
    {
      name: 'Address',
      type: 'string',
      map: '3'
    },
    {
      name: 'City',
      type: 'string',
      map: '4'
    },
    {
      name: 'Country',
      type: 'string',
      map: '5'
    }
  ],
  datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source);


$(".clickable").jqxGrid({
  width: 800,
  height: 270,
  source: dataAdapter,
  columnsresize: true,
  sortable: true,
  columns: [{
      text: 'Link',
      datafield: 'link',
      width: 200
    },
    {
      text: 'Contact Name',
      datafield: 'ContactName',
      width: 150
    },
    {
      text: 'Contact Title',
      datafield: 'Title',
      width: 100
    },
    {
      text: 'Address',
      datafield: 'Address',
      width: 100
    },
    {
      text: 'City',
      datafield: 'City',
      width: 100
    },
    {
      text: 'Country',
      datafield: 'Country'
    }
  ]
});

$(".clickable").on("rowselect", handleClick);

function handleClick(e) {
  var $el = $("<div/>", {
    class: "clickable",
    style: "margin:100px 10px 20px 20px ",
  }).append($("<div />", {
    id: "button"
  })).on('click', handleClick);

  console.log("Element el test");
  console.log($el);

  /*var buttonDiv = $("<div />", {
    id: "button"
  });
  console.log("Testing button Div");
  console.log(buttonDiv);

  $("#button").jqxButton({
    value: 'Export Grid '
  });*/

  $el.jqxGrid({
    height: 270,
    source: dataAdapter,
    columns: [{
        text: 'Link',
        datafield: 'link',
        width: 200
      },
      {
        text: 'Contact Name',
        datafield: 'ContactName',
        width: 150
      },
      {
        text: 'Contact Title',
        datafield: 'Title',
        width: 100
      },
      {
        text: 'Address',
        datafield: 'Address',
        width: 100
      },
      {
        text: 'City',
        datafield: 'City',
        width: 100
      },
      {
        text: 'Country',
        datafield: 'Country'
      }
    ]
  });

  var $this = $(this),
    $parent = $(this).parent();

  if (e.type == 'rowselect') {
    $('.clickable').slice(1).remove();
  }
  $parent.append($el);
}

它应该产生您想要的输出

答案 1 :(得分:0)

当您按名称将数组传递给Sort-Object时,似乎-InputObject实际上没有任何作用,因此可以用管道将其代替:

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

答案 2 :(得分:0)

您确定要对未成年人进行排序吗?为什么不对整个版本进行排序?

代替:

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

考虑

$versionArray = $versionArray | Sort-Object -Descending

我将其作为答案提交并需要思考... 注意v3和v5测试用例

<#
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