Returning Windows version as int

Returning Windows version as int

Post by Z3JlYXRiYX » Sun, 19 Aug 2007 08:02:04


I am trying to have WMI spit out the version number and then based on that,
assign the variable $OS to either V or XP...take a look

$Get_OS = Get-WmiObject Win32_OperatingSystem
$OS = $Get_OS.Version

$OS = [math]::truncate($OS)

switch ($OS)
{
5 {$OS = "XP"}
6 {$OS = "V"}
}
write-host $OS

That doesnt seem to work. I am currently using a method that cuts off all
but the first character so it displays as only 5 or 6, but when we get into
higher versions, 10..11, etc..this method wont work. How can i get the
windows version for what i am looking to do. Apparently it cant truncate it
because the variable $OS is seen as a string and not an int. Any ideas?
 
 
 

Returning Windows version as int

Post by Brandon Sh » Sun, 19 Aug 2007 08:15:12

Try this
$os = [int]($Get_OS.Version).split(".")[0]

 
 
 

Returning Windows version as int

Post by Z3JlYXRiYX » Sun, 19 Aug 2007 09:32:00

Brandon,

Can you explain to me a little what the split part does?
 
 
 

Returning Windows version as int

Post by Brandon Sh » Sun, 19 Aug 2007 09:38:59

Split is a standard method on a string. It converts the string to an array
splitting on what ever you specify. In this case "."

split(".")[0] means split on "." and get first element.

More info on String methods
http://www.yqcomputer.com/
 
 
 

Returning Windows version as int

Post by Z3JlYXRiYX » Sun, 19 Aug 2007 09:56:01

gotcha...sorry i just read it in the gui help. Also, what if i wanted to
split it on the 2nd "." would i use [2]?
 
 
 

Returning Windows version as int

Post by Brandon Sh » Sun, 19 Aug 2007 10:44:55

Array is 0 based... so second element would be 1, but yes... that is the
general idea.
 
 
 

Returning Windows version as int

Post by Brandon Sh » Sun, 19 Aug 2007 10:58:58

Sorry... I just reread your statement... and the answer is no

Let me give you an example... lets take 6.0.0.600

PS> ("6.0.0.600").split(".")
6
0
0
600

If you just want the 6 you could do this
("6.0.0.600").split(".")[0]
For 600
("6.0.0.600").split(".")[3]
 
 
 

Returning Windows version as int

Post by Keith Hill » Sun, 19 Aug 2007 11:42:32


*IFF* you only care about running this code on the local machine then this
approach is quite a bit easier:

[Environment]::OSVersion.Version.Major

It is a .NET Framework feature.
 
 
 

Returning Windows version as int

Post by Z3JlYXRiYX » Sun, 19 Aug 2007 13:14:00

geez...that is easy. thanks to both of you!!
 
 
 

Returning Windows version as int

Post by Z3JlYXRiYX » Sun, 19 Aug 2007 13:16:01

Keith,

is there a reference for this stuff? i would like to see more of it.
 
 
 

Returning Windows version as int

Post by Brandon Sh » Sun, 19 Aug 2007 20:00:52

Ref for System.Environment aka [Environment]
http://www.yqcomputer.com/
 
 
 

Returning Windows version as int

Post by Z3JlYXRiYX » Mon, 20 Aug 2007 00:22:01

It mentions that that page is specific to net framework 3.0. does that mean
.net 3.0 has to be installed otherwise that query will not work?
 
 
 

Returning Windows version as int

Post by Brandon Sh » Mon, 20 Aug 2007 00:26:03

Whoops... here is the 2.0 version
http://www.yqcomputer.com/ (vs.80).aspx

What you want to do will work in 2.0
 
 
 

Returning Windows version as int

Post by Z3JlYXRiYX » Mon, 20 Aug 2007 00:28:02

Thanks...geez, you guys know a ton about this stuff
 
 
 

Returning Windows version as int

Post by IT Staf » Wed, 22 Aug 2007 10:28:09

No one is born to know everything.

If you want to know everything, subscribe to MSDN :-). All the classes all
there...only need to know where to look for leaading answers ...

I prefer MSDN as i don't like accessing internet which is slow ...