Photo Gallery Member List Search Calendars FAQ Ticket List Log Out



Get the OS version (3 versions of script)

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Get the OS version (3 versions of script)
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Get the OS version (3 versions of script) - 8/29/2005 11:26:58 PM   
  mbouchard


Posts: 1986
Score: 18
Joined: 5/15/2003
From: USA
Status: offline
There will be 3 different versions of this script posted.

Script Name: GetOS.vbs
Version 1 Tested on WinNT4 and up
Version 2 Tested on Win2000 (requires wscript 5.6) and up
Version 3 Tested on Win2000 (updated WMI) and up

No testing done on 9x/ME

1)  Uses WshShell.Run to run Ver in the cmd (%comspec%) prompt.  The output is redirected to the temp folder whose location is taken from the temp environement variable.  CMD window is not displayed and the file is deleted after use.  Note, some newer virus scanners may prevent script from accessing files in the temp folder, happened with McAfee 8.0 where I work.

2) Similar to version 1, but uses WshShell.Exec instead.  Now file is created but the cmd window will flash as you can not supress it when using Exec.  Also, Wscript 5.6 is required, XP comes with it but you will need to upgrade other OS's.

3) Uses WMI to get the OS.  This method will allow you to differenciate between OS/Server versions and get the OS from a remote PC.  WMI is required and will need to be installed on older OS's.

< Message edited by mbouchard -- 9/1/2005 1:49:12 AM >


_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.
 
 
Post #: 1
 
 Using WshShell.Run - 8/29/2005 11:27:28 PM   
  mbouchard


Posts: 1986
Score: 18
Joined: 5/15/2003
From: USA
Status: offline
msgbox GetOS

Function GetOS()
'Will work with most versions of WSH.
'CMD window will not display.
  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 : GetOS = "W95"
    Case InStr(sResults, "Windows 98") > 1 : GetOS = "W98"
    Case InStr(sResults, "Windows Millennium") > 1 : GetOS = "WME"
    Case InStr(sResults, "Windows NT") > 1 : GetOS = "NT4"
    Case InStr(sResults, "Windows 2000") > 1 : GetOS = "W2K"
    Case InStr(sResults, "Windows XP") > 1 : GetOS = "WXP"
    Case Else : GetOS = "Unknown"
  End Select
End Function

< Message edited by mbouchard -- 9/15/2005 11:17:18 PM >


_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to mbouchard)
 
 
Post #: 2
 
 Using WshShell.Exec - 8/29/2005 11:28:18 PM   
  mbouchard


Posts: 1986
Score: 18
Joined: 5/15/2003
From: USA
Status: offline
msgbox GetOS

Function GetOS()
'Requires WSH 5.6
'CMD window will flash as there is no way to supress it when using exec.
    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 : GetOS = "W95"
       Case InStr(sResults, "Windows 98") > 1 : GetOS = "W98"
       Case InStr(sResults, "Windows Millennium") > 1 : GetOS = "WME"
       Case InStr(sResults, "Windows NT") > 1 : GetOS = "NT4"
       Case InStr(sResults, "Windows 2000") > 1 : GetOS = "W2K"
       Case InStr(sResults, "Windows XP") > 1 : GetOS = "WXP"
       Case Else : GetOS = "Unknown"
    End Select
End Function

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to mbouchard)
 
 
Post #: 3
 
 RE: Using WshShell.Exec - 8/29/2005 11:28:59 PM   
  mbouchard


Posts: 1986
Score: 18
Joined: 5/15/2003
From: USA
Status: offline
msgbox GetOS

Function GetOS()
'WMI is required for this script to function
Dim strComputer, strWMIOS
'If you only want to use locally, 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

    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 : GetOS = "2KSRV"
        Case InStr(strWmiOS, "2003, Standard") > 1 : GetOS = "2K3SRV"
        Case InStr(strWmiOS, "2003, Enterprise") > 1 : GetOS = "2K3ENTSRV"
       Case InStr(strWmiOS, "2000 Advanced Server") > 1 : GetOS = "2KADVSRV"
       Case InStr(strWmiOS, "Windows NT") > 1 : GetOS = "NT4"
       Case InStr(strWmiOS, "Windows 2000") > 1 : GetOS = "W2K"
       Case InStr(strWmiOS, "Windows XP") > 1 : GetOS = "WXP"
       Case Else : GetOS = "Unknown"
    End Select
End Function

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to mbouchard)
 
 
Post #: 4
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Get the OS version (3 versions of script) Page: [1]
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