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.Netw ork")
strDirectory = "C:\logs"
strFile = "\Printer.xml"
'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
' Create the File System Object
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDir ectory) Then
Set objFolder = objFSO.GetFolder(strDirect ory)
Else
Set objFolder = objFSO.CreateFolder(strDir ectory)
WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirec tory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirect ory)
Else
Set objFile = objFSO.CreateTextFile(strD irectory & 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("<?x ml version=""1.0"" standalone=""yes""?>")
objTextFile.WriteLine("<In stalledPri nters>")
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("</I nstalledPr inters>")
objTextFile.Close
'Launch explorer to check file !Remove this code after testing is completed
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shel l")
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If
WScript.Quit
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.Netw
strDirectory = "C:\logs"
strFile = "\Printer.xml"
'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
' Create the File System Object
Set objFSO = CreateObject("Scripting.Fi
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDir
Set objFolder = objFSO.GetFolder(strDirect
Else
Set objFolder = objFSO.CreateFolder(strDir
WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirec
Set objFolder = objFSO.GetFolder(strDirect
Else
Set objFile = objFSO.CreateTextFile(strD
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("<?x
objTextFile.WriteLine("<In
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("</I
objTextFile.Close
'Launch explorer to check file !Remove this code after testing is completed
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shel
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If
WScript.Quit
ASKER
That just returned [] on the webpage
ASKER
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.Netw ork")
'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper sonate}!\\ " & 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>
<!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.Netw
'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
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(Rep lace(Repla ce(strPrin ters, "&", "&"), "<", "<"), ">", ">"))
but the actual XML string should be unchanged.
J.
document.write(Replace(Rep
but the actual XML string should be unchanged.
J.
ASKER
Holly shiz what is that??!!?!?!?!? Could you insert that monster into the code please
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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\4e2x pcl</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>
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
ASKER
Think I need to integrate this part into the new stuff, know how?
<script language="vbscript">
Sub PopulatePrinters()
Set WshNetwork = CreateObject("WScript.Netw ork")
Set Printers = WshNetwork.EnumPrinterConn ections
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>
<script language="vbscript">
Sub PopulatePrinters()
Set WshNetwork = CreateObject("WScript.Netw
Set Printers = WshNetwork.EnumPrinterConn
For i = 0 To Printers.Count - 1 Step 2
If(inStr(1,Printers.Item(i
Call AddOption(Printers.Item(i+
End If
Next
End Sub
</script>
ASKER
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.Netw ork")
'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper sonate}!\\ " & 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(Rep lace(Repla ce(strPrin ters, "&", "&"), "<", "<"), ">", ">"))
</script>
</head>
<body>
</body>
</html>
<!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.Netw
'Get the printer collection
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
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,"
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(Rep
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<title>Printers</title>
<script language="JavaScript" type="text/javascript">
function Page_Load()
{
var strComputer = ".";
var strXML = "";
var objWMIService = GetObject("winmgmts:{imper
var colInstalledPrinters = objWMIService.ExecQuery("S
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.