首頁 > 軟體

bat檔案與Vbs檔案之間的常用操作(獲取使用者輸入,執行VBS檔案)

2022-11-17 14:00:03

bat檔案

set /P StrInput="輸入數位:"
echo 輸入的數位為%StrInput%
set /P Flg="是否執行(y/n):"
IF "%Flg%" equ "y" (
  echo 執行命令
  cscript abc.vbs "%StrInput%"
)

注意:

等於號(=)之間不能有空格,不然會出錯。

判斷值大小最好使用equ之類。

條件判斷後的括號的有空格。

 VBS檔案

獲取外部引數

寫檔案

WebAPI操作

日期與TimeStamp變換

Dim WshShell
Dim CurDir
Dim oParam

'取引數
Set oParam = WScript.Arguments

if oParam.Count>0 Then

Else
    WScript.Quit
End if

'獲取當前路徑
Set WshShell = WScript.CreateObject("WScript.Shell")
CurDir = WshShell.CurrentDirectory

'寫檔案操作
Function OutputData(filename)
    Dim objFSOW
    Dim objFileW

    Set objFSOW = WScript.CreateObject("Scripting.FileSystemObject")
    Set objFileW = objFSOW.OpenTextFile(filename,2,True)
    
    objFileW.Write(filename)
    objFileW.Write(vbCrLf)
    objFileW.Write(vbTab)

    Set objFileW = Nothing
    Set objFSOW =Nothing

End Function

'WebAPI操作
'params = "{""method"":""get"",""ID"":""12""}"
Function RequestAPI(url,params)
    Dim oHttp
    
    Set oHttp = CreateObject("MSXML2.ServerXMLHTTP")
    on error resume next
    
    oHttp.Open "POST",url,False
   
    If Err Then
        RequestAPI = Err.Description
    End If
    On Error Goto 0

    oHttp.SetRequestHeader "Content-Type","application/json"
    oHttp.Send params

    If oHttp.readyState<>4 Then
       oHttp.waitForResponse(10)
    End If

    RequestAPI = oHttp.ResponseText

    Set oHttp = Nothing

End Function

'TimeStamp -> Date
Function FormatDate(timestamp)
   FormatDate = DateAdd("s",CLng(timestamp),"01/01/1970 00:00:00")
End Function

'Date ->TimeStamp
Function DateToTimeStamp(dateValue)
   DateToTimeStamp = DateDiff("s","01/01/1970 00:00:00",dateValue)
End Function

vbs伺服器bat檔案,window下批次處理操作:bat檔案中呼叫vbs

@echo "kill CRT process"
taskkill /F /IM SecureCRT.exe
::ping 127.0.0.1 -n 30
echo "start vos through CRT"
echo off
d:
cd D:toolsSecureCRT_x86
start SecureCRT.EXE /SCRIPT D:Secure_vosStart_vos.vbs
echo "start vos through CRT end"
ping 127.0.0.1 -n 4
pause

上述程式碼的意思是:在.bat檔案中執行Start_vos.vbs檔案

批次處理執行檔案之前首先需要下載SecureCRT.exe檔案

Start_vos.vbs檔案內容如下:

On Error Resume Next
Dim result
If crt.Session.Connected Then crt.Session.Disconnect
' connected to the '172.21.1.114'
Set objtab = crt.Session.ConnectInTab("/SSH2 /PASSWORD root root@172.21.1.114",True)
' Capture error code and description (if any)
nError = Err.Number
strErr = Err.Description
' Now, tell the script host that it should handle errors as usual now:
On Error Goto 0
' change tab name
objtab.caption = "Gdb_vos"
' enter the folder
crt.Screen.Send "cd /home/test" & chr(13)
crt.Screen.WaitForString chr(27) & "[32m(none) /home/test" & chr(27) & "[m # "
End Sub

上述程式碼主要是將手動輸入的過程使用VBS指令碼進行自動化
首先:Set objtab = crt.Session.ConnectInTab("/SSH2 /PASSWORD root123 root@172.11.1.15",True)
採用SSH2的方式連線linuxIP地址172.21.1.114,密碼是root123,使用者名稱是rootobjtab.caption = "Start_vos"
將當前連線頁面重新命名為Start_voscrt.Screen.Send "cd /home/test" & chr(13)輸入cd /home/test並回車
crt.Screen.WaitForString chr(27) & "[32m(none) /home/test" & chr(27) & "[m # "等待頁面下一行的內容為chr(27) & "[32m(none) /home/test" & chr(27) & "[m # "

到此這篇關於bat檔案與Vbs檔案之間的常用操作(獲取使用者輸入,執行VBS檔案)的文章就介紹到這了,更多相關bat獲取使用者輸入,執行VBS檔案內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com