我试图创建几行代码,如果一台机器是32/64位,那么它将从WMI中提取,如果它是64,那么这就是....如果它是32位,那么这样做......
有人可以帮忙吗?
Dav*_*obb 11
您可以检查和比较环境中的两种布尔静态方法,一种是查看PowerShell进程,另一种是查看底层操作系统。
if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem)
{
"PowerShell process does not match the OS"
}
Run Code Online (Sandbox Code Playgroud)
小智 6
这与之前的答案类似,但无论 64 位/64_位/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"
}
Run Code Online (Sandbox Code Playgroud)
包括在64位机器上运行的32位版本的PowerShell中为我工作的示例:
gwmi win32_operatingsystem | select osarchitecture
Run Code Online (Sandbox Code Playgroud)
返回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"
}
Run Code Online (Sandbox Code Playgroud)
[IntPtr]::Size -eq 4 # 32 bit
Run Code Online (Sandbox Code Playgroud)
IntPtr 的大小在 32 位计算机上为 4 字节,在 64 位计算机上为 8 字节 ( https://msdn.microsoft.com/en-us/library/system.intptr.size.aspx )。