【问题标题】:Checking and opening different browsers using wsh script使用 wsh 脚本检查和打开不同的浏览器
【发布时间】:2024-01-12 18:23:02
【问题描述】:

嘿,伙计们,我知道这听起来可能很愚蠢,但我脑子里一直在思考这个问题……我对这个 wscript 或 vbscripting 真的很陌生……在撰写本文时,我想出了如何使用打开 IE wscript...这是代码

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("iexplore.exe www.bbc.co.uk", 1)

但我不知道如何检查是否安装了firefox,然后打开firefox,如果安装了chrome,打开chrome,所有浏览器类型都一样.....

更新:

我做了一些研究,想为什么不检查注册表,所以我想出了这个检查注册表的脚本,现在我不知道为什么,但这总是给出相同的输出“密钥不存在”事件我的系统中有这个注册表

keyTest = keyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox")
If keyTest = False Then
    wscript.echo "Key does not exist"
Elseif keyTest = True then
    wscript.echo "Key exists"
End if

Function keyExists (RegistryKey)
    If (Right(RegistryKey, 1) <> "\") Then
        RegistryKeyExists = false
    Else
        On Error Resume Next
        WshShell.RegRead RegistryKey
        Select Case Err

            Case 0:
                keyExists = true
            Case &h80070002:
                ErrDescription = Replace(Err.description, RegistryKey, "")
                Err.clear
                WshShell.RegRead "HKEY_ERROR\"
            If (ErrDescription <> Replace(Err.description, _
            "HKEY_ERROR\", "")) Then
                keyExists = true
            Else
                RegistryKeyExists = false
            End If
            Case Else:
                keyExists = false
        End Select
        On Error Goto 0
    End If
End Function

【问题讨论】:

    标签: shell browser vbscript wsh


    【解决方案1】:

    你的例子中的问题:

    • keyExists() 中,当打算使用keyExists 时,一个名为RegistryKeyExists 的变量被用作函数的返回值。

    • Shell 对象变量WshShell 永远不会通过CreateObject() 实例化。

    • 感兴趣的注册表项的值不以反斜杠结尾。

    这是您的代码的简化版本,我相信它可以实现您的目标:

    Option Explicit  ' programming with your seatbelt on :-)
    
    Dim keys(4)
    keys(0) = "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox"
    keys(1) = "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox\"
    keys(2) = "HKEY_LOCAL_MACHINE\Bad\Key\"
    keys(3) = "BAD\Root\On\This\Key\Causes\Exception"
    keys(4) = "HKLM\SOFTWARE\Microsoft\Internet Explorer\"
    
    On Error Resume Next
    
    Dim i, key
    For i = 0 To UBound(keys)
        key = keyExists(keys(i))
    
        If Err Then
            WScript.Echo "An exception occurred reading registry key" _
                   & " '" & keys(i) & "':" _
                   & " [" & Err.Number & "] " _
                   & Err.Description _
                   & ""
        Else
            If keyExists(keys(i)) Then
                WScript.Echo "Key *exists*: [" & keys(i) & "]"
            Else
                WScript.Echo "Key does *not* exist: [" & keys(i) & "]"
            End If
        End If
    
        WScript.Echo "--"
    Next
    
    Function keyExists (RegistryKey)
        Dim keyVal, errNum, errDesc
    
        keyExists = False
        On Error Resume Next
    
        Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
        keyVal = WshShell.RegRead(RegistryKey)
    
        Select Case Err
            Case 0
                keyExists = True
    
            Case &h80070002
                ' key does not exist
    
            Case Else
                errNum = Err.Number
                errDesc = Err.Description
                On Error GoTo 0
                Err.Raise vbObjectError + 1, "WScript.Shell", _
                   "Something went wrong reading the registry:" _
                   & " [" & Hex(errNum) & "] " & errDesc
        End Select
    
        On Error GoTo 0
        Set WshShell = Nothing
    End Function
    
    ' End
    

    【讨论】:

    • @Hmxa Mughal:睡了一夜好觉之后,它陷入了你试图对异常处理所做的事情。因此,我修改了我的示例以合并改进的异常处理并添加了几个单元测试。
    【解决方案2】:

    通常可以使用以下代码来查找获取所有已安装软件的列表。 这里我使用了Message box来显示这个列表,你可以使用if条件来判断是否安装了想要的软件............

    ' List All Installed Software
      Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
      strComputer = "."
      strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
      strEntry1a = "DisplayName"
    
    
      Set objReg = GetObject("winmgmts://" & strComputer & _
       "/root/default:StdRegProv")
      objReg.EnumKey HKLM, strKey, arrSubkeys
    
      For Each strSubkey In arrSubkeys
          intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _
          strEntry1a, strValue1)
    
          If strValue1 <> "" Then
              MsgBox  VbCrLf & "Display Name: " & strValue1
          End If
      Next
    

    我已经在机器上尝试过这段代码,发现它只是列出了 Firefox 浏览器,即使我已经安装了 chrome 和 IE。所以这种常规方法肯定对每个人都有效。之后我检查了注册表,发现所有浏览器都列在.....

    HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\
    

    所以我们可以编写代码来查找是否安装了特定的浏览器。 以下示例代码检查是否安装了 Chrome 和 Firefox,如果已安装并通过 URL 打开它

    Set WshShell = CreateObject("WScript.Shell")
    Const HKEY_LOCAL_MACHINE = &H80000002 
    strComputer = "." 
    
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv") 
    
    strKeyPath = "SOFTWARE\Clients\StartMenuInternet\chrome.exe\shell\open\command\" 
    strValueName = "" 
    oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
    If InStr(1,strValue,"chrome",vbTextCompare) Then WshShell.Run("chrome www.google.com")
    
    
    strKeyPath = "SOFTWARE\Clients\StartMenuInternet\FIREFOX.EXE\shell\open\command\" 
    strValueName = "" 
    oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
    If InStr(1,strValue,"firefox",vbTextCompare) Then WshShell.Run("firefox www.google.com")
    

    同样,您可以为 IE、Opera 和 Safari 修改此代码 希望这会有所帮助.......

    【讨论】:

    • 嘿 4M01,感谢您的回复..您发送给我的代码非常适合 firefox,即使我的系统中安装了 chrome...它仍然可以打开 firefox.. ..是否有任何我可以看到的代码或脚本,例如: if(firefox) open firefox else if(chrome) open chrome else open IE
    • 只需从最后一个代码框中复制上面的代码,给它任何名称,用.vbs extn保存并双击vbs文件