domingo, 17 de junio de 2012

Automatización de la instalación de la búsqueda de contenido de PDF para SharePoint Server 2010

He extraído de SPAutoInstaler el script que automatiza el proceso de instalación del PDF para la inclusión de los contenidos del PDF en instalaciones existentes de SharePoint que no lo tengan instalados. 

El Script modificado solicita los siguientes parámetros:

param (

[string]$iFilter = "$(Read-Host '-ConfigureAdobeDPFiFilter [e.g. $true | $false]')"

,[string]$Icon = "$(Read-Host '-ConfigureAdobePDFIcon [e.g. $true | $false]')"

,[string]$MIMEType = "$(Read-Host '-ConfigureAdobePDFMIMEType [e.g. $true | $false]')"

,[string]$WebAppUrl = "$(Read-Host '-WebAppUrl [e.g. http://fqdn]')"

)

ConfigureAdobeDPFiFilter : El primer parámetro si se pasa el valor verdadero extraerá del sitio de Adobe Acrobat el Filtro y lo instala en el servidor, incluyendo la modificación en el registro de Windows .  También agrega la extensión del archivo en el servicio de Search.

ConfigureAdobePDFIcon : El segundo parámetro extrae el Icono del sitio de Adobe y lo agrega al archivo DOCICON.XML.

MIMEType y WebAppUrl :  Estos dos parámetros van juntos ya que es necesario agregar la Aplicación Web para agregar el MIMEType que permitirá abrir los documentos desde el navegador.

 

NOTA: Para ejecutar este script es necesario tener acceso a Internet para que automáticamente descargue los archivos y los instale.  Puede pasar un parámetro a la vez y revisar cada proceso que realiza o bien ejecutarlos todos a la vez.  La línea de comando a ejecutar es la siguiente, asumiendo que el script se nombre de la siguiente forma:  ConfigurePDFSearchAndIcon.ps1

c:\>.\ConfigurePDFSearchAndIcon.ps1 –ConfigureAdobeDPFiFilter $true –ConfigureAdobePDFIcon $false –ConfigureAdobePDFMIMEType $false –WebAppUrl http://miportal.miempresa.com

 

El resto del script es el siguiente:

function main()

{

# check if the SharePoint DLLs are installed

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}

if ($snapin -eq $null)

{

Write-Verbose "Loading SharePoint Powershell Snapin"

Add-PSSnapin "Microsoft.SharePoint.Powershell"

}

Write-Host -ForegroundColor White " - Configuring PDF file search, display and handling..."

$SharePointRoot = "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\14"

$SourceFileLocations = @("$bits\PDF\","$bits\AdobePDF\","$env:TEMP\")

If ($iFilter -eq $true)

{

$PDFiFilterUrl = "http://download.adobe.com/pub/adobe/acrobat/win/9.x/PDFiFilter64installer.zip"

Write-Host -ForegroundColor White " - Configuring PDF file iFilter and indexing..."

# Look for the installer or the installer zip in the possible locations

ForEach ($SourceFileLocation in $SourceFileLocations)

{

If (Get-Item $($SourceFileLocation+"PDFFilter64installer.msi") -ErrorAction SilentlyContinue)

{

Write-Host -ForegroundColor White " - PDF iFilter installer found in $SourceFileLocation."

$iFilterInstaller = $SourceFileLocation+"PDFFilter64installer.msi"

Break

}

ElseIf (Get-Item $($SourceFileLocation+"PDFiFilter64installer.zip") -ErrorAction SilentlyContinue)

{

Write-Host -ForegroundColor White " - PDF iFilter installer zip file found in $SourceFileLocation."

$ZipLocation = $SourceFileLocation

$SourceFile = $SourceFileLocation+"PDFiFilter64installer.zip"

Break

}

}

# If the MSI hasn't been extracted from the zip yet then extract it

If (!($iFilterInstaller))

{

# If the zip file isn't present then download it first

If (!($SourceFile))

{

Write-Host -ForegroundColor White " - PDF iFilter installer or zip not found, downloading..."

If (Confirm-LocalSession)

{

$ZipLocation = $env:TEMP

$DestinationFile = $ZipLocation+"\PDFiFilter64installer.zip"

Import-Module BitsTransfer | Out-Null

Start-BitsTransfer -Source $PDFiFilterUrl -Destination $DestinationFile -DisplayName "Downloading Adobe PDF iFilter..." -Priority High -Description "From $PDFiFilterUrl..." -ErrorVariable err

If ($err) {Write-Warning " - Could not download Adobe PDF iFilter!"; Pause "exit"; break}

$SourceFile = $DestinationFile

}

Else {Write-Warning " - The remote use of BITS is not supported. Please pre-download the PDF install files and try again."}

}

Write-Host -ForegroundColor White " - Extracting Adobe PDF iFilter installer..."

$Shell = New-Object -ComObject Shell.Application

$iFilterZip = $Shell.Namespace($SourceFile)

$Location = $Shell.Namespace($ZipLocation)

$Location.Copyhere($iFilterZip.items())

$iFilterInstaller = $ZipLocation+"\PDFFilter64installer.msi"

}

Try

{

Write-Host -ForegroundColor White " - Installing Adobe PDF iFilter..."

Start-Process -FilePath "msiexec.exe" -ArgumentList "/i $iFilterInstaller /passive /norestart" -NoNewWindow -Wait

}

Catch {$_}

If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)

{

Write-Host -ForegroundColor White " - Loading SharePoint Powershell Snapin..."

$PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell

}

Write-Host -ForegroundColor White " - Setting PDF search crawl extension..."

$searchApplications = Get-SPEnterpriseSearchServiceApplication

If ($searchApplications)

{

ForEach ($searchApplication in $searchApplications)

{

Try

{

Get-SPEnterpriseSearchCrawlExtension -SearchApplication $searchApplication -Identity "pdf" -ErrorAction Stop | Out-Null

Write-Host -ForegroundColor White " - PDF file extension already set for $($searchApplication.DisplayName)."

}

Catch

{

New-SPEnterpriseSearchCrawlExtension -SearchApplication $searchApplication -Name "pdf" | Out-Null

Write-Host -ForegroundColor White " - PDF extension for $($searchApplication.DisplayName) now set."

}

}

}

Else {Write-Warning " - No search applications found."}

Write-Host -ForegroundColor White " - Updating registry..."

If ((Get-Item -Path Registry::"HKLM\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\Filters\.pdf" -ErrorAction SilentlyContinue) -eq $null)

{

$item = New-Item -Path Registry::"HKLM\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\Filters\.pdf"

$item | New-ItemProperty -Name Extension -PropertyType String -Value "pdf" | Out-Null

$item | New-ItemProperty -Name FileTypeBucket -PropertyType DWord -Value 1 | Out-Null

$item | New-ItemProperty -Name MimeTypes -PropertyType String -Value "application/pdf" | Out-Null

}

If ((Get-Item -Path Registry::"HKLM\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\ContentIndexCommon\Filters\Extension\.pdf" -ErrorAction SilentlyContinue) -eq $null)

{

$registryItem = New-Item -Path Registry::"HKLM\SOFTWARE\Microsoft\Office Server\14.0\Search\Setup\ContentIndexCommon\Filters\Extension\.pdf"

$registryItem | New-ItemProperty -Name "(default)" -PropertyType String -Value "{E8978DA6-047F-4E3D-9C78-CDBE46041603}" | Out-Null

}

##Write-Host -ForegroundColor White " - Restarting SharePoint Foundation Search Service..."

##Restart-Service SPSearch4

If ((Get-Service OSearch14).Status -eq "Running")

{

Write-Host -ForegroundColor White " - Restarting SharePoint Search Service..."

Restart-Service OSearch14

}

Write-Host -ForegroundColor White " - Done configuring PDF iFilter and indexing."

}

If ($Icon -eq $true)

{

$PDFIconUrl = "http://helpx.adobe.com/content/dam/kb/en/837/cpsid_83709/attachments/AdobePDF.png"

$DocIconFolderPath = "$SharePointRoot\TEMPLATE\XML"

$DocIconFilePath = "$DocIconFolderPath\DOCICON.XML"

Write-Host -ForegroundColor White " - Configuring PDF Icon..."

$pdfIcon = "AdobePDF.png"

If (!(Get-Item $SharePointRoot\Template\Images\$pdfIcon -ErrorAction SilentlyContinue))

{

ForEach ($SourceFileLocation in $SourceFileLocations)

{

# Check each possible source file location for the PDF icon

$CopyIcon = Copy-Item -Path $SourceFileLocation\$pdfIcon -Destination $SharePointRoot\Template\Images\$pdfIcon -PassThru -ErrorAction SilentlyContinue

If ($CopyIcon)

{

Write-Host -ForegroundColor White " - PDF icon found at $SourceFileLocation\$pdfIcon"

Break

}

}

If (!($CopyIcon))

{

Write-Host -ForegroundColor White " - `"$pdfIcon`" not found; downloading it now..."

Import-Module BitsTransfer | Out-Null

Start-BitsTransfer -Source $PDFIconUrl -Destination "$SharePointRoot\Template\Images\$pdfIcon" -DisplayName "Downloading PDF Icon..." -Priority High -Description "From $PDFIconUrl..." -ErrorVariable err

If ($err) {Write-Warning " - Could not download PDF Icon!"; Pause "exit"; break}

}

If (Get-Item $SharePointRoot\Template\Images\$pdfIcon) {Write-Host -ForegroundColor White " - PDF icon copied successfully."}

Else {Throw}

}

$xml = New-Object XML

$xml.Load($DocIconFilePath)

If ($xml.SelectSingleNode("//Mapping[@Key='pdf']") -eq $null)

{

Try

{

Write-Host -ForegroundColor White " - Creating backup of DOCICON.XML file..."

$backupFile = "$DocIconFolderPath\DOCICON_Backup.xml"

Copy-Item $DocIconFilePath $backupFile

Write-Host -ForegroundColor White " - Writing new DOCICON.XML..."

$pdf = $xml.CreateElement("Mapping")

$pdf.SetAttribute("Key","pdf")

$pdf.SetAttribute("Value",$pdfIcon)

$pdf.SetAttribute("EditText","Adobe Acrobat or Reader X")

$pdf.SetAttribute("OpenControl","")

$xml.DocIcons.ByExtension.AppendChild($pdf) | Out-Null

$xml.Save($DocIconFilePath)

Write-Host -ForegroundColor White " - Restarting IIS..."

iisreset

}

Catch {$_; Pause "exit"; Break}

}

}

If ($MIMEType -eq $true)

{

# Add the PDF MIME type to each web app so PDFs can be directly viewed/opened without saving locally first

# More granular and generally preferable to setting the whole web app to "Permissive" file handling

$MimeType = "application/pdf"

Write-Host -ForegroundColor White " - Adding PDF MIME type `"$MimeType`" web apps..."

$WebApp = Get-SPWebApplication -Identity $WebAppUrl

If ($WebApp.AllowedInlineDownloadedMimeTypes -notcontains $MimeType)

{

Write-Host -ForegroundColor White " - "$WebAppUrl": Adding "`"$MimeType"`"..." -NoNewline

$WebApp.AllowedInlineDownloadedMimeTypes.Add($MimeType)

$WebApp.Update()

Write-Host -ForegroundColor White "Done."

}

Else

{

Write-Host -ForegroundColor White " - "$WebAppUrl": "`"$MimeType"`" already added."

}

}

Write-Host -ForegroundColor White " - Done configuring PDF indexing and icon display."

}

Function Confirm-LocalSession

{

# Another way

# If ((Get-Process -Id $PID).ProcessName -eq "wsmprovhost") {Return $false}

If ($Host.Name -eq "ServerRemoteHost") {Return $false}

Else {Return $true}

}

#EndRegion

main

Inspirado en el buen trabajo de  AndrewWoody en el sitio de codeplex para automatizar el proceso de instalación de SharePoint extraje la instalación de PDF para realizarlo sobre instalaciones de SharePoint ya realizadas y facilitar la instalación.

Script4Fun,

Juan Manuel Herrera

No hay comentarios.: