Convert VBS to ASP or JavaScript

Posted on 2006-06-07
Last Modified: 2008-02-01
i was giving a .vbs file and i need to run it using either ASP, ASP.NET or Javascript but i cant code in ASP or ASP.NET and dont know what to change for it to work in Javascript.

Can somebody provide a helping hand please to convert it?


Option Explicit

Dim StrComputer
strComputer = ""

strComputer = Trim(strComputer)
If strComputer = "" Then strComputer = "."

Dim sCompName : sCompName = GetProbedID(StrComputer)

Dim sFileName
sFileName = strComputer & "_software_list.txt"

Dim s : s = GetAddRemove(strComputer)

If WriteFile(s, sFileName) Then
'optional prompt for display
'If MsgBox("Finished processing.  Results saved to " & sFileName & _
'vbcrlf & vbcrlf & "Do you want to view the results now?", _
'4 + 32, sTitle) = 6 Then
'WScript.CreateObject("WScript.Shell").Run sFileName, 9
'End If
End If

Function GetAddRemove(sComp)
  'Function credit to Torgeir Bakken
  Dim cnt, oReg, sBaseKey, iRC, aSubKeys
  Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
  Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
              sComp & "/root/default:StdRegProv")
  sBaseKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
  iRC = oReg.EnumKey(HKLM, sBaseKey, aSubKeys)

  Dim sKey, sValue, sTmp, sVersion, sDateValue, sYr, sMth, sDay

  For Each sKey In aSubKeys
    iRC = oReg.GetStringValue(HKLM, sBaseKey & sKey, "DisplayName", sValue)
    If iRC <> 0 Then
      oReg.GetStringValue HKLM, sBaseKey & sKey, "QuietDisplayName", sValue
    End If
    If sValue <> "" Then
      iRC = oReg.GetStringValue(HKLM, sBaseKey & sKey, _
                                "DisplayVersion", sVersion)
      If sVersion <> "" Then
        sValue = sValue & " Ver: " & sVersion
        sValue = sValue & vbTab
      End If
      iRC = oReg.GetStringValue(HKLM, sBaseKey & sKey, _
                                "InstallDate", sDateValue)
      If sDateValue <> "" Then
        sYr =  Left(sDateValue, 4)
        sMth = Mid(sDateValue, 5, 2)
        sDay = Right(sDateValue, 2)
        'some Registry entries have improper date format
        On Error Resume Next
        sDateValue = DateSerial(sYr, sMth, sDay)
        On Error GoTo 0
        If sdateValue <> "" Then
          sValue = sValue '& vbTab & "Installed: " & sDateValue
        End If
      End If
      sTmp = sTmp & sValue & vbcrlf
    cnt = cnt + 1
    End If
  sTmp = BubbleSort(sTmp)
  GetAddRemove = sTmp
End Function

Function BubbleSort(sTmp)
  'cheapo bubble sort
  Dim aTmp, i, j, temp
  aTmp = Split(sTmp, vbcrlf)  
  For i = UBound(aTmp) - 1 To 0 Step -1
    For j = 0 to i - 1
      If LCase(aTmp(j)) > LCase(aTmp(j+1)) Then
        temp = aTmp(j + 1)
        aTmp(j + 1) = aTmp(j)
        aTmp(j) = temp
      End if
  BubbleSort = Join(aTmp, vbcrlf)
End Function

