Created
June 26, 2019 15:44
-
-
Save Razzile/8ddd96b4b6a3a22499a245f7f1ec11bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Launch-App { | |
param( | |
[parameter(mandatory = $true)][ValidateNotNullOrEmpty()][string[]]$appname, | |
[switch] $admin | |
) | |
$apps = (New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | |
if ($apps) { | |
$count = 0 | |
foreach ($app in $apps) { | |
if ($app.Name -like $appname) { | |
$count++ | |
if ($admin) { | |
#"Run as administrator" action | |
$action = $app.Verbs() | ? { $_.Name.replace('&', '') -eq "Run as administrator" } | |
} | |
else { | |
# "Open" action | |
$action = $app.Verbs() | ? { $_.Name.replace('&', '') -eq "Open" } | |
} | |
if ($action) { | |
$action.DoIt() | |
} | |
else { | |
Write-Error "Application '$appname' doesn't support this action." | |
} | |
} | |
} | |
if ($count -eq 0) { | |
Write-Error "Application '$appname' Not Found." | |
} | |
} | |
else { | |
Write-Error "An unexpected error has occurred." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment