?
Solved

VBScript with 2 arguments - converting to a HTA

Posted on 2009-04-15
5
Medium Priority
?
1,423 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Simon336697
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
sj_hicks earned 2000 total points
ID: 24153580
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
 
LVL 1

Author Comment

by:Simon336697
ID: 24153604
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
 
LVL 1

Author Comment

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

Expert Comment

by:sj_hicks
ID: 24154539
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
 
LVL 7

Expert Comment

by:sj_hicks
ID: 24154541
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

850 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