Home > Enterprise >  Open local file with query params (Using Chrome and Powershell)
Open local file with query params (Using Chrome and Powershell)

Time:10-15

I need to launch a local application (it needs some parameters in the query string) with Chrome and Powershell.

This is what I'm trying, but it doesn't work.

$MediaName = "Test_Media"

$CurrentLocation = Get-Location

$PreviewTemplateLocation = "$($CurrentLocation.Drive.Name):\\Work\_all\Tools\Script\MCD\Script\template"
$TemplateIndexLocation = Join-Path -Path $PreviewTemplateLocation -ChildPath "index.html"

[System.Diagnostics.Process]::Start("chrome.exe", "$($TemplateIndexLocation)`#media=$($MediaName) --incognito --disable-web-security --auto-open-devtools-for-tabs --user-data-dir=C:/chromeTemp")

The url should be

C:\Work_all\Tools\Script\MCD\Script\template\index.html#media=Test_Media

but what I get is

C:\Work_all\Tools\Script\MCD\Script\template\index.html#media=Test_Media

Note that I get # instead of # in the URL. Shouldn't the # character be escaped or something like that?

Thank you!!

CodePudding user response:

If you use a regular file-system path, Chrome automatically escapes URL metacharacters such as # for you (# is the escaped form of #).

Use the file:// protocol to avoid this escaping (file:/// to specify a local path).
Additionally, you can use Start-Process instead of [System.Diagnostics.Process]::Start(), for a more PowerShell-idiomatic experience.

Start-Process chrome "file:///$TemplateIndexLocation#media=$MediaName --incognito --disable-web-security --auto-open-devtools-for-tabs --user-data-dir=C:/chromeTemp"
  • Related