powershell - 无法从 Powershell 将 Chrome 设置为默认浏览器

标签 powershell

我正在运行以下 powershell 命令

$chromePath = "${Env:ProgramFiles(x86)}\Google\Chrome\Application\" 
$chromeApp = "chrome.exe"
$chromeCommandArgs = @('--make-default-browser')
Invoke-Expression “cmd.exe /C `"$chromePath$chromeApp`" $chromeCommandArgs”

不幸的是,当我运行它时,我收到以下错误消息。

cmd.exe : [1396:2128:0708/153347:ERROR:gpu_info_collector_win.cc(98)] Can't retrieve a valid WinSAT assessment. At line:1 char:1 + cmd.exe /C "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --make- ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: ([1396:2128:0708...SAT assessment.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError

[1396:2128:0708/153347:ERROR:shell_integration_win.cc(200)] Chrome could not be set as default browser.



什么可能导致此错误?我意识到有多种执行命令的方法,但最重要的是使用 --make-default-browser 执行 Chrome开关故障。

最佳答案

试试这个链接 poshcode ,您可以使用它在 Chrome、Firefox、Internet Explorer、Opera 和 Safari 之间切换。我已经在所有 5 个浏览器的版本上进行了测试...

  • Chrome = 39
  • 火狐 = 34
  • Internet Explorer = 11
  • 歌剧 = 11
  • Safari = 5

  • PoShCode 片段中的代码如下...
    function Set-DefaultBrowser
    {
        param($defaultBrowser)
    
        $regKey      = "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\{0}\UserChoice"
        $regKeyFtp   = $regKey -f 'ftp'
        $regKeyHttp  = $regKey -f 'http'
        $regKeyHttps = $regKey -f 'https'
    
        switch -Regex ($defaultBrowser.ToLower())
        {
            # Internet Explorer
            'ie|internet|explorer' {
                Set-ItemProperty $regKeyFtp   -name ProgId IE.FTP
                Set-ItemProperty $regKeyHttp  -name ProgId IE.HTTP
                Set-ItemProperty $regKeyHttps -name ProgId IE.HTTPS
                break
            }
            # Firefox
            'ff|firefox' {
                Set-ItemProperty $regKeyFtp   -name ProgId FirefoxURL
                Set-ItemProperty $regKeyHttp  -name ProgId FirefoxURL
                Set-ItemProperty $regKeyHttps -name ProgId FirefoxURL
                break
            }
            # Google Chrome
            'cr|google|chrome' {
                Set-ItemProperty $regKeyFtp   -name ProgId ChromeHTML
                Set-ItemProperty $regKeyHttp  -name ProgId ChromeHTML
                Set-ItemProperty $regKeyHttps -name ProgId ChromeHTML
                break
            }
            # Safari
            'sa*|apple' {
                Set-ItemProperty $regKeyFtp   -name ProgId SafariURL
                Set-ItemProperty $regKeyHttp  -name ProgId SafariURL
                Set-ItemProperty $regKeyHttps -name ProgId SafariURL
                break
            }
            # Opera
            'op*' {
                Set-ItemProperty $regKeyFtp   -name ProgId Opera.Protocol
                Set-ItemProperty $regKeyHttp  -name ProgId Opera.Protocol
                Set-ItemProperty $regKeyHttps -name ProgId Opera.Protocol
                break
            }
        } 
    
    # thanks to http://newoldthing.wordpress.com/2007/03/23/how-does-your-browsers-know-that-its-not-the-default-browser/
    
    <#
    (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice').ProgId
    (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice').ProgId
    (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice').ProgId
    #>
    
    }
    
    # Set-DefaultBrowser cr
    # Set-DefaultBrowser ff
    # Set-DefaultBrowser ie
    # Set-DefaultBrowser op
    # Set-DefaultBrowser sa
    

    关于powershell - 无法从 Powershell 将 Chrome 设置为默认浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17536405/

    相关文章:

    powershell - 与 Chocolatey 一起使用什么安装程序类型来安装 powershell 模块?

    powershell - new-itemproperty 和 set-itemproperty 有什么区别?

    .net - 使用 PowerShell 将文件设置为 'Writable'

    azure - 提取存储 blob 内的文件

    powershell - 将 null 传递给函数的强制参数

    c# - 在 Linux 容器上导入 PowerShell 二进制模块时不导出命令

    powershell - 删除所有。字符串中的(点)(最后一个除外)

    c# - 无法从 Powershell 3.0 调用 C# 类

    powershell - 使用 Powershell 脚本开始调试 Visual Studio 项目

    powershell - 使用 PowerShell 删除重复字符串