I am trying to write a script, which will return a formatted version number.
However: due to winget outputting text, I have to first convert it to object, using the
Then I can get rid of the ID, which I have no use for and display only name and version.
Here's the code, taken from this answer:
function ConvertFrom-FixedColumnTable {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)] [string] $InputObject
)
begin {
Set-StrictMode -Version 1
$lineNdx = 0
}
process {
$lines =
if ($InputObject.Contains("`n")) { $InputObject.TrimEnd("`r", "`n") -split '\r?\n' }
else { $InputObject }
foreach ($line in $lines) {
++$lineNdx
if ($lineNdx -eq 1) {
# header line
$headerLine = $line
}
elseif ($lineNdx -eq 2) {
# separator line
# Get the indices where the fields start.
$fieldStartIndices = [regex]::Matches($headerLine, '\b\S').Index
# Calculate the field lengths.
$fieldLengths = foreach ($i in 1..($fieldStartIndices.Count-1)) {
$fieldStartIndices[$i] - $fieldStartIndices[$i - 1] - 1
}
# Get the column names
$colNames = foreach ($i in 0..($fieldStartIndices.Count-1)) {
if ($i -eq $fieldStartIndices.Count-1) {
$headerLine.Substring($fieldStartIndices[$i]).Trim()
} else {
$headerLine.Substring($fieldStartIndices[$i], $fieldLengths[$i]).Trim()
}
}
}
else {
# data line
$oht = [ordered] @{} # ordered helper hashtable for object constructions.
$i = 0
foreach ($colName in $colNames) {
$oht[$colName] =
if ($fieldStartIndices[$i] -lt $line.Length) {
if ($fieldLengths[$i] -and $fieldStartIndices[$i] + $fieldLengths[$i] -le $line.Length) {
$line.Substring($fieldStartIndices[$i], $fieldLengths[$i]).Trim()
}
else {
$line.Substring($fieldStartIndices[$i]).Trim()
}
}
++$i
}
# Convert the helper hashable to an object and output it.
[pscustomobject] $oht
}
}
}
}
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
(winget list --name "ourSoft") -match '^(\p{L}|-)' | # filter out progress-display lines
ConvertFrom-FixedColumnTable | # parse output into objects
Select-Object -Property Name, Version | # show only Name and Version
Sort-Object Name | # sort by the Name property (column)
Format-Table # display the objects in tabular format
I am trying to do the last step, which will return a version number different in what is stored in OS:
5.4.33676.0 - stored number in OS
5.4.0.33676 - what I want to return (part 3 and 4, divided by a dot, are swapped)
Is there any way to do that?
I am trying to write my own function for that, but it seems CharArray is not filling up + I don´t know how to make the piped output display the modified numbers instead of the former ones:
function ModifyVersionNumber {
$VersionNumber = Select-Object -Property Version
$CharArray =$VersionNumber.Split(".")
$ModifiedCharArray = $CharArray[0] + "." + $CharArray[1] + "." + $CharArray[3] + "." + $CharArray[2]
$ModifiedCharArray
}
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
(winget list --name "ourSoft") -match '^(\p{L}|-)' | # filter out progress-display lines
ConvertFrom-FixedColumnTable | # parse output into objects
ModifyVersionNumber |
Select-Object -Property Name, Version | # show only Name and Version
Sort-Object Name | # sort by the Name property (column)
Format-Table # display the objects in tabular format