Start code in 32bit and 64bit powershell at the same time(one session)

Hi,

I’m opening a power-shell session(64bit) to a remote computer. In that session a lot of scripts(functions) are getting executed. I am trying to connect to an Oracle db server and execute some queries there. Before actual connection, there are couple of tests that are getting executed, pre-deployment tests, where we are checking if connection to the database is good, sftp connection is ok, ssh connection, they are executed one by one. If all that tests are passing, then we start deployment.

Before starting the tests we are loading all the power-shell scripts where those functions are (for db connection test, ssh connection…). Like this :

LogInfo("Load DatabaseActions")
$incFile = Resolve-Path .\deployment\DatabaseActions.ps1
. $incFile

LogInfo("Load SFTPUpload")
$incFile = Resolve-Path .\deployment\SFTPUploadTest.ps1
. $incFile

LogInfo("Load SSH")
$incFile = Resolve-Path .\deployment\SSH_test.ps1
. $incFil

After they are loaded, tests are starting to execute. We have a separate power-shell script for each test.
First test is connection to the database. There is a script called Db_Connection_test.ps1 which has the following code:

$TestResult = TestDBConnection $RunSettings.ConnectionString

All the needed functions for db connection live in DatabaseActions.ps1 script(including TestDBConnection) :

Add-Type -Path "C:\somePath\Oracle.DataAccess.dll"

<span class=""typ"">Function</span> <span class=""typ"">TestDatabaseConnection</span><span class=""pun"">(</span><span class=""pln"">$connectString</span><span class=""pun"">)</span>

<span class=""pun"">{</span><span class=""pln"">
try
</span><span class=""pun"">{</span>
Code for opening connection to the database
<span class=""pun"">...</span>
<span class=""pun"">}</span>
<span class=""pun"">}</span>

The problem here is that I need to execute the first test in power-shell 32bit because of the Oracle-Client dll. It throws an error in 64bit. But it is one session, so I need to execute that test in 32bit and again continue in 64bit because of the other tests. I tried to start everything in 32bit session, connection to the database is good, but the other tests like ssh connection is falling because the ssh client is 64bit.

I tried this approach by defining an alias when starting the session like this :

Set-Alias Start-PowerShell32 “$env:windir\syswow64\WindowsPowerShell\v1.0\powershell.exe”

And wrap the code in DatabaseActions.ps1 sript like this:

Start-PowerShell32 { 
```
Add-Type -Path "C:\somePath\Oracle.DataAccess.dll"

```

```
Function TestDatabaseConnection($connectString)

```

```
{
try
{
Code for opening connection to the database
...
}
}
```

}

But then the TestDatabaseConnection is not known, because it is a new session.

It’s a bit confusing but I tried to explain as much as possible. I hope you can understand what is going on and what is the problem. I really don’t know how to fix this problem. Probably there is a solution but I don’t know it :).

Would like to hear your suggestions how to fix this.

Thanks

Here is one way you can run a 32bit process from a 64bit console.

Let’s first capture the 32 bit powershell.exe path as a string
$32bitpowershell = 'C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe'

Next we define a scriptblock that will write if it’s in a 32 or 64 bit process.

$scriptblock = {
    $bit = if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem)
    {
        "This is a 32 bit process"
    }
    else
    {
        "This is a 64 bit process"
    }
    Write-Output $bit
}

Now we call it in our current 64 bit console, then execute 32 bit, then 64 one more time for fun.

& $scriptblock
& $32bitpowershell $scriptblock
& $scriptblock

The output

This is a 64 bit process
This is a 32 bit process
This is a 64 bit process

Thanks, simple and useful explanation.

I still need to figure out how to implement it in my case.

Hi Doug,

When I start for example a 64bit session and in the middle of executing some scripts I need to switch and start 32bit session(like I must do for the 32bit Oracle dll), is there a way I can transfer all the previously loaded scripts from the 64bit session into the 32bit session. Can I transfer some variables from one process to another since it as a new session and it doesn’t know the scripts all the variables from the previous one.

Thanks

If you call with . instead of & then any variable defined in the child scope will remain in the parent scope.

Get-Variable Test # Error, no test variable

$script = {$test = "I'm a child scope variable"}

. $script

Get-Variable Test # Test variable now defined in calling session

You could also export the information to a file or database and then read it in with the other script.

Additionally, you could pass in arguments. Maybe this small example will spark some ideas.

$scriptblock = {
    
    $32bitpowershell = "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"

    foreach ($arg in $args[0])
    {
        $current = "Write-Host "$arg""
        & $32bitpowershell $current
    }
}

. $scriptblock "We","are","arguments"