Reputation: 358
I want to get installed versions and current versions from a log file and I have got the output by using the following commands:
$versions = Select-String -Path $path -Pattern "Comparing product versions" |
% { $_.Line.Split() }
$installed = $versions[6]
$installed = echo "$installed".Trim(",")
$current = $versions[7]
The problem is: I don't want hardcoding. Is there any method like regex to replace this and to get installed version and current version seperately.
This is what the relevant log line looks like:
ISS_LOG [14:45:36]: Comparing Product Versions - Installed[1.2.0.10], Current[1.2.0.10]
Trying the 1st solution, I get an error:
Upvotes: 1
Views: 75
Reputation: 10019
You can use the regex strings Installed\[(.*)\],
and Current\[(.*)\]
, assuming the pattern is that you have installed/current followed by square brackets with the version number.
This returns groups with a full match (not what you want) and match for what's int he square brackets (what you want). More regex explanation
In Code:
$myString = "ISS_LOG [14:45:36]: Comparing Product Versions - Installed[1.2.0.10], Current[1.2.0.10]"
$installedRegex = "Installed\[(.*)\],"
$currentRegex = "Current\[(.*)\]"
$installedVersion = $([regex]::Matches($myString,$installedRegex)).Groups.value[1]
$CurrentVersion = $([regex]::Matches($myString,$currentRegex)).Groups.value[1]
Edit - PowerShell 2.0 version
$installedVersion = $([regex]::Matches($myString,$installedRegex)).Groups[1].value
$CurrentVersion = $([regex]::Matches($myString,$currentRegex)).Groups[1].value
Upvotes: 2