In a regular PowerShell window, one can determine if the current shell is x64 or x86 by examining the boolean environment variable [Environment]::Is64BitProcess
.
Now I did the same but from an embedded PS session in a .NET application. And the output shows the bitness is not 64bit.
static void Main(string[] args)
{
using (PowerShell ps = PowerShell.Create())
{
foreach (var res in ps
.AddScript("$host.version.tostring()").AddStatement()
.AddScript("[Environment]::Is64BitProcess").AddStatement()
.Invoke())
{
Console.WriteLine(res.BaseObject);
}
// Outputs:
// 4.0
// False
}
}
Using corflags
and ildasm
, I made sure the referenced System.Management.Automation
DLL is v4.0 and ILONLY
. And even if I invoke my application from an x64 shell (e.g., PowerShell.exe
x64), the result still indicates Is64BitProcess == false
. Any hint?
This is important because I want to Add-PsSnapin
from the embedded session. Without getting the bitness right, the SnapIn can't be loaded.