Tags

,

We have a system which contains thousands of InfoPath forms, the template files are in the SharePoint site. And some forms cannot be opened after some users editing because of the wrong InfoPath Cache. So I wrote the little scrip to loop all the forms and get HREF string to check if the url is valid.

Param($Path = "D:\Temp",[switch]$verbose)

Write-Host

$fc = new-object -com scripting.filesystemobject
$folder = $fc.getfolder($path)
$strFinder = "http.*xsn"
$logfile = "D:\Temp\NPILog.txt"

function ProcessFiles([string]$folderpath) {

Write-host " + Processing all Forms from: $folderpath"
$Forms = dir $folderpath "*.xml"

		Write-host "  + $($Forms.count) Forms found"
		Write-host " + Processing all $($Forms.count) Forms located at: $folderpath" -fore White

			foreach($Form in $Forms)
			{
				Write-Host "  + +	Processing Form: $($Form.Name)" -fore White
				Find-String  $strFinder $Form.FullName
			}	
	
}

function Find-String($find, $path)
{
    $url_values = New-Object System.Collections.ArrayList
	$i = 0
	echo "Finding string `"$find`" in file contents and file names of path: $path"
	ls $path | select-string $find -list |% { echo "Processing contents of $($_.Path)"; (get-content $_.Path) |% { [regex]::matches($_, $find) |%{$url_values.Add($_.value); $i++}} }
    echo $url_values[0]
	if(Check-URL $url_values[0])
	{
		Write-Host "$($path) href is fine"
	}
	else
	{
		Write-Host "$($path) href is wrong" -foregroundcolor red -backgroundcolor yellow
		Add-Content $logfile "$($path) href is wrong, checked at $(Get-Date)"

	}
}

function Check-URL($url)
{
	$urlIsValid = $false
	try
	{
		$request = [System.Net.WebRequest]::Create($url)
		$request.Credentials = [System.Net.CredentialCache]::DefaultCredentials;
		$request.Method = 'HEAD'
		$response = $request.GetResponse()
		$httpStatus = $response.StatusCode
		$urlIsValid = ($httpStatus -eq 'OK')
		$tryError = $null
		$response.Close()
	}
	catch [System.Exception] {
		$httpStatus = $null
		$tryError = $_.Exception
		$urlIsValid = $false;
	}
    
	return $urlIsValid
}

Write-Host "$($folder.ShortName) "
Write-Host "  + Total Folders: $($folder.SubFolders.Count)"
foreach ($i in $folder.SubFolders) {
	Write-Host "  + Folder Name: $($i.ShortName)"
	ProcessFiles($i.Path)
}
Advertisements