Last active
August 29, 2015 14:03
-
-
Save peaeater/32d339acb7cd3eec1e3e to your computer and use it in GitHub Desktop.
Sample Andi extractomatic script that creates its own ODBC connection string and logs to Application event log.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Extracts data from the named textbase as files like {tn}-{0}.xml in output folder. | |
Logs to Application event log - source must already have been added. | |
#> | |
param( | |
[Parameter(Mandatory=$false,Position=0)] | |
[string]$logsrc = "Andi Solr Update" | |
) | |
$basePath = "c:\path\to\extract-exe" | |
$textbasePath = "c:\path\to\textbases" | |
$tn = "mytextbase" | |
$log = "" # if empty, won't write to local text file | |
Add-Type -AssemblyName System.Data | |
function odbcConnectionString([string]$dir) { | |
$builder = new-object -typename System.Data.Odbc.OdbcConnectionStringBuilder | |
$builder.Driver = "Inmagic DB/Text Driver (*.tba)" | |
$builder.Add("dbq", $dir) | |
$builder.Add("server", "NotTheServer") | |
$builder.Add("readonly", "1") | |
return $builder.ToString() | |
} | |
function logError([string]$msg) { | |
# write error msg to Application EventLog | |
Write-EventLog -LogName Application -Source $logsrc -EventId 500 -EntryType Error -Message $msg -Category 0 | |
Write-Host ("ERROR {0}" -f $msg) | |
} | |
function logInfo([string]$msg) { | |
# write info msg to Application EventLog | |
Write-EventLog -LogName Application -Source $logsrc -EventId 200 -EntryType Information -Message $msg -Category 0 | |
Write-Host ("INFO {0}" -f $msg) | |
} | |
try { | |
$dsn = odbcConnectionString($textbasePath) | |
# prep | |
if (!(test-path "$basePath\output")) { | |
mkdir "$basePath\output" | |
} | |
# clean | |
rm "$basePath\output\$tn*" | |
# connect via odbc and write to file/s | |
start-process -nonewwindow -wait -filepath "$basePath\extract.exe" -argumentlist "/from:`"$tn`"", | |
"/query:`"Status LIKE 'Available'`"", | |
"/log:`"$log`"", | |
"/rowmax:1000", | |
"/connection:`"$dsn`"", | |
"/out:`"$basePath\output\$tn.txt`"", | |
"/field:`"RecordID,Title,Subject,Author`"", | |
"/formatType:`"nominal`"", | |
"/format:`" | |
<doc> | |
<field name='src'><![CDATA[MyTextbase]]></field> | |
<field name='id'><![CDATA[mytextbase{RecordID}]]></field> | |
<field name='srcid'><![CDATA[{RecordID}]]></field> | |
<field name='title'><![CDATA[{Title}]]></field> | |
<field name='topic'><![CDATA[{Subject}]]></field> | |
<field name='topic_free'><![CDATA[{Subject}]]></field> | |
<field name='name'><![CDATA[{Author}]]></field> | |
<field name='name_free'><![CDATA[{Author}]]></field> | |
<field name='freetext'><![CDATA[<name label='Author'>{Type}</name>]]></field> | |
<field name='freetext'><![CDATA[<topic label='Subject'>{Subject}</topic>]]></field> | |
</doc>`"" | |
# construct xml files | |
$xmlheader = "$basePath\config\xmlheader.txt" | |
$xmlfooter = "$basePath\config\xmlfooter.txt" | |
$files = ls "$basePath\output\*.*" -include $tn*.txt | |
$utf8nobom = new-object System.Text.UTF8Encoding($false) | |
# check for txt (attempt to determine if extractomatic has worked) | |
if ($files.Count -eq 0) { | |
logError("Extractomatic {0} produced no files." -f $script:myinvocation.mycommand.name) | |
exit 1 | |
} | |
else { | |
logInfo("Extractomatic {0} produced {1} files." -f $script:myinvocation.mycommand.name, $files.Count) | |
} | |
foreach ($file in $files) { | |
# write UTF8 output without BOM as Java xml parsers choke on it | |
$content = get-content (get-childitem $xmlheader, $file, $xmlfooter) -encoding UTF8 | |
[System.IO.File]::WriteAllLines("$basePath\output\$($file.BaseName).xml", $content, $utf8nobom) | |
} | |
# clean txt out | |
rm "$basePath\output\$tn*.txt" | |
exit 0 | |
} | |
catch [Exception] { | |
logError($_.Exception) | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment