Convert VBS to ASP or JavaScript

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
LVL 16
Who is Participating?
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.

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.

ellandrdAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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.
ellandrdAuthor Commented:
>>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?
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?
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.
ellandrdAuthor Commented:
i dont have vb6...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.