InstanceCreationPlugins/shortcut-plugin.psm1

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#requires -version 4

function ModuleRoot
{
    $MyInvocation.ScriptName | Split-Path -Parent
}
$PrivPath = $(Join-Path $(ModuleRoot | Split-Path -Parent) "\Private")
. "$PrivPath\New-Shortcut.ps1"

<#
.SYNOPSIS
    Plugin for creating shortcuts as defined in the ISPSInstance.config
.DESCRIPTION
    This plugin only acts on nodes with the Name attribute of "Shortcut".
    This element is an instruction to create an application shortcut or a URL shortcut.
    The shortcut will be placed in the %progdata%\IntelliSearch\<InstanceName>\<ShortcutName> folder
    The target path should be expanded and the shortcut path would look something like this:
    C:\ProgramFiles\IntelliSearch\<packageName>\bin\someexecutablefile.exe -f C:\ProgramData\IntelliSearch\<InstanceName>\someconfigfile.exe.config
 
    Both the StartupArgument and ConfigurationFile parameters are optional, but the ConfigurationFile parameter will not be used unless the StartupArgument is specified.
    The {0} in the StartupArgument parameter is a placeholder for the configuration path.
 
    Example from ISPSInstance.config:
    <Shortcut
        Target="\bin\someexecutablefile.exe"
        ConfigurationFile="\cfg\someexecutable.exe.config"
        StartupArgument="-f {0}"
        ShortcutName="Some Name"/>
     
.EXAMPLE
    $Splat = @{
        Node = @{
            "Name" = "Shortcut"
            "Target" = "\bin\someexecutablefile.exe"
            ConfigurationFile = "\cfg\someexecutable.exe.config"
            StartupArgument = "-f {0}"
            ShortcutName = "Some Name"
        }
        InstanceName = "This is ignored"
        InstanceDirectory = "C:\ProgramData\IntelliSearch\MyInstance\"
        ComponentDirectory "C:\ProgramFiles\IntelliSearch\IntelliSearch.Server.IndexManager.4.0.2\"
    }
    shortcut.plugin @Splat
.PARAMETER Node
    The XML node currently being iterated over
.PARAMETER InstanceName
    A string with the name of the instance. This plugin ignores this parameter.
.PARAMETER InstanceDirectory
    The root directory of the new instance
.PARAMETER ComponentDirectory
    The root directory of the installed component
.INPUTS
    System.String
.OUTPUTS
    None
#>

function shortcut-plugin
{
    param (
        # XML node from ISPSInstance.config
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "XML node from ISPSInstance.config")]
        $Node,
    
        # The name representing the new instance
        [Parameter(
            Mandatory = $true,
            Position = 1,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Instance name")]
        [ValidateNotNullOrEmpty()]
        [string] $InstanceName,
        
        # Path to the directory of the new instance
        [Parameter(
            Mandatory = $true,
            Position = 2,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Path to the directory of the new instance")]
        [ValidateNotNullOrEmpty()]
        [ValidateScript( {Test-Path -Path $_ -PathType Container})]
        [string] $InstanceDirectory,
    
        # Path to the component nuget package directory
        [Parameter(
            Mandatory = $true,
            Position = 3,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Path to the component nuget package directory")]
        [ValidateNotNullOrEmpty()]
        [ValidateScript( {Test-Path -Path $_ -PathType Container})]
        [string] $ComponentDirectory
    )

    if ($Node.Name -ne "Shortcut")
    {
        return
    }

    Write-Verbose "Creating new shortcut"
    $ShortcutName = $Node.ShortcutName
    if ($ShortcutName -notmatch '.+\.(?:lnk|url)$')
    {
        $ShortcutName = $ShortcutName + ".lnk"
    }

    if ($ShortcutName -match '.+\.lnk$')
    {
        $ShortcutTarget = Join-Path $ComponentDirectory $Node.Target
        if ($Node.StartupArgument)
        {
            $ShortcutArguments = $Node.StartupArgument -f (Join-Path $InstanceDirectory $Node.ConfigurationFile)
        }
    }
    elseif ($ShortcutName -match '.+\.url$')
    {
        $ShortcutTarget = $Node.Target
        $ShortcutArguments = $null
    }

    $ShortcutDestination = Join-Path $InstanceDirectory $ShortcutName

    if ($ShortcutArguments)
    {
        return New-Shortcut -Target $ShortcutTarget -Destination $ShortcutDestination -Arguments $ShortcutArguments
    }

    New-Shortcut -Target $ShortcutTarget -Destination $ShortcutDestination
}