9

When I try to do an invoke-sql command from Powershell X86 I get an error

invoke-sqlcmd -Query "SELECT 'HELLO!'" -ServerInstance Server -Database DB

invoke-sqlcmd : Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=15.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

If I run the exact same command from regular x64 Powershell prompt I do not get the error

invoke-sqlcmd -Query "SELECT 'HELLO!'" -ServerInstance Server -Database DB
Column1
-------
HELLO!

This is on a newly built windows 2016 x64 bit server:

OS Name Microsoft Windows Server 2016 Datacenter Version 10.0.14393 Build 14393

I need x86 powershell to run a script that uses a legacy 32 bit ODBC driver, it also uses invoke-sql commands as well.

Milo
  • 3,365
  • 9
  • 30
  • 44
user313720
  • 91
  • 1
  • 1
  • 2
  • 10
    Yeah I think the 32bit powershell is your issue. The `Microsoft.SqlServer.BatchParser` in the SQL server module is an x64 assembly. You have tested your ODBC driver in the x64 shell and it will not work? If that is indeed the case perhaps you can use a separate x86 powershell process for those parts of your script? – Mike Frank Jan 16 '19 at 15:59
  • Is there a x86 assembly? This script works in other environments? Would that be the only difference? But I did find a neat trick: Invoke-Command -ConfigurationName Microsoft.PowerShell32 { ... } will run 32 bit commands – user313720 Jan 16 '19 at 21:34
  • There are SQL [shared memory object packages](https://learn.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/installing-smo?view=sql-server-2017) that should include the required assemblies but I don't know if there is an x86 build, and if there were if it would work with the SQL module. I think your best bet is to use the Invoke-Command option for the 32 bit stuff. I would expect the module to work fine running in an x64 shell. – Mike Frank Jan 17 '19 at 02:44

2 Answers2

1

As mentioned in some other comments, it may be related to the bit version. If that is the case, you can start a job with the -RunAs32 flag for it to act like a 32-bit namespace and then return any data through the Receive-Job CMDlet

# I am 64-bit land
$job_handle = Start-Job -RunAs32 -ArgumentList "Relevant Data" -ScriptBlock {
    Param ( [string]$stuff_i_need )

    # I am 32-bit land

    return "Complete"
}

# I am 64-bit land (again)
$result = $job_handle | Wait-Job | Receive-Job

Note that it will be forced to use a namespace so any relevant data will need to be passed in

SlaQr
  • 11
  • 2
1

The SQLServer Module may be x86 compatible, but installing it with x64 Powershell as administrator places it under "Program Files", you need to install the module under x86 Powershell to get it in "Program Files (x86)".

An alternative is to adjust you Module Paths (System Environment variable).

Check the differences of $env:PSModulePath between x86 and x64.

Carsten.R
  • 128
  • 9