如何使用 VBScript 确定我运行的是 32 位还是 64 位 Windows 操作系统?

发布于 2024-09-16 09:21:33 字数 350 浏览 2 评论 0原文

如何在 VBScript 中检测 Windows 操作系统的位数(32 位与 64 位)?

我尝试过这种方法,但它不起作用;我猜 (x86) 导致检查文件夹时出现一些问题..

还有其他选择吗?

progFiles="c:\program files" & "(" & "x86" & ")"

set fileSys=CreateObject("Scripting.FileSystemObject")

If fileSys.FolderExists(progFiles) Then    
   WScript.Echo "Folder Exists"    
End If

How do i detect the bitness (32-bit vs. 64-bit) of the Windows OS in VBScript?

I tried this approach but it doesn't work; I guess the (x86) is causing some problem which checking for the folder..

Is there any other alternative?

progFiles="c:\program files" & "(" & "x86" & ")"

set fileSys=CreateObject("Scripting.FileSystemObject")

If fileSys.FolderExists(progFiles) Then    
   WScript.Echo "Folder Exists"    
End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

东走西顾 2024-09-23 09:21:33

前几天在工作中也遇到了同样的问题。偶然发现了这个天才的 vbscript 作品,觉得它太好了,不能不分享。

Bits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth

来源:http://csi-windows.com/toolkit/csi-getosbits

Came up against this same problem at work the other day. Stumbled on this genius piece of vbscript and thought it was too good not to share.

Bits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth

Source: http://csi-windows.com/toolkit/csi-getosbits

山川志 2024-09-23 09:21:33

您可以查询PROCESSOR_ARCHITECTURE此处进行了描述,您必须添加一些额外的检查,因为对于任何 32 位进程,PROCESSOR_ARCHITECTURE 的值都将为 x86,即使它运行在 64 位上操作系统。在这种情况下,变量 PROCESSOR_ARCHITEW6432 将包含操作系统位数。更多详细信息请参见 MSDN

Dim WshShell
Dim WshProcEnv
Dim system_architecture
Dim process_architecture

Set WshShell =  CreateObject("WScript.Shell")
Set WshProcEnv = WshShell.Environment("Process")

process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE") 

If process_architecture = "x86" Then    
    system_architecture= WshProcEnv("PROCESSOR_ARCHITEW6432")

    If system_architecture = ""  Then    
        system_architecture = "x86"
    End if    
Else    
    system_architecture = process_architecture    
End If

WScript.Echo "Running as a " & process_architecture & " process on a " _ 
    & system_architecture & " system."

You can query the PROCESSOR_ARCHITECTURE. A described here, you have to add some extra checks, because the value of PROCESSOR_ARCHITECTURE will be x86 for any 32-bit process, even if it is running on a 64-bit OS. In that case, the variable PROCESSOR_ARCHITEW6432 will contain the OS bitness. Further details in MSDN.

Dim WshShell
Dim WshProcEnv
Dim system_architecture
Dim process_architecture

Set WshShell =  CreateObject("WScript.Shell")
Set WshProcEnv = WshShell.Environment("Process")

process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE") 

If process_architecture = "x86" Then    
    system_architecture= WshProcEnv("PROCESSOR_ARCHITEW6432")

    If system_architecture = ""  Then    
        system_architecture = "x86"
    End if    
Else    
    system_architecture = process_architecture    
End If

WScript.Echo "Running as a " & process_architecture & " process on a " _ 
    & system_architecture & " system."
稀香 2024-09-23 09:21:33

这是基于 @Bruno 非常简洁的答案的一对 VBScript 函数:

Function Is32BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 32 Then
        Is32BitOS = True
    Else
        Is32BitOS = False
    End If
End Function

Function Is64BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 64 Then
        Is64BitOS = True
    Else
        Is64BitOS = False
    End If
End Function

更新: 根据 @Ekkehard.Horner 的建议,这两个函数可以写得更简洁 使用单行语法,如下所示:

Function Is32BitOS() : Is32BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 32) : End Function

Function Is64BitOS() : Is64BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 64) : End Function

(请注意 周围的括号GetObject(...) = 32 条件不是必需的,但我相信它们增加了有关运算符优先级的清晰度。另请注意,修订后的实现中使用的单行语法避免了使用 If/。然后构造!)

