Save I3DIR Files to CSV with PowerShell

Here’s a post that will apply to exactly 0.3 people out there. But I think it’s a great story of how PowerShell came to the rescue for one of our customers.

A support request came in this morning for a customer who had accidentally deleted the source for one of their data sources. (Background: our client software displays directories–sort of like detailed address books–that can be sourced from pretty much anywhere.) They remembered that our client app maintains an encrypted local cache of the data and wanted to know if there was any way they could use that to create a CSV file to re-import back to the original data source.

My first thought: PowerShell.

It took 20-30 minutes to review and remember what our application code was doing and create a PowerShell script that decrypted the data and exported it to CSV. It helps that we used standard .NET functionality like the built-in serialization of a DataSet and the Data Protection API. If anything had been customized it would’ve been trickier, but still do-able.

End result, here’s the script (it’s also available for download here, if you’re an Interactive Intelligence customer and find it useful in some way) — completely unsupported of course, use at your own risk, etc.


param
(
    [string] $i3DirFile = $(throw "You must specify an i3dir file that has been created on this same machine"),
    [string] $outputCSVFile = $(throw "You must specify an output file name for the resulting CSV data")
)

# Load the DPAPI assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null

# Load the encrypted file
$encryptedData = get-content -encoding byte $i3DirFile
$decryptedData = [System.Security.Cryptography.ProtectedData]::Unprotect($encryptedData, $null, [System.Security.Cryptography.DataProtectionScope]::LocalMachine)

$rawXml = [System.Text.Encoding]::UTF8.GetString($decryptedData)

$dataset = new-object System.Data.DataSet
$reader = new-object System.IO.StringReader($rawXml)
$dataset.ReadXml($reader, [System.Data.XmlReadMode]::ReadSchema) | out-null

$dataset.Tables[0] | export-csv $outputCSVFile

That’s it! Just 8 or so lines of code (with no error checking, of course). A great example of how PowerShell came to the rescue, even though this had nothing to do with the command line, or scripting, or PowerShell itself.

This entry was posted on Tuesday, September 18th, 2007 at 11:10 pm and is filed under interactive intelligence, powershell. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

2 Responses to “Save I3DIR Files to CSV with PowerShell”

  1. mike Says:

    That’s pretty cool. It’s still unbelievable to me how few lines it takes in powershell to do things. How many lines do you think it would have been to do this in a regular C# app?

  2. aaron Says:

    Maybe 15-20 or so, for parameter validation, etc.
    But that’s not including converting the datatable to CSV format – that would’ve been a custom routine that would’ve been maybe another 15-20 lines, not to mention it would’ve been buggy. ;)
    With PowerShell we get the “Export-CSV” out of the box.
    So nice.