暂停 vbs 代码直到 shell 脚本完成

Pausing vbs code until shell script finishes

我正在尝试创建一个脚本来自动安装打印机。 我找到了一些我修改过的代码来给用户一些提示。我认为原来是一个批处理文件。有 4 个进程必须 shell 到 运行 一个 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

所有 WshShell.Run "cscript.exe " & …, intWindowStyle, WaitOnReturn 都应等待,直到被调用的 shell 进程完成提供 WaitOnReturn = true。但是,以下简化脚本的输出(其中 ProgramArgs 的分配是 Copied&Pasted 来自原始代码)显示一些缺失的空格在提供的参数中:

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\SO303301.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方法return是一个整数。您可以将进程 return 代码获取到变量 RetCode,然后使用

检查其值是否为零(当一切正常时)
RetCode = WshShell.Run ( "cscript.exe " & _
        Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs _
  , intWindowStyle, WaitOnReturn )