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

问题描述 投票:0回答:1

我正在尝试创建一个自动安装打印机的脚本。我发现了一些我修改过的代码,可以给用户一些提示。我认为原件是一个批处理文件。有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
shell vbscript pause
1个回答
0
投票

所有WshShell.Run "cscript.exe " & …, intWindowStyle, WaitOnReturn应该等到提供WaitOnReturn = true的被叫shell进程完成。但是,以下简化脚本(其中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 )
© www.soinside.com 2019 - 2024. All rights reserved.