¿Determinar si el proceso actual de PowerShell es de 32 o 64 bits?


Cuando se ejecuta un script de PowerShell en una plataforma de sistema operativo de x64 bits, ¿cómo se puede determinar en el script en qué versión de PowerShell (32 bits o 64 bits) se está ejecutando el script?

Antecedentes
Tanto las versiones de 32 bits como las de 64 bits de PowerShell se instalan de forma predeterminada en una plataforma de 64 bits como Windows Server 2008. Esto puede generar dificultades cuando se ejecuta un script de PowerShell que debe dirigirse a una arquitectura específica (es decir, usar 64 bits para un script de SharePoint 2010, para consumir las bibliotecas de 64 bits).

Pregunta relacionada:

Author: Community, 2011-12-21

3 answers

Si su shell se ejecuta en. NET 4.0 (PowerShell 3.0):

PS> [Environment]::Is64BitProcess
True
 118
Author: Shay Levy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-21 12:13:10

Para determinar en su script qué versión de PowerShell está utilizando, puede utilizar las siguientes funciones auxiliares (cortesía de Jaredpar's respuesta a una pregunta relacionada):

# Is this a Wow64 powershell host
function Test-Wow64() {
    return (Test-Win32) -and (test-path env:\PROCESSOR_ARCHITEW6432)
}

# Is this a 64 bit process
function Test-Win64() {
    return [IntPtr]::size -eq 8
}

# Is this a 32 bit process
function Test-Win32() {
    return [IntPtr]::size -eq 4
}

Las funciones anteriores hacen uso del hecho de que el tamaño del sistema.IntPtr es específico de la plataforma. Es de 4 bytes en una máquina de 32 bits y 8 bytes en una máquina de 64 bits.

Tenga en cuenta que las ubicaciones de las versiones de 32 bits y 64 bits de Powershell son algo engañoso. El PowerShell de 32 bits se encuentra en C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe, y el PowerShell de 64 bits está en C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe, cortesía de este artículo .

 75
Author: MagicAndi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:17:39

También puedes usar esto. Lo probé en la versión 2.0 y 4.0 de PowerShell.

$Arch = (Get-Process -Id $PID).StartInfo.EnvironmentVariables["PROCESSOR_ARCHITECTURE"];
if ($Arch -eq 'x86') {
    Write-Host -Object 'Running 32-bit PowerShell';
}
elseif ($Arch -eq 'amd64') {
    Write-Host -Object 'Running 64-bit PowerShell';
}

El valor de $Arch será x86 o amd64.

Lo bueno de hacerlo de esta manera es que también puede especificar un ID de proceso diferente, además del local ($PID), para determinar la arquitectura de un proceso de PowerShell diferente.

 13
Author: Trevor Sullivan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-19 16:29:54