Link to home
Start Free TrialLog in
Avatar of padgettbrian
padgettbrian

asked on

Integrate .VBS script into a web page

EXPERTS,

Turn this vbs into something I can run on the web, also instead of creating an actual xml file I need it to return an xml string variable. How do you do this?

Thanks!


CODE
---------------------------------------------------------------------------------
Option Explicit
Dim strComputer, objWMIService, colInstalledPrinters, objPrinter
Dim objNetwork, strDirectory, strFile, objFSO, objFolder, objShell, objTextFile, objFile

Set objNetwork = CreateObject("WScript.Network")

strDirectory = "C:\logs"
strFile = "\Printer.xml"

'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")


' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Else
   Set objFolder = objFSO.CreateFolder(strDirectory)
   WScript.Echo "Just created " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Else
   Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
   Wscript.Echo "Just created " & strDirectory & strFile
End If

set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value ForAppending = 8 ForReading = 1, ForWriting = 2

Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, 2, True)

objTextFile.WriteLine("<?xml version=""1.0"" standalone=""yes""?>")
objTextFile.WriteLine("<InstalledPrinters>")
For Each objPrinter in colInstalledPrinters
    objTextFile.WriteLine("  <Printer>")
    objTextFile.WriteLine("    <Name>" & objPrinter.Name & "</Name>")
    objTextFile.WriteLine("    <Location>" & objPrinter.Location & "</Location>")
    objTextFile.WriteLine("    <Default>" & objPrinter.Default & "</Default>")
    objTextFile.WriteLine("  </Printer>")
Next
objTextFile.WriteLine("</InstalledPrinters>")

objTextFile.Close

'Launch explorer to check file !Remove this code after testing is completed
If err.number = vbEmpty then
   Set objShell = CreateObject("WScript.Shell")
   objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If

WScript.Quit
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

It's a tough one to run from ASP as it has to be done client side (to get the local printers).  Although its possible in ASP/VBScript it may be better to do it as a JavaScript block.  The following seems the most logical to me but I don't reckon that IE will let you create the WMI object (for security purposes):

      <html>
            <head>
                  <title>Printers</title>
                  <script language="JavaScript" type="text/javascript">
                        function Page_Load()
                        {
                              var strComputer = ".";
                              var strXML = "";
                              var objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\" + strComputer + "\\root\\cimv2");
                              var colInstalledPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer");
                              
                              strXML += "<?xml version=\"1.0\" standalone=\"yes\"?>";
                              strXML += "<InstalledPrinters>";
                              
                              for (var objPrinter in colInstalledPrinters)
                              {
                                    strXML += "<Printer>";
                                    strXML += ("<Name>" + objPrinter.Name + "</Name>");
                                    strXML += ("<Location>" + objPrinter.Location + "</Location>");
                                    strXML += ("<Default>" + objPrinter.Default + "</Default>");
                                    strXML += "</Printer>";
                              }
                              
                              strXML += "</InstalledPrinters>";
            
                              document.write("[" + strXML + "]");
                              return;
                        }
                  </script>
            <head>
            <body onload="Page_Load()">
                  <p>Loading...</p>
            </body>
      </html>

HTH

J.
Avatar of padgettbrian
padgettbrian

ASKER

That just returned [] on the webpage
i was able to get this to work but I cant figure out how to get the < or > sign in vbscript. Any ideas?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>


<script language="vbscript">
Option Explicit
Dim strComputer, objWMIService, colInstalledPrinters, objPrinter
Dim objNetwork, strDirectory, strFile
Dim strPrinters

Set objNetwork = CreateObject("WScript.Network")

'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
      
For Each objPrinter in colInstalledPrinters
 
    strPrinters = strPrinters & "Name" & objPrinter.Name & "/Name" & vbCrLf
    strPrinters = strPrinters & "Location" & objPrinter.Location & "/Location" & vbCrLf
    strPrinters = strPrinters & "Default" & objPrinter.Default & "/Default" & vbCrLf
   
Next
strPrinters = strPrinters & "</InstalledPrinters>" & vbCrLf

document.write(strPrinters)

</script>

</head>
<body>

</body>
</html>
If you want to display the '<' and '>' you can try:

    document.write(Replace(Replace(Replace(strPrinters, "&", "&amp;"), "<", "&lt;"), ">", "&gt;"))

but the actual XML string should be unchanged.

J.
Holly shiz what is that??!!?!?!?!? Could you insert that monster into the code please
ASKER CERTIFIED SOLUTION
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland 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
Awh sweeeeeeeeeeeeet that worked for that piece

It returned...  Noticed that it returns all printers, any idea how to make it only return the network printers ie the ones after name that have a \\ before them???????????

<Name>Microsoft Office Document Image Writer</Name> <Location></Location> <Default>False</Default> <Name>hp psc 1310 series</Name> <Location></Location> <Default>False</Default> <Name>\\ps01\4e4x</Name> <Location></Location> <Default>True</Default> <Name>\\ps01.dell.com\4e2x</Name> <Location></Location> <Default>False</Default> <Name>\\ps01.dell.com\4e2xpcl</Name> <Location></Location> <Default>False</Default> <Name>\\ps02\1Wcolor</Name> <Location>1 West Copy Center</Location> <Default>False</Default> <Name>\\ps02.dell.com\4E2X</Name> <Location>4 East</Location> <Default>False</Default> </InstalledPrinters>
Think I need to integrate this part into the new stuff, know how?

<script language="vbscript">
Sub PopulatePrinters()
     Set WshNetwork = CreateObject("WScript.Network")
     Set Printers = WshNetwork.EnumPrinterConnections
     For i = 0 To Printers.Count - 1 Step 2
       If(inStr(1,Printers.Item(i+1),"\\") > 0) Then
          Call AddOption(Printers.Item(i+1), Printers.Item(i+1))
       End If
     Next    
End Sub
</script>
Got it JimBob, Thanks for helping out :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<script language="vbscript">
Option Explicit
Dim strComputer, objWMIService, colInstalledPrinters, objPrinter
Dim objNetwork
Dim strPrinters

Set objNetwork = CreateObject("WScript.Network")

'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
      
strPrinters = strPrinters & "<?xml version=""1.0"" standalone=""yes""?>"
strPrinters = strPrinters & "<InstalledPrinters>"
For Each objPrinter in colInstalledPrinters

 If(inStr(objPrinter.Name,"\\") > 0) Then
    strPrinters = strPrinters & "<Name>" & objPrinter.Name & "</Name>" & vbCrLf
    strPrinters = strPrinters & "<Location>" & objPrinter.Location & "</Location>" & vbCrLf
    strPrinters = strPrinters & "<Default>" & objPrinter.Default & "</Default>" & vbCrLf  
end if
Next
strPrinters = strPrinters & "</InstalledPrinters>"
document.write(Replace(Replace(Replace(strPrinters, "&", "&amp;"), "<", "&lt;"), ">", "&gt;"))

</script>
</head>
<body>
</body>
</html>