Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value to type "System.Version"

I cannot convert a properly formatted string stored in a variable into a version type. The issue must be with the variable as it works fine if I provide the value manually but I don't know what could be wrong with it. I am new to PowerShell so I'm not sure where to look.

Here's my variable:

Write-Host "$env:BUILD_BUILDNUMBER"
1.0.0.20​

Is it really a string? Yes:

($env:BUILD_BUILDNUMBER).GetType().fullname
System.String

Let's convert it into a version:

[version]($env:BUILD_BUILDNUMBER)
Cannot convert value "1.0.0.20​" to type "System.Version". Error: "Input string was not in a correct format."

Works just fine when tried manually:

[version]("1.0.0.20")

Any idea what's wrong with my variable? I can't change it as it's provided to me as is.

Thanks!

like image 975
Bluonic Avatar asked Jun 29 '26 01:06

Bluonic


1 Answers

I had the same issue, but within my Azure DevOps pipeline (PowerShell task).

I solved this by placing extra brackets around [Version]$someVariable, so it would be like: ([Version]$someVariable)

like image 106
user7383193 Avatar answered Jul 01 '26 19:07

user7383193