Monday, August 20, 2012

Installing PDF Filter with Powershell


Hi,

Recently we have worked on installing PDF Filter with the power shell.
Along with the script, PDFFilter64installer, pdf16 image was copied in the same folder.
Below is the code as below

//PDFFilterInstall_Batch.bat
Powershell.exe -Command Set-ExecutionPolicy "Bypass"
Powershell.exe -Command "& {E:\PDFFilter\PDFFilterScript.ps1}"
Pause

// PDFFilterScript.ps1
Add-PSSnapin "Microsoft.SharePoint.PowerShell"

$0 = $MyInvocation.MyCommand.Definition                 # Get complete file path (eg:  E:\SP2010AppServer\SetupInputs.xml)
$dp0 = [System.IO.Path]::GetDirectoryName($0)           # Get current Directory  file path  (eg:  E:\SP2010AppServer)
$bits = Get-Item $dp0 | Split-Path -Parent              # Get current Drive   (eg:  E:\)

function InstallPDFFilter()
{
                $installationPackagePath = $dp0
                $installationPackageFileName = "PDFFilter64installer.msi"
                $logoFileName = "pdf16.gif"   
    $searchServiceApplication = "EnterPrise Search Service Application"
    $filterFile = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML\DOCICON.XML"
    $filterIconPath = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\IMAGES\"

    #Install PDF iFilter package
    Write-Host -ForegroundColor White "- Installing PDF Filter..."
    try
    {
        Start-Process "$installationPackagePath\$installationPackageFileName" -ArgumentList "/passive /norestart" -WindowStyle Minimized -Wait
        If (-not $?) {throw}
    }
    catch
    {
        Write-Host -ForegroundColor Red "- Error: $LastExitCode"
                                If ($LastExitCode -eq "1") {throw "- Another instance of this application is already running"}
                                ElseIf ($LastExitCode -eq "2") {throw "- Invalid command line parameter(s)"}
                                ElseIf ($LastExitCode -eq "1001") {throw "- A pending restart blocks installation"}
                                ElseIf ($LastExitCode -eq "3010") {throw "- A restart is needed"}
                                Else {throw "- An unknown error occurred installing package"}
    }
    Write-Host -ForegroundColor White "- PDF Filter installed successfully"
    #Copy logo file to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\IMAGES\
    Copy-Item "$installationPackagePath\$logoFileName" -destination $filterIconPath

    #Write mapping entry to the DOCICON.xml file under C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML
   
    Write-Host -ForegroundColor White " - Adding pdfs as extension to docicons xml file" 
    # Load XML from file:
    $xmldata = [xml](Get-Content $filterFile)
    $xpath="Mapping[@Key='pdf']"
    $pdfMapping = $xmldata.DocIcons.ByExtension.SelectSingleNode($xpath)
    if($pdfMapping -eq $null -or $pdfMapping -eq "")
    {
        # Create new node:
        $pdfMapping = $xmldata.CreateElement("Mapping")
        $pdfMapping.SetAttribute("Key","pdf")
        $pdfMapping.SetAttribute("Value",$logoFileName)
        # Write nodes in XML:
        $xmldata.DocIcons.ByExtension.AppendChild($pdfMapping)
        $xmldata.Save($filterFile)
    }
    Write-Host -ForegroundColor White " - XML updated."
   
    #add file type to search service application
   
    Write-Host -ForegroundColor White "Adding PDF to the list of search crawl extensions in the Search Appliation..."
    $searchapp = Get-SPEnterpriseSearchServiceApplication $searchServiceApplication
    $pdfExtension = Get-SPEnterpriseSearchCrawlExtension "pdf" -SearchApplication $searchApp -ErrorVariable err -ErrorAction "SilentlyContinue"
   
    if($pdfExtension -eq $null)
    {
        $searchapp | New-SPEnterpriseSearchCrawlExtension "pdf"
        Write-Host -ForegroundColor White " - Adding PDF extension completed."
    }
    else
    {
        Write-Host -ForegroundColor White " - The PDF extension was already in the list"
    }
   
    Write-Host -ForegroundColor White " - Adding pdf entries for Sharepoint PDF Search in the registry..."
    # if (-not $(Test-Path -Path $InputFile -Type Leaf))
    if (-not $(Test-Path "HKLM:\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\Filters\.pdf"))
    {
        New-Item -path registry::'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\Filters\.pdf' | Out-Null
        New-ItemProperty -Path registry::'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\Filters\.pdf' -Name "Extension" -value ".pdf" -PropertyType string  | Out-Null
        New-ItemProperty -Path registry::'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\Filters\.pdf' -Name "Mime Types" -value "application/pdf" -PropertyType string  | Out-Null
        New-ItemProperty -Path registry::'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\Filters\.pdf' -Name "FileTypeBucket" -value "1" -PropertyType dword  | Out-Null
    }
    if (-not $(Test-Path "HKLM:\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\ContentIndexCommon\Filters\Extension\.pdf"))
    {
        New-Item -Path registry::'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\ContentIndexCommon\Filters\Extension\.pdf' | Out-Null
        New-ItemProperty -Path registry::'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\ContentIndexCommon\Filters\Extension\.pdf' -name "(Default)" -Value "{E8978DA6-047F-4E3D-9C78-CDBE46041603}" -PropertyType string | Out-Null
    }
   
   
    #Restart services
    Write-Host -ForegroundColor White "- Restart SharePoint Timer Service..."
    IISReset
    restart-service SPTimerV4
    Write-Host -ForegroundColor White "- Timer Service restarted successfully"
}

InstallPDFFilter

Finally after executing the script you can see the PDF icon in the crawl extensions as below

No comments:

Post a Comment