【问题标题】:Determining 32/64 bit in Powershell在 Powershell 中确定 32/64 位
【发布时间】:2015-08-13 00:33:59
【问题描述】:

我正在尝试创建几行代码,如果机器是 32/64 位,则将从 WMI 中提取,如果是 64 位,则执行此操作....如果是 32 位执行此操作...

p>

谁能帮忙?

【问题讨论】:

  • [Environment]::Is64BitOperatingSystem

标签: powershell


【解决方案1】:

环境中有两种布尔静态方法可供您检查和比较,一种查看 PowerShell 进程,一种查看底层操作系统。

if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem)
{
"PowerShell process does not match the OS"
}

【讨论】:

  • 此变量在旧环境中不可用,例如 Windows 7 上的 Powershell 2.0。@Guvante 的回答似乎有效。
  • @dmattp Is64BitProcessIs64BitOperatingSystem 是在 .NET 4.0 中引入的——最初由 PowerShell 3.0 使用——而 Win32_OperatingSystem WMI class 是在 Vista/Server 2008 中引入的。在旧系统上可以@987654324 @他们自己。
【解决方案2】:

Random discussion about it

假设您至少运行 Windows 7,以下应该可以工作。

包括一个在 64 位机器上运行的 32 位版本的 powershell 中对我有用的示例:

gwmi win32_operatingsystem | select osarchitecture

为 64 位返回“64 位”。

if ((gwmi win32_operatingsystem | select osarchitecture).osarchitecture -eq "64-bit")
{
    #64 bit logic here
    Write "64-bit OS"
}
else
{
    #32 bit logic here
    Write "32-bit OS"
}

【讨论】:

  • 旧操作系统没有 osarchitecture 属性,fwiw。
  • 什么是旧操作系统?我们只有 Windows 7 及更高版本
  • 那我觉得你很好。 Vista 可能是最后一个没有它的版本。
  • 64 位并不总是得到保证,我遇到了一个用户,他得到“64 位”作为响应。
  • @QuickNull:你能扩展那个评论吗?这种逻辑可能完全无法检测到 64 位(如其他 cmets 中所述),但我想不出它会错误检测 64 位的情况。
【解决方案3】:

这与之前的答案类似,但无论 64 位/64_bit/64 位/64 位格式如何,都会得到正确的结果。

if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -like "64*")
{
#64bit code here
Write "64-bit OS"
}
else
{
#32bit code here
Write "32-bit OS"
}

【讨论】:

    【解决方案4】:

    两条线拼接在一起形成了一条漂亮的单线:

    Write-Host "64bit process?:"$([Environment]::Is64BitProcess) ;Write-Host "64bit OS?:"$([Environment]::Is64BitOperatingSystem);
    

    【讨论】:

      【解决方案5】:
      [IntPtr]::Size -eq 4 # 32 bit
      

      IntPtr 的大小在 32 位机器上为 4 字节,在 64 位机器上为 8 字节 (https://msdn.microsoft.com/en-us/library/system.intptr.size.aspx)。

      【讨论】:

      • 我如何将它放入 if 语句中?
      • 这只会告诉您正在运行的 Powershell 版本。拉起一个 32 位窗口进行验证。
      • “IntPtr 的大小在 32 位机器上为 4 字节,在 64 位机器上为 8 字节”不正确。此代码检查 进程(而不是 操作系统)是否为 32 位。请参阅链接文档,其中显示(强调我的)“此进程中指针或句柄的大小,以字节为单位。此属性的值在 32 位 进程中为 4 ,以及 64 位 进程中的 8 个。"
      【解决方案6】:
      if($env:PROCESSOR_ARCHITECTURE -eq "x86"){"32-Bit CPU"}Else{"64-Bit CPU"}
      

      -编辑,抱歉忘记包含更多代码来解释用法。

      if($env:PROCESSOR_ARCHITECTURE -eq "x86")
       {
      #If the powershell console is x86, create alias to run x64 powershell console.
       set-alias ps64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe"
      
      $script2=[ScriptBlock]::Create("#your commands here, bonus is the script block expands variables defined above")
      
      ps64 -command $script2
       }
       Else{
       #Otherwise, run the x64 commands.
      

      【讨论】:

      • 如何将其放入 if 语句中,以便我可以将代码放在 32 位 cpu 和 64 位 cpu 之后。我尝试了你的线路,但没有成功,虽然我相信还有更多的事情
      • 这在 64 位版本的 Windows 上运行的 32 位版本的 powershell 中返回 true。
      • 抱歉,忘记包含处理在 x64 或 x86 powershell 中运行的代码。
      • 这可以在较新版本的 Windows ex.8.1 上运行吗
      • 我认为不会有任何问题,只要Powershell环境变量$env:PROCESSOR_ARCHITECTURE可用。
      【解决方案7】:

      重用 Guvante 的答案来创建一个全局布尔值

      $global:Is64Bits=if((gwmi win32_operatingsystem | select osarchitecture).osarchitecture -eq "64-bit"){$true}else{$false}
      

      【讨论】:

        猜你喜欢
        • 2010-09-16
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 2019-12-17
        • 2021-11-16
        • 2011-02-03
        • 1970-01-01
        • 2012-08-19
        相关资源
        最近更新 更多