VBScript with 2 arguments - converting to a HTA

Hi guys,
I have the following vbscript which takes currently takes 2 arguments at the command line to run:
cscript <script>.vbs /srv:<computers>.txt /svc:<servicename>

The output is:

Computer Name    Service     Status
<serverA>            browser    running
<serverB>             browser   stopped
etc etc

What id like to do is:
1) Convert this to a hta
2) In the hta,
a) Have a text box that allows you to input the service name you require a status on
b) Have a Browse button that enables you to browse for the text file that has a list of computers you wish to read and run this script against
c) Have a "Run" button to execute the script

Any help greatly appreciated.
'Usage:   cscript me.vbs /srv:<servername> /svc:<servicename>
'----------------------------------------------------------------------------------------------------- cscript me.vbs /srv:<server> /svc:browser
'-- namedCmdLineArgs.vbs - example of how to pass named
'-- arguments to a script
'----------------------------------------------------------
option explicit
'----------------------------------------------------------
'-- declare the local variables to be used
'----------------------------------------------------------
dim unamedArgs, namedArgs, scriptName, serverName, serviceName
dim objWMIService, colServiceList, objService
 
'----------------------------------------------------------
'-- get the name of the running script
'----------------------------------------------------------
scriptName = wscript.scriptname
 
'----------------------------------------------------------
'-- get the named arguments
'----------------------------------------------------------
set namedArgs = wscript.arguments.named
 
'----------------------------------------------------------
'-- check to see if all the required arguments were passed
'----------------------------------------------------------
if not namedArgs.exists("srv") then
   wscript.echo "Usage: " & scriptName & " /srv:<server name> is required"
   wscript.quit
else
   serverName = namedArgs.item("srv")
end if
 
if not namedArgs.exists("svc") then
   wscript.echo "Usage: " & scriptName & " /svc:<service name> is required"
   wscript.quit
else
   serviceName = namedArgs.item("svc")
end if
 
 
 
'----------------------------------------------------------
'-- echo out the arguments
'----------------------------------------------------------
'wscript.echo "Checking server: " & serverName & vbcrlf & "Service: " & serviceName
 
'--------------------------------------------------------------------------------------------------
 
Dim objFSO, objFile
WScript.Echo "Computer Name" & vbTab & vbTab & "Service" & vbTab & vbTab & "Status"
If Right(LCase(serverName), 4) = ".txt" Then
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Const intForReading = 1
 Set objFile = objFSO.OpenTextFile(serverName, intForReading, False)
 While Not objFile.AtEndOfStream
  Output objFile.ReadLine
 Wend
 objFile.Close
Else
 Output serverName
End If
 
Sub Output(serverName)
 If Ping(serverName) = True Then
  On Error Resume Next
  Set objWMIService = GetObject("winmgmts:\\" & serverName & "\root\cimv2")
  Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name = '" & serviceName & "'")
  If Err.Number = 0 Then
   For Each objService in colServiceList
     WScript.Echo serverName & vbTab & vbTab & serviceName & vbTab & vbTab & objService.State
   Next
  Else
   WScript.Echo serverName & vbTab & vbTab & "WMI Error"
  End If
 Else
  WScript.Echo serverName & vbTab & vbTab & "Unreachable"
 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

Open in new window

LVL 1
Simon336697Asked:
Who is Participating?
 
sj_hicksConnect With a Mentor Commented:
Grr, I just posted a whole lot of into a duplicate of this question, only to lose it all when I submitted cos it has been deleted.  I'll try to remember what I wrote.

1)  Create an HTML file with the desired textboxes, button etc.
2)  Assign all the controls an ID - this will be the variable name you can reference them with from your script.
3)  Change file extension to HTA and add the HTA:Application header (see refs below)
4)  Create a <SCRIPT language=VBScript> tag in the HTA and copy in your script
5)  Move your code before Sub Output into it's own function.  Add an onclick event to your Run button to call this function.
6)  Remove unneeded code (i.e. command line parameters)
7)  Reference data in text controls using the value property  e.g. MyTextBox.value
8)  The WScript object cannot be used in HTAs.  Change WScript.echo to msgbox (or write text to a <DIV> tag).  Change WScript.CreateObject to CreateObject.

References:
HTA:Application reference:  http://msdn.microsoft.com/en-us/library/ms536495(VS.85).aspx
Microsoft Scripting Guys:  http://www.microsoft.com/technet/scriptcenter/default.mspx
HTA Helpomatic download:  http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=231d8143-f21b-4707-b583-ae7b9152e6d9
0
 
Simon336697Author Commented:
Hi sj,
Sorry to hear about that mate.
sj,
THANKS SO MUCH FOR YOUR HELP ON THIS.
Ill do my best to try and follow your kind help.
0
 
Simon336697Author Commented:
hi sj,
Ive given it a go (having just a few problems.

<html>
<head>
 
<!-- hta section below -->
 
<HTA:APPLICATION 
border="thin" 
borderStyle="normal" 
caption="yes" 
maximizeButton="yes" 
minimizeButton="yes" 
showInTaskbar="no" 
innerBorder="yes" 
navigable="yes" 
scroll="auto" 
scrollFlat="yes"
windowstate="minimize"
/>
 
<SCRIPT LANGUAGE="VBScript">
 
 
Sub Output
 
serverName = "txt_computer.Value"
serviceName = "txt_service.Value"
 
  On Error Resume Next
 
'Connect to WMI namespace:
  Set objWMIService = GetObject("winmgmts:\\" & serverName & "\root\cimv2")
 
'Create a WMI query to query for service status:
  Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name = '" & serviceName & "'")
 
  If Err.Number = 0 Then
    strHTML = "<table border='1' style='border-collapse: collapse' "  & _
        "bordercolor='#111111' width='100%' id='Table1' >"
 
   For Each objService in colServiceList
        strHTML = strHTML & "<tr>"
        strHTML = strHTML & "<td width='20%'>" & serverName & "</td>"
        strHTML = strHTML & "<td width='30%'>" & serviceName & "</td>"
        strHTML = strHTML & "<td width='50%'>" & objService.State & _
            "</td>"
   Next
 
    strHTML = strHTML & "</table>"
    DataArea.InnerHTML = strHTML
 
  Else
        strHTML = strHTML & "<tr>"
        strHTML = strHTML & "<td width='20%'>" & serverName & "</td>"
        strHTML = strHTML & "<td width='80%'>" & "WMI Error" & _
            "</td>"
    strHTML = strHTML & "</table>"
    DataArea.InnerHTML = strHTML
  End If
 
  Else
        strHTML = strHTML & "<tr>"
        strHTML = strHTML & "<td width='20%'>" & serverName & "</td>"
        strHTML = strHTML & "<td width='80%'>" & "Unreachable" & _
            "</td>"
    strHTML = strHTML & "</table>"
    DataArea.InnerHTML = strHTML
  End If
End Sub
 
 
</script>
 
<body>
	<div style="text-align: center;"><p>Enter the computer name below:</p><input type="text" id="txt_computer" name="txt_computer" size='50'></div>
	<div style="text-align: center;"><p>Enter the service to query:</p><input type="text" id="txt_service" name="txt_service" size='50'></div>
	<div style="text-align: center;"><p><input type="button" id="btn_run" name="btn_run" value="Execute"  onClick="Output"></div>
	<span id = "DataArea"></span>
</body>
</html> 

Open in new window

0
 
sj_hicksCommented:
Your problem here is your IF statement(s).  You've got:

IF
...
ELSE
...
END IF
...
ELSE ***
...
END IF

You can't have an ELSE (designated above by ***) without an If to start it.  Checkout IF syntax here: http://msdn.microsoft.com/en-us/library/5h27x7e9(VS.85).aspx
0
 
sj_hicksCommented:
VBScript documentation download available here:  http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9
It's an invaluable reference for me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.