【发布时间】:2015-08-13 00:33:59
【问题描述】:
我正在尝试创建几行代码,如果机器是 32/64 位,则将从 WMI 中提取,如果是 64 位,则执行此操作....如果是 32 位执行此操作...
p>谁能帮忙?
【问题讨论】:
-
[Environment]::Is64BitOperatingSystem
标签: powershell
我正在尝试创建几行代码,如果机器是 32/64 位,则将从 WMI 中提取,如果是 64 位,则执行此操作....如果是 32 位执行此操作...
p>谁能帮忙?
【问题讨论】:
[Environment]::Is64BitOperatingSystem
标签: powershell
环境中有两种布尔静态方法可供您检查和比较,一种查看 PowerShell 进程,一种查看底层操作系统。
if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem)
{
"PowerShell process does not match the OS"
}
【讨论】:
Is64BitProcess 和 Is64BitOperatingSystem 是在 .NET 4.0 中引入的——最初由 PowerShell 3.0 使用——而 Win32_OperatingSystem WMI class 是在 Vista/Server 2008 中引入的。在旧系统上可以@987654324 @他们自己。
假设您至少运行 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"
}
【讨论】:
这与之前的答案类似,但无论 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"
}
【讨论】:
两条线拼接在一起形成了一条漂亮的单线:
Write-Host "64bit process?:"$([Environment]::Is64BitProcess) ;Write-Host "64bit OS?:"$([Environment]::Is64BitOperatingSystem);
【讨论】:
[IntPtr]::Size -eq 4 # 32 bit
IntPtr 的大小在 32 位机器上为 4 字节,在 64 位机器上为 8 字节 (https://msdn.microsoft.com/en-us/library/system.intptr.size.aspx)。
【讨论】:
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.
【讨论】:
$env:PROCESSOR_ARCHITECTURE可用。
重用 Guvante 的答案来创建一个全局布尔值
$global:Is64Bits=if((gwmi win32_operatingsystem | select osarchitecture).osarchitecture -eq "64-bit"){$true}else{$false}
【讨论】: