检查处理器架构并继续执行if语句



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

这应该得到在x64

上具有true或false检查的类型

我的问题是:我如何得到这个输出到一个if语句?

示例:如果是64位处理器,则执行几个步骤,如果是32位处理器,则执行其他步骤。我怎样才能走得更远?

我尝试了几个版本的代码,但也得到了真或假的回来,这是可以的,但如何去进一步?

你们能帮我一下吗?

谢谢

谢谢大家。

我用下面的方法解决了它:

$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" }

[System.Environment]::Is64BitProcess返回true或false,因此这是一个非常简单的if语句。

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

你没有说明你使用的是"许多例子"中的哪一个,所以我展示了我使用的方法。

也可以使用环境变量

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

谢谢!

我试过了,例如:

谢谢你的回答!例如,我尝试如下:

    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:UserscpDesktopnssm.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:Windowswtswatchdog.exe"
    #copying nssm to target - WORKING
    if (-not(Test-path $targetnssm)) {Copy-item -Path $sourcenssmnssm.exe -Destination $targetnssm}
    #copying wtswatchdog to target - CHECK!
    if (-not(Test-path $targetwts)) {Copy-item -Path $sourcewtswtswatchdog.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"}

那么它也会进入回声部分

我相信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
}

最新更新