Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


OS detection

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2993
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> OS detection
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 OS detection - 5/13/2005 11:37:49 PM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
'My script :

Set WshShell = wscript.CreateObject ("Wscript.Shell")
WinVers = WshShell.Run("%comspec% /c ver")
wscript.echo WinVers

'In result, WinVers return 0 ! Why ?????
 
 
Post #: 1
 
 Re: OS detection - 5/14/2005 12:55:54 AM   
  TNO


Posts: 1303
Score: 12
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Maybe this will work for you:

SetLocale "en-us" ' do not remove


iOSVer = GetOsVersionNumber()


Function GetOsVersionNumber()
''''''''''''''''''''''''''''''í''''''''''''''''''''''''''''''í''''''
' Determines OS by reading reg val & comparing to known values
' OS version number returned as number of type double:
' Windows 95: 1
' Windows 98: 2
' Windows ME: 3
' Windows NT4: 4
' Windows 2k: 5
' Windows XP: 5.1
' Windows Server 2003: 5.2
' Windows x: >5.2
'
''''''''''''''''''''''''''''''í''''''''''''''''''''''''''''''í''''''
Dim oShell, sOStype, sOSversion
Set oShell = CreateObject("Wscript.Shell")


On Error Resume Next
sOStype = oShell.RegRead(_
"HKLM\SYSTEM\CurrentControlSetí\Control\ProductOptions\ProducítType")
If Err.Number<>0 Then
' Hex(Err.Number)="80070002"
' - Could not find this key, OS must be Win9x
Err.Clear


sOStype = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windoíws" & _
"\CurrentVersion\VersionNumberí")


Select Case sOStype
Case "4.00.950"
sOSversion = 1 ' Windows 95A
Case "4.00.1111"
Dim sSubVersion
sSubVersion = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windoíws" & _
"\CurrentVersion\SubVersionNumíber")
Select Case sSubVersion
Case " B"
sOSversion = 1 ' Windows 95B
Case " C"
sOSversion = 1 ' Windows 95C
Case Else
sOSversion = 1 ' Unknown Windows 95
End Select
Case "4.03.1214"
sOSversion = 1 ' Windows 95B
Case "4.10.1998"
sOSversion = 2 ' Windows 98
Case "4.10.2222"
sOSversion = 2 ' Windows 98SE
Case "4.90.3000"
sOSversion = 3 ' Windows Me
Case Else
sOSversion = 1 ' Unknown W9x/Me
End Select
Else ' OS is NT based
sOSversion = oShell.RegRead(_
"HKLM\SOFTWARE\Microsoft\Windoíws NT\CurrentVersion\CurrentVersiíon")
If Err.Number<>0 Then
GetOsVersion = "Unknown NTx"
' Could not determine NT version
Exit Function ' >>>
End If
End If

' Setting Locale to "en-us" to be indifferent to country settings.
' CDbl might err else
SetLocale "en-us"
GetOsVersionNumber = CDbl(sOSversion)
End Function
wscript.echo GetOsVersionNumber

(in reply to littlebouda)
 
 
Post #: 2
 
 Re: OS detection - 5/14/2005 1:18:24 AM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
Thanks. I would do a script in 4-5 code line, but the registry base is another way. I will try it.

(in reply to littlebouda)
 
 
Post #: 3
 
 Re: OS detection - 5/14/2005 2:27:20 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
WinVers returns the status (exit) code of runinng the cmd you specified, not the output of the cmd.
You have 2 ways of determing os related info. The code below uses the same way you provided.

Option Explicit
Dim shell
Set shell = CreateObject("WScript.Shell")

WScript.Echo shell.Exec("cmd /c ver").StdOut.ReadAll

(in reply to littlebouda)
 
 
Post #: 4
 
 Re: OS detection - 5/14/2005 3:42:38 PM   
  TNO


Posts: 1303
Score: 12
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
StdOut.ReadAll
Excellent, I never knew that.

(in reply to littlebouda)
 
 
Post #: 5
 
 Re: OS detection - 5/16/2005 12:01:55 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
I use have created a function, 3 to be exact, that will find the OS. 1 uses WMI while the other 2 use ver, 1 with exec and the other writing to and reading from a txt file.


Option Explicit

msgbox GetOS1
msgbox GetOS2
msgbox GetOS3

Function GetOS1()
'Will work with most versions of WSH.
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1

Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
Dim sTemp, sTempFile, fFile, sResults
sTemp = WshShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"

WshShell.Run "%comspec% /c ver >" & sTempFile, 0, True

Set fFile = FSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)

sResults = fFile.ReadAll
fFile.Close
FSO.DeleteFile(sTempFile)

