Public/Add-ISCrawler.ps1

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
#requires -version 4

function ModuleRoot { $PSScriptRoot | Split-Path -Parent}

<#
.SYNOPSIS
    Adds a crawler configuration to an existing configuration file
.DESCRIPTION
    The Add-ICDCrawlerToConfig cmdlet will add a valid crawler configuration to an existing configuration file.
.EXAMPLE
    New-ICDCrawler "FileConnector" "PublicFileShare" | Add-ICDCrawlerToConfig -Path "D:\IntelliSearch\ESP-1\Config\CrawlerConfig.xml"
.EXAMPLE
    Another example of how to use this cmdlet
.INPUTS
    Crawler, System.String
.OUTPUTS
    None
#>

function Add-ISCrawler {
    [CmdletBinding(DefaultParameterSetName='Parameter Set 1',
                   SupportsShouldProcess=$true,
                   PositionalBinding=$false,
                   ConfirmImpact='Medium')]
    [Alias()]
    Param (
        # Crawler objects to add to the configuration file
        [Parameter(Mandatory=$true,
                   Position=0,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false, 
                   ParameterSetName='Parameter Set 1')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias()] 
        $Objects,
        
        # Path to the configuration file
        [Parameter(Mandatory=$true,
                   Position=1,
                   ParameterSetName='Parameter Set 1')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias("ConfigFile", "File", "FilePath")]
        [string] $Path
    )
    
    begin {
        $Path = (Resolve-Path $Path).Path
        
        $xml = New-Object XML
        $xml.Load($Path)

        # Create XML namespaces for use when searching for the correct node to remove.
        # Prepend "ns:" to the node to search for
        $XmlNamespace = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
        $XmlNamespace.AddNamespace("ns", $xml.DocumentElement.NamespaceURI)

        $Crawlers = $xml.SelectNodes("//ns:Crawlers", $XmlNamespace)
    }
    
    process {
        if ($pscmdlet.ShouldProcess("Target", "Operation")) {
            foreach ($Object in $Objects) {
                Write-Verbose "Processing node: $($Object.InstanceId)"
                $Imported = $xml.ImportNode($Object.ToXml().Crawler, $true)
                [Void] $Crawlers.AppendChild($Imported)
            }
        }
    }
    
    end {
        $xml = [xml] $xml.OuterXml.Replace(' xmlns=""', "")
        $xml.Save($Path)
    }
}