We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Reading a tab delimited text file through a hta

Simon336697
Simon336697 asked
on
Medium Priority
520 Views
Last Modified: 2012-05-07
Hi guys i hope you are well.
Guys i have attached a screen dump of what im trying to achieve.
Id like to create a vbs (hta) file that:
1) Reads a tab delimited text file 2 columns wide (computers.txt)
2) For each computer name listed in computers.txt (column 2), do what the objectives i have in the screen dump.

Any help greatly appreciated.
color.jpg
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi Simon,

Give this a shot....I haven't been able to test all of it, but see how it goes...

Regards,

Rob.
<head>
<title>System Information Report</title>
<HTA:APPLICATION 
     APPLICATIONNAME="System Information Report"
     BORDER="thin"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="normal"
>
</head>
 
<script language="VBScript">
 
Dim dictComputers
Const intForReading = 1
 
Sub Window_OnLoad
	intWidth = 800
	intHeight = 600
	Me.ResizeTo intWidth, intHeight
	Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
	span_progress.InnerHTML = ""
	span_results.InnerHTML = ""
	Set dictComputers = CreateObject("Scripting.Dictionary")
End Sub
 
Sub Run_Script
	If txt_textfile.Value = "" Then
		MsgBox "Please select a text file."
		txt_textfile.Focus
	Else
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objFile = objFSO.OpenTextFile(txt_textfile.Value, intForReading, False)
		While Not objFile.AtEndOfStream
			strLine = objFile.ReadLine
			If InStr(strLine, vbTab) > 0 Then
				dictComputers.Add Split(strLine, vbTab)(0), Split(strLine, vbTab)(1)
			End If
		Wend
		objFile.Close
		arrComputers = Split(strComputers, ";")
		strHTML = "<table width='100%' border='1'>"
		span_results.InnerHTML = strHTML
		strHTML = strHTML & "<tr><th>Case</th><th>System</th><th>Ping</th><th>Net Use</th><th>Alerter</th><th>Browser</th><th>Server</th></tr>"
		span_results.InnerHTML = strHTML
		intCount = 0
		For Each strCase In dictComputers
			intCount = intCount + 1
			span_progress.InnerHTML = "Scanning " & intCount & " of " & dictComputers.Count
			boolPingStatus = Ping(dictComputers(strCase))
			If boolPingStatus = True Then
				strBGColor = "white"
				boolPingStatus = "Y"
				boolNetUseStatus = TestNetUseCommand(strComputer)
				If boolNetUserStatus = "X" Then strBGColor = "orange"
				strAlerterStatus = Get_Service_State(strComputer, "alerter")
				strBrowserStatus = Get_Service_State(strComputer, "browser")
				strServerStatus = Get_Service_State(strComputer, "lanmanserver")
			Else
				strBGColor = "red"
				boolPingStatus = "X"
				boolNetUseStatus = "X"
				strAlerterStatus = "X"
				strBrowserStatus = "X"
				strServerStatus = "X"
				span_results.InnerHTML = strHTML
			End If
			strHTML = strHTML & "<tr>" & _
				"<td bgcolor='" & strBGColor & "'>" & strCase & "</td>" & _
				"<td bgcolor='" & strBGColor & "'>" & dictComputers(strCase) & "</td>" & _
				"<td bgcolor='" & strBGColor & "'>" & boolPingStatus & "</td>" & _
				"<td bgcolor='" & strBGColor & "'>" & boolNetUseStatus & "</td>" & _
				"<td bgcolor='" & strBGColor & "'>" & strAlerterStatus & "</td>" & _
				"<td bgcolor='" & strBGColor & "'>" & strBrowserStatus & "</td>" & _
				"<td bgcolor='" & strBGColor & "'>" & strServerStatus & "</td>" & _
				"</tr>"
			span_results.InnerHTML = strHTML
		Next
	End If
End Sub
 
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function
 
Function TestNetUseCommand(strComputer)
	Set objShell = CreateObject("WScript.Shell")
	strDrive = Get_Next_Available_Drive_Letter
	strCommand = "net use " & strDrive & ": \\" & strComputer & "\IPC$"
	intReturn = objShell.Run(strCommand, 0, True)
	If intReturn = 0 Then
		strStatus = "Y"
		strCommand = "net use " & strDrive & ": /delete"
		objShell.Run strCommand, 0, True
	Else
		strStatus = "X"
	End If
	TestNetUseCommand = strStatus
End Function
 
Function Get_Service_State(strComputer, strServiceName)
	Const wbemFlagReturnImmediately = &h10
	Const wbemFlagForwardOnly = &h20
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT State FROM Win32_Service WHERE Name='" & strServiceName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	For Each objItem In colItems
		strState = objItem.State
	Next
	If Err.Number <> 0 Then
		strState = "N/A"
		Err.Clear
	End If
	On Error GoTo 0
	Get_Service_State = strState
End Function
 
Function Get_Next_Available_Drive_Letter
	' Use the ASCII code to be converted to a letter of the alphabet
	' starting from "E" which is character number 69
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	strFirstAvailable = ""
	For intChar = 69 To 90
		If objFSO.DriveExists(Chr(intChar) & ":") = False Then
			strFirstAvailable = Chr(intChar) & ":"
			Exit For
		End If
	Next
	If strFirstAvailable <> "" Then
		Get_Next_Available_Drive_Letter = Chr(intChar) & ":"
	Else
		Get_Next_Available_Drive_Letter = ""
	End If
End Function
 
</script>
 
<body>
<table width='90%' height='90%' align='center' border='0'>
	<tr height="100">
		<td align='center' colspan="2">
			<h3>System Information Report</h3>
		</td>
	</tr>
	<tr height="100">
		<td>
			Text file:
		</td>
		<td>
			<input type="file" size="40" id="txt_textfile" name="txt_textfile">&nbsp;&nbsp;&nbsp;<button name="btn_runscript" accesskey="R" onclick="Run_Script"><u>R</u>un Script</button>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<span id="span_progress"></span><br>
			<span id="span_results"></span>
		</td>
	</tr>
</table>
</body>

Open in new window

Author

Commented:
Rob,
Hi mate hope you are well bud :>)

Will do and let you know.

Author

Commented:
Rob youre the best.
That does work.
There is a dns issue where i am, so im getting a lot of issues using the hostname to ping and net use.
Im having much more luck using the ip address.

Rob,

Is there any way to somehow alter your brilliant work here, and retrieve the ip address for each host by using the nslookup command?

Or,

Id be happy with the following mate (always happy with you anyway :>)).

Create another hta (separate question probably):

----------------------------------------------------- createtextfileofips.hta
Please load a text file of computer names ________
Run script...

This would read the text file of computer names, and for each, return the ip address of the host, and write it to a new file called ips.txt


Author

Commented:
Rob, ill leave this one open, and create a new question.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Rob,

I TOTALLY agree.
And youre right. We do have DNS issues.
PInging by the ip address/es returned by the nslookup id rather :>)

As im now at home, i havent got a production example Rob.
Most of our client systems would have 1 ip address returned, but i spose it would be good to factor those in with multiple ips.

The points are yours mate here.
Ill post another question called "NSLOOKUP IP" Rob :>)
Always great communicating with you mate.

Author

Commented:
Rob youre the best.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
OK, no problem. Thanks for the grade. Let me know the URL of the new question, and I'll take a look at it soon.

Regards,

Rob.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.