Chrome dynamisch auf dem Windows-Rechner des Endbenutzers finden

Chrome dynamisch auf dem Windows-Rechner des Endbenutzers finden

Ich habe also überall danach gesucht, wo ich nur hinkam, und kann es nicht herausfinden. Ich hoffe, die Antwort ist ganz einfach. Hier ist die Situation:

Ich erstelle einen Verknüpfungslink für einen Endbenutzer. Wir nennen ihn „shortcut.lnk“. Wir können davon ausgehen, dass Chrome installiert ist und dass „myFolder“ auf seinem Desktop liegt. Wichtig ist, dass diese App in Chrome geöffnet werden muss, nicht im Standardbrowser des Benutzers. Derzeit habe ich Folgendes als „Ziel“ von shortcut.lnk:

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

Dies funktioniert auf den 3 Maschinen, auf denen ich es getestet habe. Bei meinen Recherchen ist mir jedoch aufgefallen, dass Chrome manchmal in AppData oder an anderen Orten installiert wird, anstatt in den Programmdateien.

Meine Frage ist nun: Gibt es eine Möglichkeit, dynamisch zu ermitteln, wo Chrome auf Ihrem Windows-Computer installiert ist, sodass ich es an das „Ziel“ von shortcut.lnk anhängen kann?

Antwort1

Gibt es eine Möglichkeit, dynamisch zu bestimmen, wo Chrome installiert ist?

Der folgende Befehl ermittelt, wo Chrome installiert ist, und setzt die CHROMEPATHUmgebungsvariable auf diesen Wert:

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

Beispielausgabe:

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

Zur Verwendung in einer Batchdatei müssen Sie die Prozentsätze wie folgt verdoppeln:

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

Antwort2

Ich bin auf dasselbe Problem gestoßen und da das Skript von @DavidPostill unter Powershell nicht funktionierte, habe ich auf Grundlage seiner Antwort mein eigenes erstellt.

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 = ""
        }
    }
}

Verwenden Sie es wie folgt:

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

Falls nicht gefunden, $res.successwird angezeigt $false.

Antwort3

Ich brauchte dies also, um Anwendungen zu finden und neue Verknüpfungen auf Endpunkten zu erstellen. Diese Funktion hat mir SO VIEL ZEIT gespart, da ich die ausführbare Chrome-Datei in einer Umgebung finden musste, in der Chrome manchmal an zwei Orten vorhanden ist, was mir das Leben so viel leichter gemacht hat! Vielen Dank! Ich habe einige Optimierungen daran vorgenommen und möchte sie gerne mit Ihnen teilen, also...

<#
.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 = ""
    }
}

verwandte Informationen