VBS - 如何等待Windows设置应用程序完成?

时间:2017-02-21 14:39:55

标签: windows vbscript

我正在通过VBS脚本为一组Windows10平板电脑构建自定义设置 此设置是一系列调用,可打开一些Windows设置应用程序,如下所示:

start ms-settings:dateandtime
start ms-settings:camera
....

我想这样,当然,每个命令等待前一个命令的结束 如果我使用

shell.run("ms-settings:dateandtime")
使用等待设置的

命令,我收到错误'无法等待进程' 如果我运行命令:

shell.exec("start ms-settings:dateandtime /wait")

我收到了错误:系统无法找到指定的文件 如果我使用.Run命令,则相同。

1 个答案:

答案 0 :(得分:1)

不确定这会在平板电脑中有效(我只能在桌面上测试),但您可以将其作为起点使用

Option Explicit

    Call ShowSettingsAndWait ( "ms-settings:dateandtime" )


Function ShowSettingsAndWait( setting )

    ' Default return value
    ShowSettingsAndWait = False 

    ' Resolve executable file and start required setting panel    
    Dim executable
    With WScript.CreateObject("WScript.Shell")
        executable = Replace( _ 
            .ExpandEnvironmentStrings("%systemroot%\ImmersiveControlPanel\SystemSettings.exe") _ 
            , "\", "\\" _ 
        )
        Call .Run( setting )
    End With 

    ' Wait for the process to start 
    Call WScript.Sleep( 500 )

    ' Instantiate WMI
    Dim wmi, query
    Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

    ' Search for SystemSettings executable
    Dim process, processID
    query = "SELECT * FROM Win32_Process WHERE ExecutablePath='" & executable & "'"
    processID = - 1
    For Each process In wmi.ExecQuery( query )
        processID = process.processID
    Next 
    ' If not found, leave 
    If processID < 0 Then 
        Exit Function
    End If 

    ' Request process termination events
    Dim events
    query = "Select * From __InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'"
    Set events = wmi.ExecNotificationQuery( query )

    ' Wait for the process to end
    Dim lastEvent
    Do While True
        WScript.Echo "."
        Set lastEvent = events.NextEvent
        If lastEvent.TargetInstance.ProcessID = processID Then 
            Exit Do 
        End If 
    Loop

    ' Done, everything was 
    ShowSettingsAndWait = True 
End Function