Function GetProbedID(sComp)
  Dim objWMIService, colItems, objItem
  Set objWMIService = GetObject("winmgmts:\\" & sComp & "\root\cimv2")
  Set colItems = objWMIService.ExecQuery("Select SystemName from " & _
  For Each objItem in colItems
    GetProbedID = objItem.SystemName
End Function

Function GetDTFileName()
  dim sNow, sMth, sDay, sYr, sHr, sMin, sSec
  sNow = Now
  sMth = Right("0" & Month(sNow), 2)
  sDay = Right("0" & Day(sNow), 2)
  sYr = Right("00" & Year(sNow), 4)
  sHr = Right("0" & Hour(sNow), 2)
  sMin = Right("0" & Minute(sNow), 2)
  sSec = Right("0" & Second(sNow), 2)
  GetDTFileName = sMth & sDay & sYr & "_" & sHr & sMin & sSec
End Function

Function WriteFile(sData, sFileName)
  Dim fso, OutFile, bWrite
  bWrite = True
  Set fso = CreateObject("Scripting.FileSystemObject")
  On Error Resume Next
  Set OutFile = fso.OpenTextFile(sFileName, 2, True)
  'Possibly need a prompt to close the file and one recursion attempt.
  If Err = 70 Then
    Wscript.Echo "Could not write to file " & sFileName & ", results " & _
                 "not saved." & vbcrlf & vbcrlf & "This is probably " & _
                 "because the file is already open."
    bWrite = False
  ElseIf Err Then
    WScript.Echo err & vbcrlf & err.description
    bWrite = False
  End If
  On Error GoTo 0
  If bWrite Then
  End If
  Set fso = Nothing
  Set OutFile = Nothing
  WriteFile = bWrite
End Function
Question by:ellandrd
    LVL 19

    Expert Comment

    What exactly is this script supposed to do?  Much of this would work unaltered in ASP, however some of the functions may have problems.

    The WriteFile Function - Are you trying to write to the Web Server or to the client computer?  In ASP, the file system object will be writing to the Server, but I'm not sure if this is what you intend.

    Your references to wscript will need to be looked at.  You can create a wscript object in ASP, but it needs to be done similar to the way you are creating the FileSystemObject in the WriteFile function.

    In the GetProbedID function, you are using getObject.  I've not tried using that in ASP and am not sure if it is valid.

    The biggest thing to think about though is that ASP is Server Side Code.  I have a feeling that the original script is designed to be run on a client computer (although I'm not sure of that).  All of the functions that run will run on the server, not the client.  

    I hope I've at least given you some thoughts on where to start.

    LVL 16

    Author Comment

    the vbs code was giving to my boss by another developer.  it lists all the installed software and its version on a machine by passing an IP address.

    so for exmaple, file.vbs exists on our intranet server and i run file.vbs passing it my client IP address(internal as its on an intranet).

     well it will list all the software ive installed with the version.  but i cannot run this .vbs file so i need convert it to javascript or ASP so i can run it... but i can code in either
    LVL 19

    Accepted Solution

    OK, so the vbs file creates a report of all the software installed on the machine that calls it?  

    Here's where there is a problem.  As I mentioned above ASP is server side code.  It has no ability to gather information from a client computer, only from the server.  So ASP will not work for what you are trying to do.  

    Javascript is generally used for client-side code, so the possibility exits that you may be able to this with Javascript, but I wouldn't hold out a lot of hope as I don't think that any browser is going to allow a call to the registry from a Javascript in a webpage.  (I could be wrong here there might be a way, but if so I don't know of it)

    I don't use ASP.Net, I keep thinking I should start heading that direction, but it's too easy to fall back on what you already know.  In any case I think you will find the same problem with ASP, that you are looking at Server Side code, not client side.

    Sorry I can't be of more help.

    LVL 15

    Expert Comment

    Nothing jumps out at me that won't work in ASP other than "WScript.Echo" should be changed to "response.write".  
    If it doesn't work in ASP it's probably a permissions issue.
    LVL 16

    Author Comment

    >>OK, so the vbs file creates a report of all the software installed on the machine that calls it?



    Can you help then?  i have no idea where to start?
    LVL 14

    Expert Comment

    ellandrd your best bet would be to look into writing an ActiveX component. These components can be downloaded through browsers but don't have the security restrictions that ASP/JavaScript do. ASP.Net might have a way of doing this but most likely it uses a library component itself so you're better off just writing your own. Do you have Visual Basic 6.0?
    LVL 15

    Expert Comment

    Start by changing "WScript.Echo" to "response.write" and let us know what error you get.    Changing IIS to run under your id would help troubleshoot a permssions problem assuming you can run the vbs file under your ID.
    LVL 16

    Author Comment

    i dont have vb6...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now