I saw a post internally that asked this question: Anyone have a handy powershell script testing if the installed flyway version matches a specific string?
That seemed simple, but getting program output from PoSh wasn’t something I’ve tried. So I tackled the challenge and this is what happened.
Getting the Output
The first thing I wanted to do was actually figure out what the output of checking the version was from the CLI. I looked at the help and noticed a version verb. When I run that, I see a bunch of lines of output.
A lot of output. I need to parse a bunch of strings, and then find a line.
My first experiment was to run this to get a file with this output.
flyway version > fwversion.txt
Now, let’s parse this.
Parsing Content
It’s been awhile since I read stuff from a file, but I know Get-Content works to read the file. What about finding a line. I saw this post with an answer that noted Select-String can be used, so I decided to try that.
Here’s a first cut of code:
That didn’t work. However, with some experiments, I tried this code:
Get-content fwversion.txt | select-string 'Edition'
That worked.
Now, I’ll assign that to a variable with this code:
$a=Get-content fwversion.txt | select-string ‘Edition’
Next, I’ll split this string by spaces into a new variable with this:
$b = $a -split(‘ ‘)
Then I can evaluate the various element of $b. You can see below the first and third elements are what I’m interested in. Really the third. Remember, PoSh is zero-based.
That let’s me parse the output, but I don’t want to save a file. Now on to the next step.
Capturing the Output from a Program
One of the things I know you can do in a PoSh ptompt is run a program. The redirection operator allows you to move output. When I tried it, I couldn’t quite get the output I wanted, but I did find this post that helped. With that, I ran this code:
$a = & "flyway" --version 2>&1 | select-string 'Edition'
This runs Flyway, captures the output in a stream and then uses the code above to find the right line. I assign this to a variable.
Almost there.
Adding a Parameter and a Test
Since I want to call this from the CLI and pass i a parameter, I added a param() clause to my script and then a test that compares the version output from the flyway.exe to the parameter. That gest me this code:
param(
[string]$versionToCheck=""
)
$a = & "flyway" --version 2>&1 | select-string 'Edition'
$b = $a -split(' ')
if ($b[3] -eq $versionToCheck)
{ Write-Output("$($b) installed")}
else {
Write-Output(“wrong version – $($b) installed”)
}
Now I can call this from the CLI and check things. It works well. At least for now.
I am certainly not a PoSh expert, but this short script took me about 15 minutes to write with a little research. Then a little testing and I sent it off to the requester. Haven’t heard any complaints, so I’m hoping this actually works for them.


If the EXE is properly configured you can grab the version from the EXE itself
[version](Get-Item c:pathtoflyway.exe).versioninfo.productversion
LikeLike
In this case, it isn’t an exe. Not quite sure how this works, but it’s a Java based thing.
Still, this is good to know
LikeLike