Search code examples
vbscriptcommandcommand-line-interfacepinghta

The VBScript Command Line Conundrum?


I have an HTA where I can pass an IP (user input in an HTML text box as txtIP.value) to a vendor's management software - all that works. However, sometimes the device won't respond to a GUI request, but will respond to a ping request. I have a really nice ping command I use which passes what the ping command returns to another cmd which changes the prompt to show the times for each reply which allows me to watch multiple devices and coordinate lost or high pings. I cannot for the life of me get this command to pass to the command line. Any and all help is appreciated.

'********************************************************************
'Trying to run the following command:
'* ping -t 172.17.100.33|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 localhost>nul"
'********************************************************************
Sub TimedPing
  'Set the HTML Field value - for testing purposes
  txtIP.value = "172.17.100.33"

  strIP1 = "ping -t " 
  strIP2 = txtIP.value
  strIP3 = "|cmd /q /v /c "
  strIP4 = """(pause&pause)>nul & "
  strIP5 = "for /l %a in ()"
  strIP6 = "do (set /p "
  strIP7 = " ""data="" && echo(!time! !data!)&ping -n 2 localhost>nul"""

  Set objShell = CreateObject ("WScript.Shell")

  MsgBox("Step 1: " & strIP1 & strIP2 & strIP3 & strIP4 & strIP5 & strIP6 & strIP7)
  txtCmd1.value = strIP1 & strIP2 & strIP3 & strIP4 & strIP5 & strIP6 & strIP7  
  objShell.Run strIP1 & strIP2 & strIP3 & strIP4 & strIP5 & strIP6 & strIP7

  MsgBox("Step 2: " & "ping -t 172.17.100.33|cmd /q /v /c ""(pause&pause)>nul & for /l %a in () do (set /p ""data="" && echo(!time! !data!)&ping -n 2 localhost>nul""")
  txtCmd2.value = "ping -t 172.17.100.33|cmd /q /v /c ""(pause&pause)>nul & for /l %a in () do (set /p ""data="" && echo(!time! !data!)&ping -n 2 localhost>nul"""
  objShell.Run "ping -t 172.17.100.33|cmd /q /v /c ""(pause&pause)>nul & for /l %a in () do (set /p ""data="" && echo(!time! !data!)&ping -n 2 localhost>nul"""

  MsgBox("Step 3: " & strIP1 & strIP2)
  objShell.Run strIP1 & strIP2

  MsgBox("Step 4: CMD")
  objShell.Run "cmd"

End Sub

Step 1 compiles (HTA refreshes without an error message) and run without visible error. The DOS screen opens and closes instantly, without showing anything. I have tried to put a pause in there, but it doesn't work either.

Step 2 does the same thing.

Step 3 works fine.

Step 4 works fine.

I dumped the values I am sending the objShell.Run command to separate text boxes to compare how they match up to the original. After getting the results identical to the original command, I can copy the text from the HTA and run it in a command window without error for both step one and two, but I get nothing but a quickly closing command window from the Run command. Any ideas?


Solution

  • Maybe one day, someone will find an answer to this. Until then, the only way I could execute this command was to use a bat file (after hours of trying to not make one - I got this working with variable IP address and colors in a couple hours). Funny enough, like Geert Bellekens mentioned, I had to start the bat file with some other command (I used @echo off), and then I had to double up on the percent sign in the command itself. For completeness sake, below is the code to run call the bat file, pass some variables (IP Address and Color Theme) and then the bat file working. FYI, I used an If-Then-Else statement to check which Theme the user has selected and the second variable in the VBS script change according to the color they selected. I'll show the default.

    VBScript in HTA triggered by HTML button click.

    Sub TimedPing
    Set objShell = CreateObject ("WScript.Shell")
    objShell.Run "TimedPing.bat " & txtIP.value & " " & 07
    End Sub
    
     
    

    Bat file stored in the same directory as the HTA:

    @echo off
    title Timed Ping
    color %2
    @ping -t %1|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 localhost>nul"