InstanceCreationPlugins/copy-plugin.psm1

#requires -version 4
<#
.SYNOPSIS
    Plugin for copying files as defined in the ISPSInstance.config
.DESCRIPTION
    This plugin only acts on nodes with the Name attribute of "Copy".
    It will recursively copy items from the component directory matching the "$Node.Source" property to the "$Node.Destination" in the instance directory.
    Existing files in the destination directory will be overwritten.
 
    Example from ISPSInstance.config:
    <Copy
        Source="\cfg"
        Destination="\cfg" />
.EXAMPLE
    $Splat = @{
        Node = @{
            "Name" = "Copy"
            "Source" = "/cfg/*.*"
            "Destination" = "/cfg"
        }
        InstanceName = "This is ignored"
        InstanceDirectory = "C:\ProgramData\IntelliSearch\MyInstance\"
        ComponentDirectory "C:\ProgramFiles\IntelliSearch\IntelliSearch.Server.IndexManager.4.0.2\"
    }
    copy.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 copy-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 "Copy")
    {
        return
    }
    
    $CopySplat = @{
        Path        = Join-Path $ComponentDirectory $Node.Source
        Destination = Join-Path $InstanceDirectory $Node.Destination
        Force       = $true
        Recurse     = $true
    }

    # Make sure the source path actually exists
    if (-not (Test-Path -Path (Split-Path $CopySplat.Path)))
    {
        throw "Source path does not exist or is not valid. Got: $($CopySplat.Path)"
    }

    if (-not (Test-Path -Path $CopySplat.Destination -IsValid))
    {
        throw "Destination path is not valid. Got: $($CopySplat.Destination)"
    }

    Copy-Item @CopySplat
}