首页 > 解决方案 > 暂停 vbs 代码直到 shell 脚本完成

问题描述

我正在尝试创建一个将自动安装打印机的脚本。我发现了一些我修改过的代码来给用户一些提示。我认为原件是一个批处理文件。有 4 个进程必须退出才能运行 cscript 命令。这些是我需要暂停的,直到它们完成。我认为 shell 命令中的 WaitOnReturn 选项会让他们等待,但我没有在代码中将它们标记为“需要在这里等待......
这是代码。

Dim fso
Dim Folder
Dim ProgramPath
Dim WshShell
Dim ProgramArgs
Dim WaitOnReturn
Dim intWindowStyle

'Dim objShell
'strInput = UserInput( "Enter some input:" )
strInput = MsgBox("This will install the default HP Print driver?",1,"Windows 7 Print Driver Install")
'WScript.Echo "You entered: " & strIpAddress
If strInput = 2 Then
    WScript.Echo "Please run again when you are ready"
Else '=1 Prompt for IP Address
    'WScript.Echo "You entered: " & strInput
    strIpCheck = MsgBox("Do you have the Printers IP Address?",1,"Choose options")
    If strIpCheck = 2 Then 'Does not have IP Address
        WScript.Echo "Please run again when have the IP Address"
    Else 'Start install routine
        strIpAddress = InputBox("Enter the IP Address", "IP Address")
        Set WshShell = CreateObject("WScript.Shell")
        'Create directories
        Set FSO = CreateObject("Scripting.FileSystemObject")
        If NOT (fso.FolderExists("C:\DRIVERS")) Then
            fso.CreateFolder("C:\DRIVERS")
        End If
        If NOT (fso.FolderExists("C:\SCRIPTS")) Then
            fso.CreateFolder("C:\SCRIPTS")
        End If
        'Location of Windows 7 HP print drivers
        strSourceDriver = "C:\Windows\System32\DriverStore\FileRepository\hpoa1so.inf_amd64_neutral_4f1a3f1015001339"
        'Location of Win7 built in printer scripts
        strSourceScripts = "C:\Windows\System32\Printing_Admin_Scripts\en-US"
        If (fso.FolderExists(strSourceDriver)) Then
            fso.copyFolder strSourceDriver, "C:\DRIVERS"
        End If
        If (fso.FolderExists(strSourceScripts)) Then
            fso.copyFolder strSourceScripts, "C:\SCRIPTS"
        End If
    'Delete existing printer named HP Printer   
    ProgramPath = "C:\SCRIPTS\prnmngr.vbs"
    ProgramArgs = "-d -p " & Chr(34) & "HP Printer" & Chr(34) & ""
    intWindowStyle = 1
    WaitOnReturn = true
    WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
    ProgramPath = "C:\SCRIPTS\Prnport.vbs"
    ProgramArgs = "-a -r " &  strIpAddress & "Port -h " & strIpAddress & " -o raw -n 9100"
    intWindowStyle = 1
    WaitOnReturn = true
    WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
    ProgramPath = "C:\SCRIPTS\Prndrvr.vbs"
    ProgramArgs = "-a -m " & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS"
    intWindowStyle = 1
    WaitOnReturn = true
    WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn    
'Need to wait here until the above shell process is done    
    ProgramPath = "C:\SCRIPTS\Prnmngr.vbs"
    ProgramArgs = "-a -p " & Chr(34) & "HP Printer" & Chr(34) & "-m" & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-r " &  strIpAddress
    intWindowStyle = 1
    WaitOnReturn = true
    WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn    
'Need to wait here until the above shell process is done
    End If
End If

标签: shellvbscriptpause

解决方案


所有人WshShell.Run "cscript.exe " & …, intWindowStyle, WaitOnReturn都应该等到调用的 shell 进程完成提供WaitOnReturn = true. 但是,以下简化脚本的输出(其中的分配ProgramArgs是从原始代码复制粘贴的)显示提供的参数中缺少一些空格:

strIpAddress = "10.10.10.10"

ProgramArgs = "-d -p " & Chr(34) & "HP Printer" & Chr(34) & ""
Wscript.Echo ProgramArgs
ProgramArgs = "-a -r " &  strIpAddress & "Port -h " & strIpAddress & " -o raw -n 9100"
Wscript.Echo ProgramArgs
ProgramArgs = "-a -m " & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS"
Wscript.Echo ProgramArgs
ProgramArgs = "-a -p " & Chr(34) & "HP Printer" & Chr(34) & "-m" & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-r " &  strIpAddress
Wscript.Echo ProgramArgs

输出:cscript D:\bat\SO\55303301.vbs

-d -p "HP Printer"
-a -r 10.10.10.10Port -h 10.10.10.10 -o raw -n 9100
-a -m "HP Photosmart C8100"-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS
-a -p "HP Printer"-m"HP Photosmart C8100"-r 10.10.10.10

此外,Run方法返回一个整数。您可以将进程返回代码获取到变量RetCode,然后使用检查其值是否为零(当一切正常时)

RetCode = WshShell.Run ( "cscript.exe " & _
        Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs _
  , intWindowStyle, WaitOnReturn )

推荐阅读