Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of sj_hicks
sj_hicks
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon336697

ASKER

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.
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

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
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.