检查并打开使用WSH脚本不同的浏览器(Checking and opening different

2019-10-29 04:03发布

嘿家伙,我知道这听起来很蠢,但我坚持在我的脑海这个问题......我真的新本的WScript或vbscripting ....在写作的时候我想通了如何使用WScript的打开IE浏览器.. .heres代码

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

但我无法弄清楚如何检查是否安装了Firefox,然后打开Firefox,如果安装了镀铬,打开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

Answer 1:

问题的例子:

  • 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


Answer 2:

一般来说下面的代码可以用来找出获得所有已安装软件的列表。 在这里,我已经使用消息框中显示这个列表,你可以,如果条件找到所需软件安装或不使用............

' 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.So这种常规方法无法工作一定适合每一个人。 从那以后,我已经检查注册表,结果发现,所有的浏览器都列出.....

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,歌剧和Safari浏览器希望这个代码这有助于.......



文章来源: Checking and opening different browsers using wsh script