InstanceCreationPlugins/command-plugin.psm1

#requires -version 4

function ModuleRoot
{
    $MyInvocation.ScriptName | Split-Path -Parent
}

<#
.SYNOPSIS
    Plugin for executing custom shell command as defined in the ISPSInstance.config
.DESCRIPTION
    This plugin only acts on nodes with the Name attribute of "Command".
    This element is an instruction to run a custom Windows command.
    Placeholder '{0}' will be substituted by instance directory.
 
    Example from ISPSInstance.config:
    <Command
        Target="{0}\bin\someexecutablefile.exe"
        StartupArgument="-r"/>
     
.EXAMPLE
    $Splat = @{
        Node = @{
            Executable = "\bin\someexecutablefile.exe"
            StartupArgument = "-r"
        }
        InstanceName = "ENT-0"
        InstanceDirectory = "C:\ProgramData\IntelliSearch\MyInstance\"
        ComponentDirectory "C:\ProgramFiles\IntelliSearch\UrlManager.1.2.4\"
    }
    command.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 command-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 "Command")
    {
        return
    }

    Write-Verbose "Executing command"
    
    $BinaryPath = $Node.Executable -f $InstanceDirectory
    $StartupArgument = $Node.StartupArgument
   
    Start-Process -FilePath $BinaryPath -ArgumentList $StartupArgument
}