Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call a vbscript from inside HTML

Status
Not open for further replies.

sn0rg

IS-IT--Management
Aug 5, 2005
95
0
0
GB
I'm presenting the output of my VBScript in an HTML page. What I'd like to do is place a button at the bottom of this page which executes the same script again. I'm trying the form button (see bottom of the script below) but not having any luck - nothing happens)

Can anyone help?

Code:
Const ADS_SCOPE_SUBTREE = 2
Dim objRootDSE, objUser, objFSO, objShell, strDate, strTime, strLogFileName, strDNSname, objUsrCommand, objUsrConnection, objUsrRecordSet

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set objExplorer = CreateObject("InternetExplorer.Application")
	objExplorer.Navigate "about:blank"
	objExplorer.ToolBar = 0
	objExplorer.StatusBar = 0
	objExplorer.Width= 800
	objExplorer.Height = 480
	objExplorer.Left = 0
	objExplorer.Top = 0
	objExplorer.Visible = 1
	objExplorer.Document.BGColor="#808080"
	objExplorer.Document.FGColor="#FFFFFF" 

	strInner = "<font face=""Arial""><U><Centre><B>Commencing " & wscript.scriptname & "</B></U></centre></P></P>"

	UpdateProgress

strInner = strInner & "Get all users script - Commencing run at " & Time & " on " & Date & ".</p>" 
strInner = strInner & "Records will be read from the " & strDNSDomain & " domain.</p>"

Set objUsrCommand = CreateObject("ADODB.Command")
Set objUsrConnection = CreateObject("ADODB.Connection")
objUsrConnection.Provider = "ADsDSOObject"
objUsrConnection.Open "Active Directory Provider"
objUsrCommand.ActiveConnection = objUsrConnection
objUsrCommand.CommandText = _
    "SELECT adsPath FROM 'LDAP://" & strDNSDomain & "' " _
        & "WHERE objectClass = 'User'"
objUsrCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objUsrCommand.Properties("Page Size") = 1000
objUsrCommand.Properties("Timeout") = 30
objUsrCommand.Properties("Cache Results") = True
Set objUsrRecordSet = objUsrCommand.Execute

Do Until objUsrRecordSet.EOF

For each oField in objUsrRecordSet.Fields
		set objUser = GetObject(oField.value)
	objUser.getinfo
	strUsername = objUser.samaccountname
	if instr(1,strUserName,"$",0) > 0 then
	exit for
	else
	strInner = strInner & strUsername & "</br>"
	UpdateProgress
	end if
Next

objUsrRecordSet.MoveNext

Loop

strInner = strInner & "<FORM NAME=""Form1"">"
strInner = strInner & "<INPUT TYPE=""Button"" NAME=""Button1"" VALUE=""Run Again"">"
strInner = strInner & "<SCRIPT FOR=""Button1"" EVENT=""onClick"" LANGUAGE=""VBScript"">"
strInner = strInner & "Set objShell = CreateObject(""Wscript.Shell"")"
strInner = strInner & "objShell.Run wscript.scriptfullname , 0, True"
strInner = strInner & "</SCRIPT>"
UpdateProgress
objUsrConnection.Close
Set objUsrCommand = Nothing
Set objUsrConnection = Nothing
Set objUsrRecordSet = Nothing
wscript.quit(0)


Function UpdateProgress
	with objExplorer.Document
		.body.InnerHTML = strInner 
		.parentwindow.scrollTo 0,.body.scrollHeight
	end with
End Function
 
It is unlikely wscript.scriptfullname is in scope. Try this.
>strInner = strInner & "objShell.Run wscript.scriptfullname , 0, True"
[tt]strInner = strInner & "objShell.Run [red]" & wscript.scriptfullname & "[/red] , 0, True"[/tt]
 
Thanks tsuji, but the button still does nothing.

One thing I've always wondered about is that if I view the code of the html window, it only shows "<HTML></HTML>" - could this be related?
 
If you view source and showing that, definitely it is not right. But do you mean the other results do not show up too. If yes, that's another problem and I'll take a look at other lines.
 
After looking into it again. Here is what I see as a problem.

[1] Add some line break to the script section. For other html section, line breaks (whitespaces) may not be significant, but for script section it does need to delineate script lines.

