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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display IP address and Hostname in HTA

Status
Not open for further replies.

efiftythree

IS-IT--Management
Jan 13, 2006
27
US
I was looking through some archived threads and found this: thread329-905920

Im wondering how to add the machine hostname to the display functionality of the script. Here is the code mentioned in that thread

Code:
<script language="VBScript">
Option Explicit
' On Error Resume Next

Dim colIPResults, objFile, objFSO, objNIC, objWMI, objWSHNetwork, strAddresses, strIPAddress, strWQL
Const FOR_APPENDING = 8


Sub DestroyObjects()
If IsObject(objFile) Then Set objFile = Nothing
If IsObject(objFSO) Then Set objFSO = Nothing
If IsObject(objWMI) Then Set objWMI = Nothing
If IsObject(objWSHNetwork) Then Set objWSHNetwork = Nothing
' If IsObject() Then Set = Nothing
End Sub


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSHNetwork = CreateObject("WScript.Network")
Set objWMI = GetObject("WinMGMTS:root\cimv2")
strWQL = "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"
Set colIPResults = objWMI.ExecQuery(strWQL)
For Each objNIC In colIPResults
For Each strIPAddress in objNIC.IPAddress
If strAddresses = "" Then
strAddresses = strIPAddress
Else
strAddresses = strAddresses
End If
Next
Next

If strAddresses ="0.0.0.0" Or strAddresses ="" or strAddresses = "undefined" Then
	Document.write("No Connection Detected")
Else
	Document.write "Network Address - "+ strAddresses
End If

DestroyObjects()

  </script>

Any help would be great :)
 
StrComputer = objWSHNetwork.Computername

I hope you find this post helpful.

Regards,

Mark
 
Another quick question. Im almost done with this HTA but Im getting an error from the following peice of code:

Code:
Sub Reboot
	DIM strPath
	strPath = WScript.ScriptFullName  ' script file name
	
	Set WshShell = WScript.CreateObject("WScript.Shell")
		WshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\RunDomHTA", strPath ,"REG_SZ"
	
	Set WShell = CreateObject("WScript.Shell")
		WShell.Run "shutdown -r"
End Sub

It is activated by a button and its purpose is to add a key to the RunOnce container in the registry and then reboot the PC. The error I am getting is this:

"Object Required "WScript
 
The script host for an HTA is [tt]mshta.exe[/tt] so to run an HTA without "shellexecuting" it (i.e. via the GUI desktop) you use:

[tt]mshta.exe "c:\full path and\file name.hta" param1 param2 ... paramn[/tt]

Most HTAs don't expect command line parameters though, so just leave them off.

Try using this command for your RunOnce key value.


You can't use the WScript object in your HTA because it doesn't exist in an HTA. You aren't running a WScript and you are thus not scripting WScript and you don't have the WScript/CScript script host's environment or its global objects at your disposal.

Instead you should use the VBScript version of the CreateObject() function. Strip off the [tt]WScript.[/tt] part of what you have. You can freely create a reference to an instance of the WshShell class then:

[tt]Set WshShell = CreateObject("WScript.Shell")[/tt]

That works because it does not try to use a global reference to the WScript object at all, but instead references the Windows Script Host Object Model.


Also, since you don't have WScript you cannot use its [tt]WScript.ScriptFullName[/tt] property. You need to use the "IE" (actually the MSHTA) equivalent:

[tt]document.location.pathname[/tt]

However this pathname value will be URL encoded, meaning that for example any spaces in the pathname will appear as "%20" and so on. You'll want a little URLDecode() function to process it into plain text before using it.


All of this stuff is clearly documented. See HTML and CSS and expand this node in the treeview in the left side navigation bar.


And please do youself a favor and clean up that awful original code you got from another thread. Egad! It's like a full semester course in "How to write bad VBScript.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top