更新2:根据@Ekkehard.Horner的额外反馈,有些人可能会发现这些进一步修改的实现既简洁又增强了可读性:

Function Is32BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is32BitOS = (GetObject(Path).AddressWidth = 32)
End Function

Function Is64BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is64BitOS = (GetObject(Path).AddressWidth = 64)
End Function

Here is a pair of VBScript functions based on the very concise answer by @Bruno:

Function Is32BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 32 Then
        Is32BitOS = True
    Else
        Is32BitOS = False
    End If
End Function

Function Is64BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 64 Then
        Is64BitOS = True
    Else
        Is64BitOS = False
    End If
End Function

UPDATE: Per the advice from @Ekkehard.Horner, these two functions can be written more succinctly using single-line syntax as follows:

Function Is32BitOS() : Is32BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 32) : End Function

Function Is64BitOS() : Is64BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 64) : End Function

(Note that the parentheses that surround the GetObject(...) = 32 condition are not necessary, but I believe they add clarity regarding operator precedence. Also note that the single-line syntax used in the revised implementations avoids the use of the If/Then construct!)

UPDATE 2: Per the additional feedback from @Ekkehard.Horner, some may find that these further revised implementations offer both conciseness and enhanced readability:

Function Is32BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is32BitOS = (GetObject(Path).AddressWidth = 32)
End Function

Function Is64BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is64BitOS = (GetObject(Path).AddressWidth = 64)
End Function
只等公子 2024-09-23 09:21:33

WMIC 查询可能会很慢。使用环境字符串:

Function GetOsBits()
   Set shell = CreateObject("WScript.Shell")
   If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
      GetOsBits = 64
   Else
      GetOsBits = 32
   End If
End Function

WMIC queries may be slow. Use the environment strings:

Function GetOsBits()
   Set shell = CreateObject("WScript.Shell")
   If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
      GetOsBits = 64
   Else
      GetOsBits = 32
   End If
End Function
寂寞陪衬 2024-09-23 09:21:33

确定CPU是32位还是64位很容易,但问题是如何确定操作系统是32位还是64位。当 64 位 Windows 运行时,ProgramW6432 环境变量被定义。

这:

CreateObject("WScript.Shell").Environment("PROCESS")("ProgramW6432") = ""

对于 32 位操作系统将返回 true,对于 64 位操作系统将返回 false,并且适用于所有版本的 Windows,包括非常旧的版本。

Determining if the CPU is 32-bit or 64-bit is easy but the question asked is how to determine if the OS is 32-bit or 64-bit. When a 64-bit Windows is running, the ProgramW6432 environment variable is defined.

This:

CreateObject("WScript.Shell").Environment("PROCESS")("ProgramW6432") = ""

will return true for a 32-bit OS and false for a 64-bit OS and will work for all version of Windows including very old ones.

不交电费瞎发啥光 2024-09-23 09:21:33

Bruno 答案的附录:您可能想要检查操作系统而不是处理器本身,因为您可以在较新的 CPU 上安装较旧的操作系统:

strOSArch = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@").OSArchitecture

返回字符串“32 位”或“64 位”。

Addendum to Bruno's answer: You may want to check the OS rather than the processor itself, since you could install an older OS on a newer CPU:

strOSArch = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@").OSArchitecture

Returns string "32-bit" or "64-bit".

陪你搞怪i 2024-09-23 09:21:33

您还可以检查文件夹 C:\Windows\sysnative 是否存在。此文件夹(或更好的别名)仅存在于 32 位进程中,请参阅 文件系统重定向器

Set fso = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject( "WScript.Shell" )

If fso.FolderExists(wshShell.ExpandEnvironmentStrings("%windir%") & "\sysnative" ) Then
    WScript.Echo "You are running in 32-Bit Mode"
Else
    WScript.Echo "You are running in 64-Bit Mode"
End if

注意:此脚本显示您当前的进程是在 32 位模式还是 64 位模式下运行 - 它不显示您的 Windows 体系结构。

You can also check if folder C:\Windows\sysnative exist. This folder (or better alias) exist only in 32-Bit process, see File System Redirector

Set fso = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject( "WScript.Shell" )

If fso.FolderExists(wshShell.ExpandEnvironmentStrings("%windir%") & "\sysnative" ) Then
    WScript.Echo "You are running in 32-Bit Mode"
