Wednesday, November 20, 2013

Content Source Creation using PowerShell

Hi,

In the migration process, we have a requirement i.e.  

1)To create content source through Power Shell
2)Updating the newly created content source with site collection through Power Shell
3)Running the full crawl on the newly created content source

For these we have achieved by writing the script as below

// CreateContentWithMulURLs.ps1
#Set the Microsoft.SharePoint.PowerShell cmdlets
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
    {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }

#Get the current script execution directory
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition
$InputFileName = "Content.csv"
$ContentFile = $executingScriptDirectory +"\"+ $InputFileName
$CreateContentcsv = import-csv $ContentFile
$OutputLogFileName= "Log.txt"

#Get the Log file ready
$LogDirectory=$executingScriptDirectory + "\" + $OutputLogFileName
$file = New-Item $LogDirectory -type file -force


foreach($field in $CreateContentcsv) 
{
                $SearchServiceApplicationName = $field.SearchServiceApplicationName

                $ContentSourceName = $field.ContentSourceName

                $defaultContentSourceName = $field.defaultContentSourceName

                $SiteUrl = $field.SiteUrl
}

#Remove Urls from default Content Source
try
{

    $defaultContentSource = Get-SPEnterpriseSearchCrawlContentSource -Identity $defaultContentSourceName -SearchApplication $SearchServiceApplicationName

    if($defaultContentSource.CrawlState -eq "Idle")
    {

        #If multiple addresses, remove one by one
        if($SiteUrl.Contains(","))
        {     
                        [array]$StartAddresses = $SiteUrl.Split(",")
        }
        else
        {
                        [array]$StartAddresses = $SiteUrl
        }

        if($StartAddresses.Count -ne 0)
        {
            for ($i = 0; $i -lt $StartAddresses.length; $i++)
                        {
                                        if ($defaultContentSource.StartAddresses.Exists($StartAddresses[$i]))
                                        {
                                                    $defaultContentSource.StartAddresses.Remove($StartAddresses[$i])
                                                        $LogString= $StartAddresses[$i] +" is removed from content source "+ $defaultContentSource.Name
                    add-content $file $LogString
                                        }
                                        $defaultContentSource.Update()                   
                        }
        }
    }
    else
    {
        Write-Host "$defaultContentSourceName content source is not in Idle Status. Please re-run the script later..."
        $LogString= $defaultContentSourceName + " content source is not in Idle Status. Please re-run the script later...."
        add-content $file $LogString
                Pause
        break       
    }
}
catch
{
                    #Log the error        
        $LogString= $error[0].Exception.Message;
        add-content $file  $LogString
        $error.clear;
}



#if content Source named StorePortal already exist. Updating the Content source with startaddresses
try
{
    $flag = 0

    $SearchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction SilentlyContinue

    if($SearchServiceApplication.Count -ne 0)
    {
        $ContentSources = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchServiceApplication

        $ContentSources | ForEach-Object {   
            if ($_.Name.ToString() -eq $ContentSourceName)
            {       
                            $SPContentSource = Get-SPEnterpriseSearchCrawlContentSource -Identity $ContentSourceName -SearchApplication $SearchServiceApplicationName
                            Write-Host "Content Source : $ContentSourceName already exist. Updating the Content source with start addresses..."

                            $LogString= "Content Source : "+$ContentSourceName+" already exist. Updating the Content source with start addresses..."
                            add-content $file $LogString

                            $flag = 1
                        if($StartAddresses.Count -ne 0)
                                        {
                                                        for ($i = 0; $i -lt $StartAddresses.length; $i++)
                                                        {
                                                                        if (!($SPContentSource.StartAddresses.Exists($StartAddresses[$i])))
                                                                        {
                                                                        $SPContentSource.StartAddresses.Add($StartAddresses[$i])
                                                                                        $LogString= $StartAddresses[$i] +" is added to content source "+ $SPContentSource.Name
                                        add-content $file $LogString
                                                                        }
                                                                        $SPContentSource.Update()                                                             
                                                        }
                                        }
            }
                    }
    }
}
catch
{
                #Log the error       
    $LogString= $error[0].Exception.Message;
    add-content $file  $LogString
    $error.clear;
}


#Create the new content source if content Source named testcontentsource not exist
try
{
    if($flag -eq 0)
    {
                    $SPContentSource = New-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchServiceApplication -Type SharePoint -name $ContentSourceName -StartAddresses $SiteUrl
                    $LogString= "Content Source : "+$ContentSourceName +" created"
                    add-content $file $LogString
    }
}
catch
{
                #Log the error       
    $LogString= $error[0].Exception.Message;
    add-content $file  $LogString
    $error.clear;
}


try
{
    if($SPContentSource.CrawlState -eq "Idle")
    {
        Write-Host "Starting the FullCrawl for the content source : $ContentSourceName"
        Write-Host "Please wait for while until the crawl gets completed"
        $SPContentSource.StartFullCrawl()
        do {Start-Sleep 2; Write-Host "." -NoNewline}
        While ( $SPContentSource.CrawlState -ne "CrawlCompleting")
        Write-Host "FullCrawl for the content source : $ContentSourceName completed."
        $LogString= $ContentSourceName + " Content Source FullCrawl completed."
        add-content $file $LogString
    }
}
catch
{
                #Log the error       
    $LogString= $error[0].Exception.Message;
    add-content $file  $LogString
    $error.clear;
}
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")

Pause

// Content.csv
SearchServiceApplicationName,ContentSourceName,defaultContentSourceName,SiteUrl
Search Service Application,testcontentsource,Local SharePoint sites,"http://abc:9889/"

No comments:

Post a Comment