최종 사용자의 Windows 시스템에서 동적으로 Chrome 찾기

최종 사용자의 Windows 시스템에서 동적으로 Chrome 찾기

그래서 이것에 대해 제가 생각할 수 있는 모든 곳을 검색했지만 알아낼 수 없습니다. 나는 대답이 매우 간단하기를 바랍니다. 상황은 다음과 같습니다.

최종 사용자를 위한 바로가기 링크를 만들고 있습니다. "shortcut.lnk"라고 하겠습니다. Chrome이 설치되어 있고 "myFolder"가 데스크탑에 있다고 가정할 수 있습니다. 중요한 점은 이 앱이 사용자의 기본 브라우저가 아닌 Chrome에서 열려야 한다는 것입니다. 현재 Shortcut.lnk의 "대상"은 다음과 같습니다.

%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe --app=%USERPROFILE%\Desktop\myFolder\path\to\app.html

이것은 제가 테스트한 3개의 컴퓨터에서 작동합니다. 그러나 조사 결과 Chrome이 프로그램 파일 대신 AppData 또는 다른 위치에 설치되는 경우가 있는 것으로 나타났습니다.

제 질문은 바로가기.lnk의 "대상"에 연결할 수 있는 방식으로 Windows 컴퓨터에서 Chrome이 설치된 위치를 동적으로 결정하는 방법이 있습니까?입니다.

답변1

Chrome이 설치된 위치를 동적으로 확인하는 방법이 있나요?

다음 명령은 Chrome이 설치된 위치를 확인하고 CHROMEPATH환경 변수를 이 값으로 설정합니다.

for /f "usebackq tokens=1,2,3,4,5" %a in (`reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ /s /f \chrome.exe ^| findstr Application`) do set CHROMEPATH=%c%d%e

예제 출력:

echo %CHROMEPATH%
C:\ProgramFiles(x86)\Google\Chrome\Application\chrome.exe

배치 파일에서 사용하려면 다음과 같이 백분율을 두 배로 늘려야 합니다.

for /f "usebackq tokens=1,2,3,4,5" %%a in (`reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ /s /f \chrome.exe ^| findstr Application`) do set CHROMEPATH=%%c%%d%%e

답변2

저도 같은 문제에 직면했고 @DavidPostill이 만든 스크립트가 powershell에서 작동하지 않았기 때문에 그의 답변을 바탕으로 직접 만들었습니다.

function Find-PSFilePathInRegistry {
    param (
        [string]$FileName
    )
    $str = reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ /s /f \chrome.exe | findstr Default
    # regex to find the drive letter until $FileName
    if ($str -match "[A-Z]\:.+$FileName") {
        return @{
            success = $true
            path = $Matches[0]
        }
    }
    else {
        return @{
            success = $false
            path = ""
        }
    }
}

다음과 같이 사용하십시오:

$res = Find-FilePathInRegistry "chrome.exe"
$res.success
True
$res.path
C:\Program Files\Google\Chrome\Application\chrome.exe

찾을 수 없는 경우 $res.success입니다 $false.

답변3

그래서 엔드포인트에 새로운 바로가기를 만들기 위해 애플리케이션을 찾기 위해 이것이 필요하게 되었습니다. 이 기능을 사용하면 Chrome이 때때로 두 곳에 있을 때 환경에서 Chrome 실행 파일을 찾아야 하므로 시간이 많이 절약되어 내 삶이 훨씬 쉬워졌습니다! 감사합니다! 약간의 수정을 했고 다시 공유하고 싶습니다.

<#
.SYNOPSIS
Searches the Windows registry for the path of a specified application file.

.DESCRIPTION
The Find-PSFilePathInRegistry function searches the Windows registry for the path of a specified application file. It allows you to locate the installation directory of an application by searching through the registry keys associated with installed software.

.PARAMETER FileName
Specifies the name of the application file to search for in the registry.

.OUTPUTS
Outputs a custom object with the following properties:
- Success: Indicates whether the search was successful (true/false).
- Path: The full path to the specified application file.
- AppDir: The directory where the application file is located.

.EXAMPLE
Find-PSFilePathInRegistry -FileName "chrome.exe"
Searches the Windows registry for the path of the Google Chrome executable file.

.EXAMPLE
Find-PSFilePathInRegistry -FileName "notepad.exe"
Searches the Windows registry for the path of the Notepad executable file.

.NOTES
The function searches the following registry keys:
- HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
- HKCU:\Software\Microsoft\Windows\CurrentVersion\App Paths
#>
function Find-PSFilePathInRegistry {
    param (
        [string]$FileName
    )

    # Define an array of common registry locations where application paths are stored
    $registryKeys = @(
        "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths",
        "HKCU:\Software\Microsoft\Windows\CurrentVersion\App Paths"
    )

    # Iterate through each registry key
    foreach ($key in $registryKeys) {
        if (Test-Path $key) {
            # Get the default value (which usually contains the path) for the specified file name
            $value = Get-ItemProperty -Path "$key\$FileName" -Name "(default)" -ErrorAction SilentlyContinue
            if ($value) {
                $appDir = Split-Path -Path $value.'(default)' -Parent
                return @{
                    Success = $true
                    Path = $value.'(default)'
                    AppDir = $appDir
                }
            }
        }
    }

    # If the path is not found in any of the registry keys, return failure
    return @{
        Success = $false
        Path = ""
        AppDir = ""
    }
}

관련 정보