Save-Application.ps1

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
59
<#
.Synopsis
    Saves the startup parameters for an application.
 
.Description
    Saves the startup parameters for an application and persists
    this information to disk at $home\AppData\Low\SimplyCredential\.
 
.Parameter Name
    The reference name of the application.
 
.Parameter Path
    Path to the executable.
 
.Parameter Arguments
    Arguments to be used with the executable,
    can be overridden in 'Use-Application'.
 
.Parameter NetworkOnly
    Runs the application as the current user, except for
    operations that take place over the network.
 
.Parameter AsAdmin
    Runs the application in an elevated context.
 
.Parameter WithUserProfile
    Runs the application with a loaded user profile.
 
.Parameter Force
    Use this to overwrite an existing saved application.
 
#>

function Save-Application {
    param([Parameter(Mandatory, ValueFromPipelineByPropertyName)][string]$Name
        , [Parameter(Mandatory, ValueFromPipelineByPropertyName)][string]$Path
        , [Parameter(ValueFromPipelineByPropertyName)][string]$Arguments
        , [Parameter(ValueFromPipelineByPropertyName, ParameterSetName="netonly")][switch]$NetworkOnly
        , [Parameter(ValueFromPipelineByPropertyName, ParameterSetName="user")][switch]$AsAdmin
        , [Parameter(ValueFromPipelineByPropertyName, ParameterSetName="user")][switch]$WithUserProfile
        , [switch]$Force
    )

    if(-not (Test-Path -Path $Path -PathType Leaf) -and -not (Get-Command -Name $path -ErrorAction Ignore)) { throw "'$path' is not a valid file!" }
    if($script:AppList.Keys -notcontains $name -or $Force) { 
        $script:AppList[$Name] = [PSCustomObject]@{
            Path = $Path
            Arguments = $Arguments
            NetworkOnly = $NetworkOnly.IsPresent
            AsAdmin = $AsAdmin.IsPresent
            WithUserProfile = $WithUserProfile.IsPresent
        }
        
        $script:AppList | Export-Clixml -Path $script:AppListPath -Force
    }
    else { throw "The credential '$name' already exists, use -Force to overwrite." }
    
}

Export-ModuleMember -Function Save-Application