Solved

Reading a tab delimited text file through a hta

Posted on 2009-06-30
8
398 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
0
Comment
Question by:Simon336697
  • 5
  • 3
8 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 24751925
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

0
 
LVL 1

Author Comment

by:Simon336697
ID: 24752219
Rob,
Hi mate hope you are well bud :>)

Will do and let you know.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 24752264
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


0
 
LVL 1

Author Comment

by:Simon336697
ID: 24752271
Rob, ill leave this one open, and create a new question.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 24761651
Hey, it's probably easier to go with the NSLookup output to get the IP address.  I noticed you've got a few other questions open along these lines.  If you've got DNS issues, then the IP Address given by the Ping command may not be reliable.  Can you post an example NSLookup command, and the corresponding output, for a hostname that has a DNS record issue?  We could then use that IP address instead, and if there's multiples, we can try both.

Let me know if that suits, or if you've already got a solution from one of the other questions, and we can incorporate that if you like.

Regards,

Rob.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 24761862
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.
0
 
LVL 1

Author Closing Comment

by:Simon336697
ID: 31598551
Rob youre the best.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24762039
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now