Get the OS version (3 versions of script)

Author Message
mbouchard

  • Total Posts : 2110
  • Scores: 29
  • Reward points : 0
  • Joined: 5/15/2003
  • Location: USA
  • Status: offline
Get the OS version (3 versions of script) Tuesday, August 30, 2005 12:26 AM (permalink)
0
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 on Thursday, September 01, 2005 2:49 AM>
Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.
 
#1
    mbouchard

    • Total Posts : 2110
    • Scores: 29
    • Reward points : 0
    • Joined: 5/15/2003
    • Location: USA
    • Status: offline
    Using WshShell.Run Tuesday, August 30, 2005 12:27 AM (permalink)
    0
    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 on Friday, September 16, 2005 12:17 AM>
    Mike

    For useful Scripting links see the Read Me First stickey!

    Always remember Search is your friend.
     
    #2
      mbouchard

      • Total Posts : 2110
      • Scores: 29
      • Reward points : 0
      • Joined: 5/15/2003
      • Location: USA
      • Status: offline
      Using WshShell.Exec Tuesday, August 30, 2005 12:28 AM (permalink)
      0
      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.
       
      #3
        mbouchard

        • Total Posts : 2110
        • Scores: 29
        • Reward points : 0
        • Joined: 5/15/2003
        • Location: USA
        • Status: offline
        RE: Using WshShell.Exec Tuesday, August 30, 2005 12:28 AM (permalink)
        0
        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.
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • 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
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9