Select Case True
'Add more info to the 98 and 95 to get the specific version. i.e. 98SE 95 a,b,or c
Case InStr(sResults, "Windows 95") > 1 : GetOS1 = "W95"
Case InStr(sResults, "Windows 98") > 1 : GetOS1 = "W98"
Case InStr(sResults, "Windows Millennium") > 1 : GetOS1 = "WME"
Case InStr(sResults, "Windows NT") > 1 : GetOS1 = "NT4"
Case InStr(sResults, "Windows 2000") > 1 : GetOS1 = "W2K"
Case InStr(sResults, "Windows XP") > 1 : GetOS1 = "WXP"
Case Else : GetOS1 = "Unknown"
End Select
End Function

Function GetOS2()
'Requires WSH 5.6
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
Dim strVer : set strVer = WshShell.exec("%comspec% /c ver")
Dim sResults
sResults = strVer.stdout.readall

Select Case True
'Add more info to the 98 and 95 to get the specific version. i.e. 98SE 95 a,b,or c
Case InStr(sResults, "Windows 95") > 1 : GetOS2 = "W95"
Case InStr(sResults, "Windows 98") > 1 : GetOS2 = "W98"
Case InStr(sResults, "Windows Millennium") > 1 : GetOS2 = "WME"
Case InStr(sResults, "Windows NT") > 1 : GetOS2 = "NT4"
Case InStr(sResults, "Windows 2000") > 1 : GetOS2 = "W2K"
Case InStr(sResults, "Windows XP") > 1 : GetOS2 = "WXP"
Case Else : GetOS2 = "Unknown"
End Select
End Function

Function GetOS3
'WMI is required for this script to function
Dim strComputer, strWMIOS
'For Local use only, remove the inputbox and make strComputer = "."
strComputer = Inputbox("Input the name of the remote computer or hit enter for this PC.")
If strComputer = "" then
strComputer = "."
End if

Dim objWmiService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2")
Dim strOsQuery : strOsQuery = "Select * from Win32_OperatingSystem"
Dim colOperatingSystems : Set colOperatingSystems = objWMIService.ExecQuery(strOsQuery)
Dim objOs
Dim strOsVer

For Each objOs in colOperatingSystems
strWmios = objOs.Caption & " " & objOs.Version
Next
msgbox strWMIOS
Select Case True
'Add more info to the 98 and 95 to get the specific version. i.e. 98SE 95 a,b,or c
Case InStr(strWmiOS, "2000 Server") > 1 : GetOS3 = "2KSRV"
Case InStr(strWmiOS, "2003, Standard") > 1 : GetOS3 = "2K3SRV"
Case InStr(strWmiOS, "2003, Enterprise") > 1 : GetOS3 = "2K3ENTSRV"
Case InStr(strWmiOS, "2000 Advanced Server") > 1 : GetOS3 = "2KADVSRV"
Case InStr(strWmiOS, "Windows NT") > 1 : GetOS3 = "NT4"
Case InStr(strWmiOS, "Windows 2000") > 1 : GetOS3 = "W2K"
Case InStr(strWmiOS, "Windows XP") > 1 : GetOS3 = "WXP"
Case Else : GetOS3 = "Unknown"
End Select
End Function

< Message edited by mbouchard -- 8/29/2005 6:57:54 AM >

(in reply to littlebouda)
 
 
Post #: 6
 
 Re: OS detection - 5/16/2005 12:26:37 AM   
  crazymatt

 

Posts: 296
Score: 0
Joined: 3/4/2005
From:
Status: offline
Ambitous piece of art mbouchard! :)
Keep up the good work.

/Matt

(in reply to littlebouda)
 
 
Post #: 7
 
 Re: OS detection - 5/16/2005 12:56:33 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Thanks

(in reply to littlebouda)
 
 
Post #: 8
 
 Re: OS detection - 5/16/2005 8:04:44 PM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
Thanks a lot

<token> -> i will try to convert my script with exec method with stout.readall
<mbouchard> I think i will stay on the script of TNO or token. Thank you anyway.

(in reply to littlebouda)
 
 
Post #: 9
 
 Re: OS detection - 5/16/2005 8:19:05 PM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
Ok. Here is my script; But Mid function doesn't work as i want... I will try to modify it.

set WshShell = WScript.CreateObject ("WScript.Shell")
Winvers = WshShell.exec("%comspec% /c ver").stdout.readall
wscript.echo Winvers
WinVers = Right(WinVers,20)
wscript.echo Winvers
comp = Mid(WinVers, 19, 2)
wscript.echo comp

Select Case comp
Case comp = "95"
GetOS = "95"

Case comp = "98"
GetOS = "98"

Case comp = "Mi"
GetOS = "Millenium"

Case comp = "Mi"
GetOS = "2000"

Case comp = "NT"
GetOS = "NT"

Case comp = "XP"
GetOS = "XP"

Case else : GetOS = "Unknown"

End Select
wscript.Echo GetOS

(in reply to littlebouda)
 
 
Post #: 10
 
 Re: OS detection - 5/16/2005 8:21:43 PM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
I have to use Left and not Right !! [:I]

WinVers = Left(WinVers,22)
wscript.echo Winvers
comp = Mid(WinVers, 21, 2)
wscript.echo comp

