Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

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?

file.vbs
=============

Option Explicit

Dim StrComputer
strComputer = "192.168.0.2"

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
      Else
        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
  Next
  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
    Next
  Next
  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 " & _
                                         "Win32_NetworkAdapter",,48)
  For Each objItem in colItems
    GetProbedID = objItem.SystemName
  Next
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
    OutFile.WriteLine(sData)
    OutFile.Close
  End If
  Set fso = Nothing
  Set OutFile = Nothing
  WriteFile = bWrite
End Function
Avatar of nschafer
nschafer
Flag of United States of America image

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.

Neal.
Avatar of ellandrd

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of nschafer
nschafer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joeposter649
joeposter649

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.
>>OK, so the vbs file creates a report of all the software installed on the machine that calls it?

YES!

>>joeposter649

Can you help then?  i have no idea where to start?
Avatar of CyrexCore2k
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.
i dont have vb6...