none
PowerShell that executes VBS scripts from Task Scheduler RRS feed

  • Question

  • I have a PowerShell script that starts a VBS command file to Telnet into a server and then run a series of commands. The basic commands in the VBS file follow:

    Dim oShell
    Set oShell = WScript.CreateObject("WScript.shell")
    oShell.run ("cmd.exe")
    WScript.Sleep 1000
    oShell.SendKeys ("Telnet 00.00.000.0 1234 -f E:\Scripts\Telnet_Out.txt")
    WScript.Sleep 1000

    After that, it starts running commands until done, then it logs off and exits.

    If I run this from the PowerShell prompt, a command window opens and the script runs fine. From the Task Scheduler though, the command window doesn't launch and the script appears to hang.

    From PowerShell, I am using this command to launch the VBS script:
    Start-Process $vbsCommandFile -Wait  (where $vbsCommandFile contains the full path to the script)

    Does anyone have any suggestions on how to get this configuration to launch from the Task Scheduler (with "Run whether user is logged on or not" checked)?

    Thursday, August 23, 2018 2:58 PM

Answers

  • The SendKeys method is unreliable when running interactively. The method will not work at all in a scheduled task.

    Why do you think you need SendKeys in this case? Just run the telnet program directly with the parameters you want:


    telnet -f "[path]\telnet_out.txt" 0.1.2.3 1234

    You don't even need a script, do you? just schedule the above directly - telnet is the program name, and everything after that are the parameters.


    -- Bill Stewart [Bill_Stewart]

    Thursday, August 23, 2018 3:25 PM
    Moderator

All replies

  • The SendKeys method is unreliable when running interactively. The method will not work at all in a scheduled task.

    Why do you think you need SendKeys in this case? Just run the telnet program directly with the parameters you want:


    telnet -f "[path]\telnet_out.txt" 0.1.2.3 1234

    You don't even need a script, do you? just schedule the above directly - telnet is the program name, and everything after that are the parameters.


    -- Bill Stewart [Bill_Stewart]

    Thursday, August 23, 2018 3:25 PM
    Moderator
  • You may have confirmed my suspicion in your comment that "The method won't work in a scheduled task"

    But, just incase there is another way to do this, here is a little more on what the vb script I am starting does:

    After making the telnet connection, the script launches a 4410 terminal (emulator?) on a Linux box.

    Then it starts executing the same command to test voice circuits and capture the output in the text file. There are around 100 circuits to test and it takes about 30 minutes for the vbscript to run.

    When done, the remaining PS code reads the text file and determines if certain values exceed a threshold. If so, an alert and email go out.

    The script replaces a network guy having to do this manually. My guess (for a work around) is to have him install the script and VBS file on his server and then kick it off manually and walk away while it runs (what I do now).

    Thoughts? (Thanks!)

    Thursday, August 23, 2018 5:49 PM
  • My question remains unanswered. Why are you using the SendKeys method at all?

    All you're doing with it in your original script is typing the telnet command at a cmd.exe prompt and pressing Enter.

    You do not need the SendKeys method in a VBScript to run a program.

    In fact, you don't even need a VBScript script at all. Just run the program directly - dispense with the VBScript altogether.

    If what I am saying doesn't make sense, I would recommend hiring a consultant to assist.


    -- Bill Stewart [Bill_Stewart]

    Thursday, August 23, 2018 8:38 PM
    Moderator
  • I have a PowerShell script that starts a VBS command file to Telnet into a server and then run a series of commands. The basic commands in the VBS file follow:

    This is another Rube-Goldberg script that is really unnecessary. Just run the telnet command directly in the PowerShell script.

    In PowerShell:

    Telnet 00.00.000.0 1234 -f E:\Scripts\Telnet_Out.txt

    Telnet ca not run in a scheduled task except as the current logged in user.

    There are scriptable versions of telnet available and there is a  telnet API available that can automate telnet without the need for a user.

    Look in the Gallery as there are some examples of automating a telnet connection.

    As Bill has noted, SendKeys, is totally unreliable and will not work at all in the task scheduler.  It requires a full UI visible to work.


    \_(ツ)_/


    • Edited by jrv Thursday, August 23, 2018 8:55 PM
    Thursday, August 23, 2018 8:53 PM
  • Here is a free telnet automation tool: https://github.com/ermannog/AutoTelnet

    \_(ツ)_/

    Thursday, August 23, 2018 8:56 PM