This script exports SharePoint 2007 keywords, synonyms, and best bets to csv files

-----------------------------------------------------------
 Export keywords from SP-2007 --> CSV files

 This script will export Sharepoint 2007
 keywords/bestbets/synonyms in to CSV files.
-----------------------------------------------------------

 Definition :

 This script is used for exporting keywords/bestbets/synonyms into CSV files
 from a Sharepoint 2007. 

 Example: .\export_SP2007_keywords_to_CSV.ps1 -c .\configuration.xml

configuration.xml:


<!-- Contains Configuration for scripts that:    -->
<!--   Microsoft Search Server 2007 -> CSV   -->

<config>
 <export_SP2007_CSV>
  <!-- The Microsoft Search Server 2007 site to export keywords/bestbets/synonyms into CSV files-->  
  <SPsiteURL>http://fs14/</SPsiteURL>  
  <!-- The path+filenames for the output CSV files -->
  <output_keywordsfile>.\csv\keyword.csv</output_keywordsfile>
  <output_bestbetsfile>.\csv\bestbet.csv</output_bestbetsfile>
  <output_synonymsfile>.\csv\synonym.csv</output_synonymsfile>
 </export_SP2007_CSV>
</config>

PowerShell
Edit|Remove
$global:configFile = ""
$global:siteid=""
$global:keywordsfile=""
$global:bestbetsfile=""
$global:synonymsfile=""
$global:proceed = "True"

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search") > $null

function checkFile($path)
{
	$status = Test-Path $path
		
	return $status
}

function ProcessConfigFile($configFile)
{
	write-host ("[STATUS] Processing config file : " + $configFile) -ForegroundColor green
			
	$doc = New-Object System.Xml.XmlDocument 
	$doc.Load($configFile) 

	$global:siteid = $doc.config.export_SP2007_CSV.SPsiteURL
	$global:keywordsfile = $doc.config.export_SP2007_CSV.output_keywordsfile
	$global:bestbetsfile = $doc.config.export_SP2007_CSV.output_bestbetsfile
	$global:synonymsfile = $doc.config.export_SP2007_CSV.output_synonymsfile	
	
	write-host ("[STATUS] Using these configurations: `n") -ForegroundColor green
	write-host ("`t site url to export from : " + $siteid) -ForegroundColor green	
	if(checkFile($keywordsfile))	{ write-host ("`t File allready exists, will overwrite : " + $keywordsfile) -ForegroundColor green ; del $keywordsfile }  else { write-host ("`t keywordsfile : " + $keywordsfile) -ForegroundColor green }
	if(checkFile($bestbetsfile))	{ write-host ("`t File allready exists, will overwrite : " + $bestbetsfile) -ForegroundColor green ; del $bestbetsfile }  else { write-host ("`t bestbetsfile : " + $bestbetsfile) -ForegroundColor green }
	if(checkFile($synonymsfile))	{ write-host ("`t File allready exists, will overwrite : " + $synonymsfile) -ForegroundColor green ; del $synonymsfile}  else { write-host ("`t synonymsfile : " + $synonymsfile) -ForegroundColor green }
	
	$temp = New-item -itemtype file $keywordsfile  -force
	$temp = New-item -itemtype file $bestbetsfile  -force
	$temp = New-item -itemtype file $synonymsfile  -force
}


write-host ("-----------------------------------------------------------`n Export keywords from SP-2007 --> CSV files ") -ForegroundColor yellow
write-host ("`n`n This script will export Sharepoint 2007 `n keywords/bestbets/synonyms in to CSV files.`n-----------------------------------------------------------`n`n") -ForegroundColor yellow


if (!$args)
{
	write-host ("You must specificy command line arguments") -ForegroundColor red
	write-host ("Try : -? for more info") -ForegroundColor red
}
else
{	
	
	
	switch($args)
	{
		"-c" { 
				if ($args[1] -ne $null)
					{ 
						write-host ("[STATUS] Will use config file : " + $args[1] +"") -ForegroundColor green
						$global:configFile = $pwd.path + $args[1]
						
						if (checkFile($global:configFile)) { ProcessConfigFile($global:configFile) }
						else { write-host ("[Error ] Config file " + $configFile + " does not exist!`n`n") -ForegroundColor red ; $global:proceed = "False"}												
					}
				else 
					{ 
						write-host ("[Error ] Need to specify the config file...!") -ForegroundColor red ; $global:proceed = "False"
					}
			 }		
		"-?" { $help = "`n`n Definition : `n`n This script is used for exporting keywords/bestbets/synonyms into CSV files `n from a Sharepoint 2007. `n`n Example: .\export_SP2007_keywords_to_CSV.ps1 -c .\configuration.xml`n`n" ; write-host $help ; exit }
	}	
	
	if ($global:proceed -eq "True")
	{
		$Thesite = new-object Microsoft.SharePoint.SPSite($global:siteid)
		
		$searchContext =  [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($Thesite)
		$myURI = new-object system.URI($global:siteid)

		$keywords =  new-object Microsoft.Office.Server.Search.Administration.Keywords($searchContext,$myURI)
		
		#Add first line descriptors in all output files
		"Keyword" > $global:keywordsfile
		"Keyword, Synonyms" > $global:synonymsfile
		"Keyword, BestBet, Url, Description" > $global:bestbetsfile
		
		foreach($Keyword in $keywords.AllKeywords)
		{
			
			$Keyword.Term >> $global:keywordsfile
			
			foreach($MySynonym in $Keyword.Synonyms)
			{				
				($keyword.Term + "," + $MySynonym.Term) >> $global:synonymsfile
			}
			foreach($MyBestBet in $Keyword.BestBets)
			{				
				($keyword.Term + "," + $MyBestBet.title + "," + $MyBestBet.url + "," + $MyBestBet.description) >> $global:bestbetsfile
			}					
		}
				
		write-host ("`n[STATUS] keywordsfile output captured ") -ForegroundColor green
		write-host ("[STATUS] synonyms output captured ") -ForegroundColor green
		write-host ("[STATUS] keywordsfile output captured ") -ForegroundColor green
		write-host ("[STATUS] Job complete! `n`n") -ForegroundColor green
	}
}