【问题标题】:check processor architecture and proceed with if statement检查处理器架构并继续执行 if 语句
【发布时间】:2014-10-13 23:35:41
【问题描述】:

我知道这里有很多如何获取处理器架构的示例。

这应该得到在 x64 上进行真假检查的类型

我的问题是:如何将此输出放入 if 语句中?

示例:如果是 64 位处理器,则执行几个步骤,如果是 32 位,则执行其他步骤。我该如何继续?

我尝试了几个版本的代码,但也得到了正确或错误的回复,这没关系,但如何继续?

你们能帮帮我吗?

谢谢

【问题讨论】:

  • 请展示一些您已经尝试过的示例。我猜你在 if 语句本身上遇到了问题,而不是在获取架构上。另外,请尝试再次重写您的问题,因为不清楚您真正想要什么。

标签: powershell architecture processor


【解决方案1】:

谢谢大家。

我使用以下方法解决了它:

$os_type = (Get-WmiObject -Class Win32_ComputerSystem).SystemType -match ‘(x64)’

if ($os_type -eq "True") {
    Write-Host "i am an 64bit OS"
    write-host $os_type }
else {
    $os_type = (Get-WmiObject -Class Win32_ComputerSystem).SystemType -match ‘(x86)’

    if ($os_type -eq "True") {
        Write-Host "i am a 32 Bit OS" }

【讨论】:

    【解决方案2】:

    [System.Environment]::Is64BitProcess 返回真或假,所以这是一个非常简单的if 语句。

    if ([System.Environment]::Is64BitProcess) {
    # Do 64-bit stuff
    } else {
    #Do 32-bit stuff
    }
    

    您没有指定您使用的是“大量示例”中的哪一个,所以我展示了我使用的方法。

    【讨论】:

    • 请注意,这表明 进程 是否为 64 位,不一定是操作系统。如果您在 x64 机器上运行 x86 PowerShell,则会得到不同的结果。根据 OP 想要做什么,这可能很好,甚至可能需要。但值得注意的是。
    • 感谢您的回复!我不想获取有关流程的信息。我需要通过检查处理器架构来知道它是 64 位操作系统还是 32 位操作系统 - 或者是否有更简单的解决方案来获取此信息并在 if 语句中进一步处理?
    • 也许然后[Environment]::Is64BitOperatingSystem
    【解决方案3】:

    感谢您的意见!

    我试过这个:

    感谢您的回答!我尝试了以下示例:

        echo "check if 32 or 64bit OS"
        $var = (Get-WmiObject -Class Win32_ComputerSystem).SystemType -match ‘(x64)’
        if ($var = "True") {echo "i am an 64bit OS"
    
        #setting the current directory as the directory the script is in
        $sourcenssm= Split-Path -Parent $MyInvocation.MyCommand.Definition
    
        #setting the targetdirectory for nssm
        $targetnssm="C:\Users\cp\Desktop\nssm.exe"
    
        #setting the current directory as the directory the script is in
        $sourcewts= Split-Path -Parent $MyInvocation.MyCommand.Definition
    
        #setting the targetdirectory for wtswatchdog
        $targetwts="C:\Windows\wtswatchdog.exe"
    
        #copying nssm to target - WORKING
        if (-not(Test-path $targetnssm)) {Copy-item -Path $sourcenssm\nssm.exe -Destination $targetnssm}
    
        #copying wtswatchdog to target - CHECK!
        if (-not(Test-path $targetwts)) {Copy-item -Path $sourcewts\wtswatchdog.exe -Destination $targetwts}
    }
    

    但如果我正在检查,它每次都会给我 64 位作为输出

     echo "check if 32 or 64bit OS"
                $var = (Get-WmiObject -Class Win32_ComputerSystem).SystemType -match ‘(x86)’
                if ($var = "True") {echo "i am an 64bit OS"}
    

    那么它也将进入回声部分。

    【讨论】:

      【解决方案4】:

      我相信 Alroc 的答案适用于 powershell 3.0 或更高版本。如果另一种方法有问题,那就是:

      $architecture = (Get-WmiObject win32_processor | Where-Object{$_.deviceID -eq "CPU0"}).AddressWidth
      If ($architecture -eq 64) {
          # Do 64-bit stuff
      } else {
          #Do 32-bit stuff
      }
      

      【讨论】:

      • 感谢您的回复:当我尝试您的代码时,它给了我以下错误:Get-WmiObject:无法验证参数“ComputerName”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。
      • 这段代码是从安装脚本中复制的,所以我忘了删除我的 $computer 变量。已更新。
      【解决方案5】:

      也可以使用环境变量。

      $Is64bitProcess = $env:PROCESSOR_ARCHITECTURE -eq 'AMD64'
      $Is64bitOs = $Is64bit = $env:PROCESSOR_ARCHITEW6432 -eq 'AMD64' 
      

      【讨论】:

        猜你喜欢
        • 2021-10-14
        • 1970-01-01
        • 2016-07-30
        • 1970-01-01
        • 1970-01-01
        • 2017-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多