Skip to content

Instantly share code, notes, and snippets.

@Trucido
Created January 26, 2019 10:20
Show Gist options
  • Select an option

  • Save Trucido/eab4f0a2643d6fa3d3d44fe59a404c54 to your computer and use it in GitHub Desktop.

Select an option

Save Trucido/eab4f0a2643d6fa3d3d44fe59a404c54 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Starts Google Chrome apps
.DESCRIPTION
Provides a PowerShell function to start Google Chrome apps by AppId string
.PARAMETER AppId
The App ID to start
.EXAMPLE
Start-ChromeApp -AppId dfdfomllemfemmfjeichdkogfmfmbebh
.EXAMPLE
Start-ChromeApp 'icppfcnhkcmnfdhfhphakoifcfokfdhg'
.NOTES
Chrome App Ids are unique to each chrome install, the website or app must first exist in the user's browser and be a valid app Id
#>
[cmdletbinding()]
param(
[Parameter(
ValueFromPipeline=$true,
Mandatory=$true)]
[string]
$AppId,
[Parameter(
ValueFromRemainingArguments=$True)]
[string[]]
$RemainingArgs
)
$ErrorActionPreference = 'Stop'
try {
$verboseFlag = (($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) -and
($PSCmdlet.MyInvocation.BoundParameters["Verbose"] -ne $false))
$chromePath = Resolve-Path "${env:ProgramW6432}*\Google\Chrome\Application\chrome.exe" | Select-Object $_.ProviderPath -First 1
$startProcessArgs = @{}
if (Test-Path $chromePath)
{
$startProcessArgs = @{
FilePath = $chromePath
ArgumentList = @()
WorkingDir = Split-Path $chromePath
}
# Chrome App Id's are 32 characters long, as far as I can tell.
if (($null -ne $AppId) -and ($AppId.Length -ge 30))
{
# If the App Id is passed as an arg, use it as-is, else explicitly add the --app-id= flag.
if ($AppID -match '--app-id=') {
$startProcessArgs['ArgumentList'] += "$AppId"
} else {
$startProcessArgs['ArgumentList'] += "--app-id=$AppId"
}
}
if ([string]::IsNullOrWhiteSpace($startProcessArgs['ArgumentList']) -or
([string]::IsNullOrEmpty($startProcessArgs['ArgumentList']))) {
$startProcessArgs.Remove('ArgumentList')
}
if ($RemainingArgs) {
$startProcessArgs['ArgumentList'] += $RemainingArgs
}
Write-Verbose -Verbose:$verboseFlag $("Invoking Start-Process with args: $($startProcessArgs|Format-List|Out-String)")
Start-Process @startProcessArgs
}
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment