Add-PSSnapin Microsoft.SharePoint.PowerShell
–erroraction SilentlyContinue
## the web
application to be searched for form libraries
$url =
'https://yoursharepointurl'
## temp
working directory
$dir =
'D:\ExtractedForms'
$findstring =
'findthis String'
$exportFile =
$false
foreach ($web
in (Get-SPWebApplication
$url |
Get-SPSite -Limit
All| Get-SPWeb -Limit All))
{
foreach($list
in $web.Lists)
{
if ($list.DocumentTemplateUrl -ne
$null -or
$list.ContentTypes[0].ResourceFolder.Properties["_ipfs_infopathenabled"])
{
if ($list.ContentTypes[0].ResourceFolder.Properties["_ipfs_infopathenabled"])
{
#--------------- get the templateurl for the list
template....
foreach($folder
in $list.RootFolder.SubFolders)
{
foreach($file
in $folder.Files)
{
if($file.Name -eq 'template.xsn')
{
$listUrl =
$web.Url
+ '/' + $list.RootFolder +
'/' + $folder.Name
$templateUrl =
$web.Url
+ '/' + $list.RootFolder +
'/' + $folder.Name
+'/template.xsn'
$exportFile =
$true
}
}
}
}
elseif ($list.DocumentTemplateUrl.ToString().ToLower().EndsWith(".xsn"))
{
$listUrl = $web.Url + '/' + $list.rootFolder
$templateUrl =
$web.Url
+ '/' + $list.DocumentTemplateUrl
$exportFile =
$true
}
if($exportFile)
{
## download the form template
$filename = $file.Name
$fileID = $file.UniqueId.Tostring()
$localfile =
$fileID +
"\" +
$filename
$file =
$web.GetFile($templateUrl)
$bytes = $file.OpenBinary();
# Download the file to the path
$localfile =
$dir +
"\" +
$localfile
New-Item "$dir\$fileID"
-type directory
-force | Out-Null
New-Item "$dir\$fileID\Extracted"
-type directory
-force | Out-Null
[System.IO.FileStream] $fs = New-Object System.IO.FileStream($localfile, "OpenOrCreate")
$fs.Write($bytes, 0 , $bytes.Length)
$fs.Close()
## crack open the form template
EXPAND "$localfile"
-F:* "$dir\$fileID\Extracted" |
Out-Null
## get file content and check for string
$extractedFiles =
Get-ChildItem "$dir\$fileID\Extracted"
*.x*
foreach ($extractedFile
in $extractedFiles)
{
$efn = $extractedFile.Name
$f = [System.IO.File]::Open("$dir\$fileID\Extracted\$efn" ,"open","read","Read")
$sr = New-Object System.IO.StreamReader
$f
$content = $sr.ReadToEnd()
$sr.close()
$f.close()
if ($content.Contains($findstring))
{
Write-Host $templateUrl
" : " $extractedFile.Name
-foreground green
}
}
}
$exportFile =
$false
}
}
$web.Dispose()
}