I am reading an XML file that contains a range of versions for an example: 8.6.22.0-8.6.24.999
My goal is to know if each one of the entries contains a range only for 8.6.22-8.6.22 and does not contain version 8.6.21/3/4 in any place in the string. I don't care if after the dot there are numbers (8.6.22.000, 8.6.22.999) I tried to do it, its working for (8.6.20.0-8.6.22.999 or 8.6.1000.0000-8.6.4000.9999) but not for (8.6.22.0-8.6.24.999)
This is part of my code: there is a function that read all the version from the XML and fill the $versions I run in a loop for each one of the versions and compare to 8.6.22*
$compareversion = "8.6.22*"
if (![string]::IsNullOrEmpty($versions)) {
Write-host "Versions range (Manifest):"
foreach ($v in $versions) {
if ($v -notlike $compareversion) {
Write-host $v -ForegroundColor Red
}
else {
Write-host $v
}
}
Write-Host
}
The output is:
Versions range (Manifest):
8.6.22.607-8.6.22.999
8.6.22.0-8.6.22.999999
8.6.22.0-8.6.22.999999
8.6.22.607-8.6.22.999
8.6.22.0-8.6.22.999
8.6.22.0-8.6.22.999
8.6.22.0-8.6.22.999
8.6.22.0-8.6.22.999
8.6.22.0-8.6.24.99999999
8.6.22.0000-8.6.22.9999
8.6.22.0000-8.6.22.9999
8.6.1000.0000-8.6.4000.9999
8.6.22.607-8.6.22.999
8.6.22.000-8.6.24.999
8.6.22.000-8.6.22.999
8.6.22.1-8.6.22.9999
8.6.22.0000-8.6.22.9999
8.6.22.1-8.6.22.9999
8.6.20.0-8.6.22.999
8.6.22.001-8.6.22.999
CodePudding user response:
You can use the Version
class to help you compare the beginning and end ranges from each line. Since this class implements IComparable
, it makes it really easy to check if a version is a above or below the target range.
Assuming all these ranges are stored in $allRanges
:
$allRanges | ForEach-Object {
$start, $end = [version[]] $_.Split('-')
if($start -ge '8.6.22' -and $end -lt '8.6.23') {
return Write-host "In Range: $_" -ForegroundColor Green
}
Write-Host "Out of Range: $_" -ForegroundColor Red
}
The result using this code is:
In Range: 8.6.22.607-8.6.22.999
In Range: 8.6.22.0-8.6.22.999999
In Range: 8.6.22.0-8.6.22.999999
In Range: 8.6.22.607-8.6.22.999
In Range: 8.6.22.0-8.6.22.999
In Range: 8.6.22.0-8.6.22.999
In Range: 8.6.22.0-8.6.22.999
In Range: 8.6.22.0-8.6.22.999
Out of Range: 8.6.22.0-8.6.24.99999999
In Range: 8.6.22.0000-8.6.22.9999
In Range: 8.6.22.0000-8.6.22.9999
Out of Range: 8.6.1000.0000-8.6.4000.9999
In Range: 8.6.22.607-8.6.22.999
Out of Range: 8.6.22.000-8.6.24.999
In Range: 8.6.22.000-8.6.22.999
In Range: 8.6.22.1-8.6.22.9999
In Range: 8.6.22.0000-8.6.22.9999
In Range: 8.6.22.1-8.6.22.9999
Out of Range: 8.6.20.0-8.6.22.999
In Range: 8.6.22.001-8.6.22.99