Public/New-ISCrawler.ps1

#requires -version 4

function ModuleRoot { $MyInvocation.ScriptName | Split-Path -Parent}
$DllPath = $(Join-Path $(ModuleRoot | Split-Path -Parent) "\Lib\IntelliSearch.dll")
Add-Type -Path $DllPath

# Scriptblock for ToXml() method of IntelliSearch.CrawlerConfiguration object.
$ToXmlScriptblock = {
    [Xml] $Template = Get-Content -Path ("$PSScriptRoot\CrawlerTemplates\$($This.ConnectorType).template.xml")
    
    $Template.Crawler.InstanceId = $This.InstanceId
    $Template.Crawler.CustomConfiguration.ICDCrawlerConfiguration.Queues.Queue.address = $This.Address
    $Template.Crawler.CustomConfiguration.ICDCrawlerConfiguration.Queues.Queue.setIdentifier = $This.InstanceId

    # If the object instance was created from an XML file, replace a few fields. Otherwise uses the ones in the template.
    if ($This.Type -eq "XmlFile") {
        $Template.Crawler.CustomConfiguration.ICDCrawlerConfiguration.Fields = $This.Fields
        $Template.Crawler.CustomConfiguration.ICDCrawlerConfiguration.DataRecordProcessing = $This.DataRecordProcessing
    }
    
    return [Xml] $Template
}

<#
.SYNOPSIS
    Creates a new crawler object
.DESCRIPTION
    The New-ISCrawler cmdlet creates a new crawler object for use in the other cmdlets in the ISPS module. The object can be inserted into the configuration files via the Add-ICDCrawlerToConfig cmdlet. See the help file of that cmdlet for instructions on how to do so.
.EXAMPLE
    New-ISCrawler MailConnector Exchange
.EXAMPLE
    New-ISCrawler FileConnector FileShare fileserver.domain.com 1337
.INPUTS
    Xml, String, int, int16
.OUTPUTS
    IntelliSearch.CrawlerConfiguration
#>

function New-ISCrawler {
    [CmdletBinding(DefaultParameterSetName='2Param',
                   PositionalBinding=$false,
                   ConfirmImpact='None')]
    [Alias()]
    [OutputType([IntelliSearch.CrawlerConfiguration])]
    Param (
        # XML input for internal module use
        [Parameter(Mandatory=$true,
                Position=0,
                ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true,
                ParameterSetName='xml')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias()] 
        $XmlInput,

        # The type of connector the object represents
        [Parameter(Mandatory=$true,
                    Position=0,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ParameterSetName='2Param')]
        [Parameter(Mandatory=$true,
                    Position=0,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ParameterSetName='4Param')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("FileConnector", "MailConnector", "NoType")]
        [Alias()] 
        [String] $Type,
        
        # The instance ID of the connector
        [Parameter(Mandatory=$true,
                    Position=1,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ParameterSetName='2Param')]
        [Parameter(Mandatory=$true,
                    Position=1,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ParameterSetName='4Param')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias()] 
        [string] $InstanceId,
        
        # The host name of the queue manager service
        [Parameter(Mandatory=$true,
                    Position=2,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ParameterSetName='4Param')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias()] 
        [string] $HostName,

        # The port for the queue manager service
        [Parameter(Mandatory=$true,
                    Position=3,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true,
                    ParameterSetName='4Param')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias()] 
        [Int16] $Port
    )
    
    begin {
    }
    
    process {

        $CrawlerObj = New-Object IntelliSearch.CrawlerConfiguration

        switch ($PSCmdlet.ParameterSetName) {
            "2Param" {
                $CrawlerObj.InstanceId = $InstanceId
                Write-Verbose "Creating crawler $($CrawlerObj.InstanceId)"
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "ConnectorType" -Value $Type
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "Address" -Value "net.tcp://localhost:10103/QueueManager" 
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "SetIdentifier" -Value $InstanceId
                $CrawlerObj.Enabled = $true
            }
            "4Param" {
                $CrawlerObj.InstanceId = $InstanceId
                Write-Verbose "Creating crawler $($CrawlerObj.InstanceId)"
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "ConnectorType" -Value $Type
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "Address" -Value "net.tcp://$($HostName):$($Port)/QueueManager" 
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "SetIdentifier" -Value $InstanceId
                $CrawlerObj.Enabled = $true
            }
            "xml"    {
                $CrawlerObj.InstanceId = $XmlInput.InstanceId
                Write-Verbose "Creating crawler $($CrawlerObj.InstanceId)"
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "ConnectorType" -Value "XmlFile"
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "Address" -Value $XmlInput.CustomConfiguration.ICDCrawlerConfiguration.Queues.Queue.address
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "SetIdentifier" -Value $XmlInput.CustomConfiguration.ICDCrawlerConfiguration.Queues.Queue.setIdentifier

                # Collecting extra configuration from the XML file
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "Fields" -Value $XmlInput.CustomConfiguration.ICDCrawlerConfiguration.Fields
                $CrawlerObj = $CrawlerObj | Add-Member -MemberType NoteProperty -PassThru -Name "DataRecordProcessing" -Value $XmlInput.CustomConfiguration.ICDCrawlerConfiguration.DataRecordProcessing
                $CrawlerObj.Enabled = $XmlInput.Enabled
            }
        }
        
        $CrawlerObj = $CrawlerObj | Add-Member -MemberType ScriptMethod -PassThru -Name "ToXml" -Value $ToXmlScriptblock

        $CrawlerObj
    }
    
    end {
    }
}