FIX: Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed

Environment:

  • You are on a Windows 7 x64 bit System
  • Using PowerShell x64 bit
  • Authoring a script that connects to Oracle Database via "System.Data.OracleClient"
    Sample Script:

$connectionString = "Data Source=ORADB01.TESTLAB.LAN;User Id=root;Password=R00tUs3r;Integrated Security=no"
$queryString = "select * from users where username like ‘%govardhan%’"

[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") | Out-Null
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)
$command = new-Object System.Data.OracleClient.OracleCommand($queryString, $connection)
$connection.Open()
$resources = $command.ExecuteReader()
# Write out the results
while ($resources.read()) {
    $Process=$resources.GetDecimal(0)
    $Status=$resources.GetString(1)
    Write-Host $Process $Status
    }
$connection.Close()

 

Output:

 

PS C:>Powershell C:tempSend-Activation-Emails.ps1

Exception calling "Open" with "0" argument(s): "Attempt to load Oracle client libraries threw BadImageFormatException.  This problem will occur when running in 64 bit
mode with the 32 bit Oracle client components installed
."
At C:tempSend-Activation-Emails.ps1:23 char:1
+ $connection.Open()
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException
Exception calling "ExecuteReader" with "0" argument(s): "Invalid operation. The connection is closed."
At C:tempSend-Activation-Emails.ps1:24 char:1
+ $resources = $command.ExecuteReader()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException
You cannot call a method on a null-valued expression.
At C:tempSend-Activation-Emails.ps1:26 char:8
+ while ($resources.read()) {
+        ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

PS C:>

 

FIX:

Instead of using PowerShell x64 bit, run your command via PowerShell x86, then it should all run fine.

Leave a Reply

Your email address will not be published. Required fields are marked *