批处理命令和VB脚本命令在一个文件中

时间:2017-08-16 10:47:17

标签: batch-file vbscript

我创建了一个批处理文件,我试图创建一个.vbs文件并在其中放置一些命令。但我无法将所有命令转移到.vbs文件。

@echo off
echo:
set /a i=1
:Loop
if %i% GTR 0 (

rem create temp vbs file    
    echo Dim IPAddress > temp.vbs
    echo IPAddress="192.168.1.109" >> temp.vbs
    echo SET WshShell = WScript.CreateObject("WScript.Shell"^) >> temp.vbs
    echo WshShell.run("telnet.exe " &IPAddress) >> temp.vbs
    echo WScript.Sleep 3000 >> temp.vbs
    echo WshShell.SendKeys"qwert" >> temp.vbs
    echo WshShell.SendKeys("{Enter}") >> temp.vbs
    echo WScript.Sleep 3000 >> temp.vbs
    echo WshShell.SendKeys"asdf123" >> temp.vbs
    echo WshShell.SendKeys("{Enter}") >> temp.vbs
    echo WScript.Sleep 1000 >> temp.vbs

    echo ' Reboot Device >> temp.vbs
    echo ' WshShell.SendKeys"reboot" >> temp.vbs
    echo ' WshShell.SendKeys("{Enter}") >> temp.vbs
    echo ' WScript.Sleep 1000 >> temp.vbs
    echo strPath = WshShell.CurrentDirectory >> temp.vbs

    echo ' Close telnet session >> temp.vbs
    echo WshShell.Run "taskkill /im telnet.exe", , True  >> temp.vbs
    rem wscript C:\Users\guest\Desktop\temp.vbs
    echo Reboot Count %i%
    echo:
    set /a i+=1
    Timeout /t 90 >nul
    GOTO Loop
    ) ELSE (
    echo Out of if loop 
    )

以下是temp.vbs文件的实际输出

WshShell.run("telnet.exe " 
WScript.Sleep 3000 
WshShell.SendKeys"qwert" 
WshShell.SendKeys("{Enter}") 
WScript.Sleep 3000 
WshShell.SendKeys"asdf123" 
WshShell.SendKeys("{Enter}") 
WScript.Sleep 1000 
' Reboot Device 
' WshShell.SendKeys"reboot" 
' WshShell.SendKeys("{Enter}") 
' WScript.Sleep 1000 
strPath = WshShell.CurrentDirectory 
' Close telnet session 
WshShell.Run "taskkill /im telnet.exe", , True

temp.vbs文件的预期输出

Dim IPAddress
IPAddress="192.168.1.109"
SET WshShell = WScript.CreateObject("WScript.Shell"^)
WshShell.run("telnet.exe " &IPAddress)
WScript.Sleep 3000
WshShell.SendKeys"qwert"
WshShell.SendKeys("{Enter}")
WScript.Sleep 3000
WshShell.SendKeys"asdf123"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1000

' Reboot Device
' WshShell.SendKeys"reboot"
' WshShell.SendKeys("{Enter}")
' WScript.Sleep 1000
strPath = WshShell.CurrentDirectory

' Close telnet session
WshShell.Run "taskkill /im telnet.exe", , True

请帮助我实现这一目标。

非常感谢你。

1 个答案:

答案 0 :(得分:0)

您可以创建一个.vbs脚本,其中包含您在同一文件夹中运行并隐藏它的命令。接下来,您可以从批处理文件中运行它。