October 28, 2016 at 2:18 pm #56386
Any idea why this is happening ?
On a windows 10
PS C:\> $os = Get-WmiObject win32_operatingsystem PS C:\> $o.version -gt 6.2 False
While in Win 8.1
PS C:\> $os = Get-WmiObject win32_operatingsystem PS C:\> $o.version -gt 6.2 True
October 28, 2016 at 2:21 pm #56389
See the documentation at https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx.
Version is a string; you can't do numeric comparisons with it, unless you cast it as a [single] first. And even that will fail if the version is ever something like 6.2.2, since that's not actually a number.
October 28, 2016 at 2:26 pm #56395
You'd have to cast both values – the Version property and the 6.2 comparison – to [version], but yes, that's what [version] is for.
October 28, 2016 at 4:49 pm #56407
The casting that is being referred to would be something like this:
[version]$os.Version -gt [version]"6.2"
Powershell will do type conversions for you in some cases, however, "strong-typed" code will not leave things to chance. You want to do comparisons to the same type string to string, int to int, etc. and in this case version to version. Casting attempts to convert the value to the type so that a proper comparison can be done. Even when I was playing with the version cast, it didn't like X.0:
PS C:\Users\Rob> [version]10.0 Cannot convert value "10" to type "System.Version". Error: "Version string portion was too short or too long." At line:1 char:1 + [version]10.0 + ~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) , RuntimeException + FullyQualifiedErrorId : InvalidCastParseTargetInvocation PS C:\Users\Rob> [version]"10.0" Major Minor Build Revision ----- ----- ----- -------- 10 0 -1 -1
It's especially important when doing comparisons like -gt, because you are basically doing something like TEN -gt 6.2.
October 31, 2016 at 7:43 am #56482
exactly what i was looking for , the [version] cast , didn't know about it before
nice find 🙂
PS C:\> [version]$os.version -gt [version]"6.2" True
You must be logged in to reply to this topic.