-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStart-Program
More file actions
58 lines (46 loc) · 2.58 KB
/
Start-Program
File metadata and controls
58 lines (46 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Function Start-Program {
[CmdletBinding()]
param()
DynamicParam {
## DynamicParam from https://stackoverflow.com/questions/30111408/powershell-multiple-parameters-for-a-tabexpansion-argumentcompleter
## Generate our Program list & filter to exclude apps from the list
$ProgramList = Get-StartApps | Where { $_.appid -notmatch "http://|nvidia" }
## Create Param Dictionary
$ParamDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
## Param name
$Name = "App"
## Create a container for the new parameter's various attributes, like Manditory, HelpMessage, etc that usually goes in the [Parameter()] part
$ParamAttribCollecton = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
## Create each attribute
$ParamAttrib = new-object System.Management.Automation.ParameterAttribute
$ParamAttrib.Mandatory = $True
$ParamAttrib.Position = 0
## Create ValidationSet to make tab-complete work
$arrSet = $ProgramList.Name
$ParamValSet = New-Object -type System.Management.Automation.ValidateSetAttribute($arrSet)
## Add attributes and validationset to the container
$ParamAttribCollecton.Add($ParamAttrib)
$ParamAttribCollecton.Add($ParamValSet)
## Create the actual parameter, then add it to the Param Dictionary
$MyParam = new-object -Type System.Management.Automation.RuntimeDefinedParameter($Name, [String], $ParamAttribCollecton)
$ParamDictionary.Add($Name, $MyParam)
## Return the param dictionary so the function can add the parameters to itself
return $ParamDictionary
}
begin {
## Getting the program name you selected from -app
$Data = $PsBoundParameters["App"]
}
process {
## Filters the programlist we made earlier and gets the name+guid selected from -app
$Launch = $ProgramList | Where { $_.name -eq $Data }
## Just some cruft so you see what it's doing
Write-Host "Launching " -NoNewline -ForegroundColor DarkGray
Write-Host $Launch.Name -NoNewline -ForegroundColor Green
Write-Host " from GUID " -NoNewline -ForegroundColor DarkGray
Write-Host $Launch.AppID -NoNewline -ForegroundColor Green
## Uses explorer to launch everything as it resolves the GUIDs itself
## Should really be wrapped in a try/catch but this entire thing was "see if I can"
Start-Process "explorer.exe" -ArgumentList $("shell:appsFolder\$($Launch.AppID)")
}
}