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.
$PSVersionTable.CLRVersion
and$env:PROCESSOR_ARCHITECTURE
and make sure you have the correct DLL for that version and OS architecture. – Ansgar Wiechers