Checking and opening different browsers using wsh script

陌路散爱 提交于 2019-12-11 16:27:49

问题


hey guys i know this may sound stupid, but i am stuck with this question in my head...im really new to this wscript or vbscripting....at the time of writing i figured out how to open IE using wscript...heres the code

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

but i cant figure out how to check if firefox is installed, then open firefox, if chrome is installed, open chrome, and the same thing goes for all the browser types.....

Update:

I did a little research and thought why not check the registry for that, so i came up with this script for checking the registry, now i dont know why but this always gives the same output "key does not exists" event though i have this registry in my system

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

回答1:


Problems in your example:

  • In keyExists(), a variable named RegistryKeyExists is being used for the return value from the function when keyExists is intended.

  • The Shell object variable WshShell is never instantiated via CreateObject().

  • The value of the registry key of interest does not end with a backslash.

Here's my streamlined version of your code which I believe accomplishes your objective:

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



回答2:


Generally following code can be used to find out to get List of All Installed Software. Here I have used Message box to display this list, you can use if condition to find out desired software is installed or not............

' 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

I have tried this code on machine & found that,it just listing Firefox browser, even when i have installed chrome & IE.So this regular method wont work surely for everyone. After that I have checked registry and found that,all browser are listed on.....

HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\

So we can write code to find is is particular browser is installed or not. Following sample code to check if Chrome & Firefox is installed or not and if installed open it with URL passed

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")

Similarly you can modify this code for IE, Opera & Safari Hope this helps.......



来源:https://stackoverflow.com/questions/11357913/checking-and-opening-different-browsers-using-wsh-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!