【问题标题】:Start chrome with URL and kiosk mode using vbs使用 vbs 以 URL 和 kiosk 模式启动 chrome
【发布时间】:2019-06-07 21:32:46
【问题描述】:

我想使用 VBS 代码运行带参数的 Chrome,这是我的程序:

  1. 检查 chrome 进程是否未运行:

  2. 如果是这样...使用带有信息亭模式参数的 URL 启动 Chrome。

Set objShell = CreateObject("WScript.Shell") i = 1

strPC = "." strProfile = objShell.ExpandEnvironmentStrings("%LOCALAPPDATA%")


strPath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
--kiosk www.stackoverflow.com"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPC & "\root\cimv2")

Do While i = 1
    booChrome = False

    Set colProcesses = objWMIService.ExecQuery _
        ("Select * From Win32_Process")
    For Each objItem in colProcesses
        strProcessName = objItem.Caption
        If strProcessName = "chrome.exe" Then booChrome = True
    Next
    
    If booChrome = False Then objShell.Run(Chr(34) & strPath & Chr(34))
    WScript.Sleep 300000 Loop

此代码失败,但如果我从 URL 中删除参数,它的启动 Chrome 会很好

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    我在 Windows 10(32 位)上创建并测试了这个 vbscript。

    因此,您应该根据自己的目的对其进行修改。

    Option Explicit
    Dim ProcessPath,KioskMode,URL
    ProcessPath = "%ProgramFiles%\Google\Chrome\Application\chrome.exe"
    KioskMode = "--kiosk"
    URL = "https://stackoverflow.com/questions/56458100/start-chrome-with-url-and-kiosk-mode-using-vbs"
    'Exit if the script is already running.
    If AppPrevInstance() Then   
        MsgBox "There is an existing proceeding",VbExclamation,"There is an existing proceeding"    
        WScript.Quit   
    Else   
        Do   
            Call Main(Array(ProcessPath))
            Call Pause(1) 'Sleep for 1 seconde
        Loop   
    End If   
    '**************************************************************************
    Sub Main(colProcessPaths)   
        Dim ProcessPath   
        For Each ProcessPath In colProcessPaths     
            CheckProcess(ProcessPath)   
        Next   
    End Sub   
    '**************************************************************************
    Sub CheckProcess(ProcessPath)   
        Dim ProcessName : ProcessName = StripProcPath(ProcessPath)   
        With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
            With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " &  CommandLineLike(ProcessName))   
                If .Count = 0 Then    
                    With CreateObject("WScript.Shell")  
                        .Run DblQuote(ProcessPath) & " " & KioskMode & " " & URL
                    End With    
                Else    
                    Exit Sub    
                End if   
            End With   
        End With   
    End Sub   
    '**************************************************************************
    'Checks whether a script with the same name as this script is already running
    Function AppPrevInstance()   
        With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
            With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
                AppPrevInstance = (.Count > 1)   
            End With   
        End With   
    End Function   
    '**************************************************************************
    Sub Pause(Secs)    
        Wscript.Sleep(Secs * 1000)    
    End Sub   
    '**************************************************************************
    Function StripProcPath(ProcessPath)   
        Dim arrStr : arrStr = Split(ProcessPath, "\")   
        StripProcPath = arrStr(UBound(arrStr))   
    End Function   
    '**************************************************************************
    Function CommandLineLike(ProcessPath)   
        ProcessPath = Replace(ProcessPath, "\", "\\")   
        CommandLineLike = "'%" & ProcessPath & "%'"   
    End Function
    '**************************************************************************
    'Function to add the double quotes into a variable
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '**************************************************************************
    

    【讨论】:

      猜你喜欢
      • 2015-08-31
      • 1970-01-01
      • 2014-09-18
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2020-01-01
      • 2017-02-28
      相关资源
      最近更新 更多