0
votes

I am attempting to load the SQLite.dll from a PowerShell script so I can access a local SQLite database, but I keep getting an exception on the [System.Reflection.Assembly]::LoadFrom() method.

The error says:

Exception calling "LoadFrom" with "1" argument(s): "Could not load file or
assembly 'file:///C:\Source\System.Data.SQLite.dll' or one of its dependencies.
An attempt was made to load a program with an incorrect format."
At C:\Source\getAppData.ps1:10 char:5
+     [void][System.Reflection.Assembly]::LoadFrom($sqlite_library_path)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : BadImageFormatException

I have tried several different versions of the System.Data.SQLite.dll - my first reaction was this was a 32bit version and I'm on 64bit OS.

I have also tried Add-Type but get a similar error:

Add-Type : Could not load file or assembly
'file:///C:\Source\System.Data.SQLite.dll' or one of its dependencies. An
attempt was made to load a program with an incorrect format.
At C:\Source\getAppData.ps1:12 char:5
+     Add-Type -Path $sqlite_library_path
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type],BadImageFormatException
    + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeCommand

Here is the PowerShell script; I made sure it is running as Administrator. I am not sure if I need to unblock the DLL, give it permissions, or use some other method of loading the assembly - I am fairly new to PowerShell so I imagine this is some sort of newbie mistake or environment issue as I have friends who say this method should work.

[cmdletbinding()]
[string]$sqlite_library_paxcth = "C:\Source\System.Data.SQLite.dll"
[string]$db_query = "SELECT Name from kiosk where STRFTIME('%s',LastFullSync)>STRFTIME('%s',date('now', '-60 day'))"
[string]$db_data_source = "C:\Source\AppData.db"

if (!(Test-Path $sqlite_library_path)) {throw $outcome = "Unable to find sqlite library"}
if (!(Test-Path $db_data_source)) {throw $outcome = "Unable to find appdb"}

[void][System.Reflection.Assembly]::LoadFrom($sqlite_library_path)

#Add-Type -Path $sqlite_library_path

$db_dataset = New-Object System.Data.DataSet
$db_data_adapter = New-Object System.Data.SQLite.SQLiteDataAdapter($db_query,"Data Source=$db_data_source")
[void]$db_data_adapter.Fill($db_dataset)

My goal is to simply query a SQLite database in the local file system and pipe the results to a datagrid or some other GUI for surfacing the results.

1
which .net framework version are you using?Mina Jacob
Which version of PowerShell are you running? Also are you using 64-bit PowerShell or 32-bit? The common shortcut defaults to 64-bit so look for PowerShell (x86). Have a look at this as well.Matt
Check the values of $PSVersionTable.CLRVersion and $env:PROCESSOR_ARCHITECTURE and make sure you have the correct DLL for that version and OS architecture.Ansgar Wiechers
I ended up running my script in the32bit ISE and it worked fine btw.Shawn J. Molloy

1 Answers

1
votes

I'm having the same issue and ended up scuttling my attempt to go 64-bit. Using the 32-bit version of PowerShell and the SQLite Assembly works fine for what I need.

On a side note: My laptop loads the 64-bit version without any qualms. I'm not sure why and it's driving me a little batty. :)