InstanceCreationPlugins/shortcut-plugin.psm1

#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
}