Else
    WScript.Echo "You are running in 64-Bit Mode"
End if

Note: this script shows whether your current process is running in 32-bit or 64-bit mode - it does not show the architecture of your Windows.

源来凯始玺欢你 2024-09-23 09:21:33
' performance should be good enough
' Example usage for console:
' CSript //NoLogo *ScriptName*.vbs
' If ErrorLevel 1 Echo.Win32
' VBScript:
On Error Resume Next
Const TargetWidth = 32
Set WMI = GetObject("winMgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set Query = WMI.ExecQuery("SELECT AddressWidth FROM Win32_Processor")
For Each Item in Query
  If Item.AddressWidth = TargetWidth Then
    WScript.Quit 1
  End If
Next
WScript.Quit 0
' performance should be good enough
' Example usage for console:
' CSript //NoLogo *ScriptName*.vbs
' If ErrorLevel 1 Echo.Win32
' VBScript:
On Error Resume Next
Const TargetWidth = 32
Set WMI = GetObject("winMgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set Query = WMI.ExecQuery("SELECT AddressWidth FROM Win32_Processor")
For Each Item in Query
  If Item.AddressWidth = TargetWidth Then
    WScript.Quit 1
  End If
Next
WScript.Quit 0
柠檬色的秋千 2024-09-23 09:21:33

使用环境。在XP中测试,但我找不到32位CPU来测试......

   function getbitsos()
      with WScript.CreateObject("WScript.Shell").environment("PROCESS")
        if .item("PROCESSOR_ARCHITECTURE") ="X86" and .item("PROCESSOR_ARCHITEW6432") =vbnullstring Then
          getbitsos=array(32,32,32)
        elseif .item("PROGRAMFILES(x86)")=vbnullstring Then 
          getbitsos=array(64,32,32)
        elseif .item("PROGRAMFILES(x86)")=.item("PROGRAMFILES") Then
          getbitsos=array(64,64,32)
        Else
          getbitsos=array(64,64,64)
        end if   
      end with
    end function
    
    a=getbitsos()
    wscript.echo "Processor " &a(0) & vbcrlf & "OS "  & a(1) &vbcrlf& "Process " & a(2)& vbcrlf 

Using environment. Tested in XP, but I can't find a 32 bit CPU to test...

   function getbitsos()
      with WScript.CreateObject("WScript.Shell").environment("PROCESS")
        if .item("PROCESSOR_ARCHITECTURE") ="X86" and .item("PROCESSOR_ARCHITEW6432") =vbnullstring Then
          getbitsos=array(32,32,32)
        elseif .item("PROGRAMFILES(x86)")=vbnullstring Then 
          getbitsos=array(64,32,32)
        elseif .item("PROGRAMFILES(x86)")=.item("PROGRAMFILES") Then
          getbitsos=array(64,64,32)
        Else
          getbitsos=array(64,64,64)
        end if   
      end with
    end function
    
    a=getbitsos()
    wscript.echo "Processor " &a(0) & vbcrlf & "OS "  & a(1) &vbcrlf& "Process " & a(2)& vbcrlf 
风吹短裙飘 2024-09-23 09:21:33

这与 Microsoft 博客 https://learn.microsoft.com/en-us/archive/blogs/david.wang/howto-detect-process-bitness

在 XP 32 和 win7 64 中测试(使用 32 位和 64 位 CMD )

Set objShell = CreateObject("WScript.Shell")
os_bit = 64
arch = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
archW6432 = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITEW6432%")
If LCase(arch) = "x86" Then
    If archW6432 = "" Or LCase(archW6432) = "%processor_architew6432%" Then
        os_bit = 32
    End If
End If

WScript.Echo "Operating System Bit: " & os_bit

This is the same proposed solution in Microsoft blog https://learn.microsoft.com/en-us/archive/blogs/david.wang/howto-detect-process-bitness

tested in XP 32 and win7 64 (using a 32 bit and 64 bit CMD)

Set objShell = CreateObject("WScript.Shell")
os_bit = 64
arch = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
archW6432 = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITEW6432%")
If LCase(arch) = "x86" Then
    If archW6432 = "" Or LCase(archW6432) = "%processor_architew6432%" Then
        os_bit = 32
    End If
End If

WScript.Echo "Operating System Bit: " & os_bit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文