But my script continue to return "unknown"

(in reply to littlebouda)
 
 
Post #: 11
 
 Re: OS detection - 5/16/2005 8:25:20 PM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
set WshShell = WScript.CreateObject ("WScript.Shell")
Winvers = WshShell.exec("%comspec% /c ver").stdout.readall
wscript.echo Winvers
WinVers = Left(WinVers,22)
wscript.echo Winvers
comp = Mid(WinVers, 21, 2)
wscript.echo comp

Select Case comp
Case "95"
GetOS = "95"

Case "98"
GetOS = "98"

Case "Mi"
GetOS = "Millenium"

Case "Mi"
GetOS = "2000"

Case "NT"
GetOS = "NT"

Case "XP"
GetOS = "XP"

Case else : GetOS = "Unknown"

End Select
wscript.Echo GetOS


This time it's working (under XP and i hope it's still working on the other OS). A little problem with case.[:o)]
Thanks for your help.

(in reply to littlebouda)
 
 
Post #: 12
 
 Re: OS detection - 5/16/2005 11:12:50 PM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
I see one problem with the case statement

quote:
Case "Mi"
GetOS = "Millenium"

Case "Mi"
GetOS = "2000"



With this, you will never get to 2000. It will be hit millenium then stop. The more information you have in your case the better chance you have of getting the correct OS. Which is one reason in my examples I use Windows xx. Also, you might want to make the returns "case insensitive" I.e. WinVers = Left(Ucase(winvers),22) that way you can always be certain something will be upper or lower case, which is something I need to do with my examples script, doing this in a production script though. This won't be a problem with the numbers but can help with the letters.

(in reply to littlebouda)
 
 
Post #: 13
 
 Re: OS detection - 5/17/2005 9:36:02 PM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
OK
I had ever corrected the detection for 2000; But i didn't written my correction here. So, here the script (with the 'case insensitive modification' )

set WshShell = WScript.CreateObject ("WScript.Shell")
Winvers = WshShell.exec("%comspec% /c ver").stdout.readall
'wscript.echo Winvers
WinVers = Left(UCase(WinVers),22)
'wscript.echo Winvers
comp = Mid(WinVers, 21, 2)
'wscript.echo comp

Select Case comp
Case "95"
GetOS = "95"

Case "98"
GetOS = "98"

Case "ME"
GetOS = "Millenium"

Case "20"
GetOS = "2000"

Case "NT"
GetOS = "NT"

Case "XP"
GetOS = "XP"

Case else : GetOS = "Unknown"

End Select
wscript.Echo GetOS

(in reply to littlebouda)
 
 
Post #: 14
 
 Re: OS detection - 5/17/2005 9:57:19 PM   
  willem

 

Posts: 12
Score: 0
Joined: 4/25/2005
From: France
Status: offline
I just tested the ver command on Windows 2003 Server (I have a french version installed) - I thought it might conflict with Windows 2000 (case '20'), it returns:

Microsoft Windows [Version 5.2.3790]

so you can add "case '[V'", but maybe not for long.

(in reply to littlebouda)
 
 
Post #: 15
 
 Re: OS detection - 5/17/2005 11:08:05 PM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
Finally i think i will stay on the script wich using registry base...

(in reply to littlebouda)
 
 
Post #: 16
 
 Re: OS detection - 5/18/2005 12:21:21 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Might I inquire why you are sticking with the reg? What issues are you experiencing with other ways of doing this?

(in reply to littlebouda)
 
 
Post #: 17
 
 Re: OS detection - 5/18/2005 12:38:02 AM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
The goal of my script is to install WMI for station with an OS < Win 2000. That's all... So, all the PC which have 2000, 2003 or XP aren't important... My goal isn't to know the exact version of 2000/ 2003 or w95...[|)]

(in reply to littlebouda)
 
 
Post #: 18
 
 Re: OS detection - 5/18/2005 1:49:01 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
If all you are wanting to do is install WMI on pc's that need it, why not just check for the presence of WMI?

systemfolder/WBEM/wbemcode.dll is the file you would look for. here is a script that should work on all os's

Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
SysDir = fso.GetSpecialFolder(1).Path
msgbox WMI

if Fso.FileExists(sysDir & "\WBEM\wbemcore.dll") then
msgbox "wmi version is : " & fso.GetFileVersion(sysDir & "\WBEM\wbemcode.dll")
Else
Msgbox "WMI is Not installed"
End If


'Edit Note, fixed for code errors.

(in reply to littlebouda)
 
 
Post #: 19
 
 Re: OS detection - 5/18/2005 1:52:44 AM   
  crazymatt

 

Posts: 296
Score: 0
Joined: 3/4/2005
From:
Status: offline
Hehe this is why people really should start with explaing what the purpose of the script is and what they gonna do with it..

Even if its fun scripting it would save us all much time. :D

/Matt

(in reply to littlebouda)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> OS detection Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts