使用wsh脚本检查并打开不同的浏览器

时间:2012-07-06 07:42:08

标签: shell browser vbscript wsh

嘿伙计们,我知道这可能听起来很愚蠢,但我仍然坚持这个问题在我的脑海中...我真的很新的这个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

2 个答案:

答案 0 :(得分:0)

示例中的问题:

  • keyExists()中,当RegistryKeyExists出现时,名为keyExists的变量用于函数的返回值。

  • 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

答案 1 :(得分:0)

通常,以下代码可用于查找获取所有已安装软件的列表。 这里我用Message框来显示这个列表,你可以使用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

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

HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\

所以我们可以编写代码来查找是否安装了特定的浏览器。 以下示例代码检查Chrome&amp;是否已安装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和amp;的这段代码。苹果浏览器 希望这有助于.......