Detect Windows Version with VB Script

This post as most of my other posts is dedicated to windows developers :)Many times you are in the scenario where you want to detect the Operating System Version on the client machine. I had this when i developed a windows application and wanted to deploy it on different platforms including Windows 2000 XP or whatever NT hahaha this works for all .

So i wanna share just in case anyone has the same scenario :
I have shown the message boxes for debug purposes . Feel free to use or modify it : D happy development

‘ Muhammad Asad Siddiqi
‘ This can be run by executing cscript and passing the file name in the command prompt
‘ Purpose = “To detect the current version of the windows and execute files for XP or NT
‘ This would be made a part of Fail Safe to decide on PULIST or TaskList commands
‘ Variables used in the script
DIM   szTempFileName
DIM   szCommand     
DIM   wShell , FileObject    
DIM   nErrorCode
DIM   remoteAppFile
DIM   szLine 
DIM   ResultNT,ResultXP1,ResultXP2
 

‘ Create the temporary file as C:\TEMP\WindowsVersion_CT.txt
szTempFileName=”C:\TEMP\WindowsVersion_CT.txt”

‘ Initialize the Windows Scripting Shell instance
Set WShell = CreateObject(“Wscript.Shell”)

‘ Initialize the Windows Scripting File instance
Set FileObject = Wscript.CreateObject(“Scripting.FileSystemObject”)

‘ DEBUG # 1 TODO – Remove This
MsgBox (“Starting”)

‘ Check if the file already exists delete it
if FileObject.FileExists(szTempFileName) then
   FileObject.DeleteFile(szTempFileName)
End if

‘ DEBUG # 1 TODO – Remove This
MsgBox (“File System Checked”)
MsgBox (szTempFileName)

‘ Execute the ver command to get the windows info detail
szCommand = “%comspec% /c ver >”& szTempFileName

‘ DEBUG # 1 TODO – Remove This
MsgBox (szCommand)

‘ Execute the command and get the error code
nErrorCode = wShell.Run (szCommand , 0, TRUE)
‘ Check if the command is ran successfully
if nErrorCode = 0 then

‘Get a handle to the file where result of ver command is written
Set remoteAppFile = FileObject.OpenTextFile( szTempFileName , 1 , TRUE )

      ‘ Loop through the file till end of file character is not encountered
      do while not remoteAppFile.AtEndOfStream

              ‘ Read the line from the file
       szLine = remoteAppFile.ReadLine 
             
              ‘ If the line we have read is not an empty line then check 
              if szLine <> “” then    

        ResultNT = InStr(szLine, “4.0”)
        ResultXP1 = InStr(szLine, “XP”)
                     ResultXP2 = InStr(szLine, “5.1”)  
          
                      if ( ResultNT <> 0) then
                 MsgBox(“Win_NT”) 
               elseif (ResultXP1 <> 0) then
                        MsgBox(“Win_XP”)
                      elseif (ResultXp2 <> 0) then
          MsgBox(“Win_XP”)
               else
          MsgBox(“Unknown Windows”)             
               end if
              End If
      loop

remoteAppFile.Close()
 if FileObject.FileExists(szTempFileName) then
     FileObject.DeleteFile(szTempFileName)
 End if

End if
 

Advertisements

3 Comments

Filed under 1

3 responses to “Detect Windows Version with VB Script

  1. 0xdeadc0de

    That piece of code deserves a place in Daily WTF.

    Have you heard of registry ? Have you heard of WMI ? You have tons of right ways to get the information needed and you choose the worse of them.

    kkthxb

    • asadsiddiqi

      Dude,
      I know that this might be the most crappy way. Please understand that certain times you have to integrate some shit with an existing legacy application “where things are done a certain way” As far as OS goes the regiestry hierarchy is different for NT based systems too but thats not the point. The point is to detect it with VB Script” ….
      Appreciate your comment though. I hope it helps some1 stuck sometime trying to figure it out with vbs.
      Take care

  2. Alexey

    Thanks for idea! I have decided to use it for that definition that at me OS has version Windows XP SP2 or above. Here so has copied on javascript:

    var _fso = new ActiveXObject(“Scripting.FileSystemObject”);
    var _shell = new ActiveXObject(“WScript.Shell”);

    WScript.Echo(IsWindowsXPSP2OrAbove() ? “Yes” : “No”);

    function IsWindowsXPSP2OrAbove()
    {
    var fileName = _shell.ExpandEnvironmentStrings(“%USERPROFILE%”) + “\\Local Settings\\Temp\\winversion.txt”;
    ExecCmd(“ver > \”” + fileName + “\””);

    var fileReader = _fso.OpenTextFile(fileName, 1, true);
    var str;
    var isXP = false;

    while (!fileReader.AtEndOfStream)
    {
    str = fileReader.ReadLine();
    if (str == “”)
    continue;

    var res = str.match(/\s(\d+\.\d+).\d+]$/)[1];
    if (res >= “05.1”)
    isXP = true;

    break;
    }
    fileReader.Close();

    if (_fso.FileExists(fileName))
    _fso.DeleteFile(fileName);

    return isXP;
    }

    function ExecCmd(cmdLine)
    {
    if (_shell.Run(“cmd /c \”” + cmdLine + “\””, 1, true) == 0)
    return true;

    return false;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s