[2] My previous post is not enough. It needs a further quotes to enclose the path.
[tt]
strInner = strInner & "<FORM NAME=""Form1"">" [red]& vbcrlf[/red]
strInner = strInner & "<INPUT TYPE=""Button"" NAME=""Button1"" VALUE=""Run Again"">" [red]& vbcrlf[/red]
strInner = strInner & "<SCRIPT FOR=""Button1"" EVENT=""onClick"" LANGUAGE=""VBScript"">" [red]& vbcrlf[/red]
strInner = strInner & "Set objShell = CreateObject(""Wscript.Shell"")" [red]& vbcrlf[/red]
strInner = strInner & "objShell.Run [red]""" & [/red]wscript.scriptfullname [red]& """[/red] , 0, True" [red]& vbcrlf[/red]
strInner = strInner & "</SCRIPT>" [red]& vbcrlf[/red]
[/tt]
 
If you are going to have it display the output in HTML and want to be able to re-run the script, why not just put it into an HTA?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Err, I'm not very aware of what a HTA file is or does, or how and why I would do that?

The modus operandi here is to execute a script, have it output the results in HTML, and place a button on that page which allows the operator to rerun the script using some variables captured during the first execution, which can be passed back as arguments when clicking the buton.

If this is possible with an HTA file, please enlighten me!
 
I'm terrible at describing what things are, so....

The example below will do what I think your current script is doing. You could add an additional button to call another function/sub or modify the current one's.

What arguments where you looking at passing back to your original script and for what purpose? What additional information where you looking at retrieving and displaying?

Copy and save as .hta (i.e. GetUsers.hta):
Code:
<html>
<head>
<title>Get Users</title>
<script language="vbscript">
Option Explicit
' On Error Resume Next

Dim iTimer

Sub GetData
	' update HTML to show a message
	window.document.getElementById("DisplayData").innerHTML = "Gathering Data...."
	' timer below is needed to allow for msg above to display
	' wait 1 second and then call GetUsers sub
	iTimer = window.setInterval("GetUsers", 1000)
End Sub

Sub GetUsers
' 	On Error Resume Next
	window.clearInterval(iTimer)
	Dim objRootDSE, strDNSDomain, strHTML, objUsrCommand, objUsrConnection, objUsrRecordSet, strUsername
	
	Const ADS_SCOPE_SUBTREE = 2
	
	Set objRootDSE = GetObject("LDAP://RootDSE")
	strDNSDomain = objRootDSE.Get("DefaultNamingContext")
	
	strHTML = "Ran at: " & Time & " on " & Date & ".</p>" & _
			  "Records read from: " & strDNSDomain & " domain.</p>"
			  
	Set objUsrCommand = CreateObject("ADODB.Command")
	Set objUsrConnection = CreateObject("ADODB.Connection")
	objUsrConnection.Provider = "ADsDSOObject"
	objUsrConnection.Open "Active Directory Provider"
	objUsrCommand.ActiveConnection = objUsrConnection
	objUsrCommand.CommandText = _
	    "Select SamAccountName FROM 'LDAP://" & strDNSDomain & "' " _
	        & "Where objectClass = 'User'"
	objUsrCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
	objUsrCommand.Properties("Page Size") = 1000
	objUsrCommand.Properties("Timeout") = 30
	objUsrCommand.Properties("Cache Results") = True
	
	Set objUsrRecordSet = objUsrCommand.Execute
	Do Until objUsrRecordSet.EOF
		strUsername = objUsrRecordSet.Fields.Item("SamAccountName")
		If InStr(strUsername, "$") = 0 Then
			strHTML = strHTML & strUsername & "<br>"
		End If
		objUsrRecordSet.MoveNext
	Loop
	
	objUsrConnection.Close
	Set objUsrRecordSet = Nothing
	Set objUsrConnection = Nothing
	Set objUsrCommand = Nothing
	
	window.document.getElementById("DisplayData").innerHTML = strHTML
End Sub

</script>
<hta:application
	applicationname="Get Users"	
	border="dialog"
	borderstyle="normal"
	caption="Get Users"
	contextmenu="no"
	icon=""
	maximizebutton="no"
	minimizebutton="yes"
	navigable="no"
	scroll="auto"
	selection="no"
	showintaskbar="yes"
	singleinstance="yes"
	sysmenu="yes"
	version="1.0"
	windowstate="normal"
>
</head>
<body bgcolor="#808080">
<span id="DisplayData"></span>
<br><br>
<input type="button" value="Get Users" onclick="GetData">
</body>
</html>

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
dm4ever, thats great - opened up a new world of possibilities there. Will have a good play around.

Some of the scripts I want to run can take an hour to execute, so I want to see the HTML update dynamically. I assume I can find a way to do this in the HTA.

As for the parameters, well, some scripts prompt for an input file. I want the button to be able to remember what the input file is, so there are no more prompts following the first execution of the script/hta.
 
Code:
Dim iTimer
...
    iTimer = window.setInterval("GetUsers", 1000)
...
window.clearInterval(iTimer)
Isn't it better to use setTimeout instead? Setting an interval causes it to run every X miliseconds. Setting a time out causes it to wait X miliseconds before running.
Code:
window.setTimeout "GetUsers", 1000
setTimeout
 
Sure is, thanks Skie...funny...I don't do a lot of HTML but came across setTimeout yesterday playing with something else.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top