[Environment]::Is64BitProcess 和 [Environment]::Is32BitProcess 什么都不返回
[Environment]::Is64BitProcess and [Environment]::Is32BitProcess is returning nothing
我试图使用 PowerShell 检测计算机的体系结构 - 32 位或 64 位。
我曾经使用这个条件,对于 64 位:
if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem){}
我曾经使用这个条件,对于 32 位:
if ([Environment]::Is32BitProcess -ne [Environment]::Is32BitOperatingSystem){}
但最近它不能在 PC 上运行,所以我尝试调试它,但它一次就失败了。所以我打开 PowerShell 并尝试获取这些命令的 returns。事实证明它没有 return 任何 Environment
范围变量。
任何人都可以向我解释为什么会发生这种情况,以及如何解决这个问题吗?
您可以使用一个使用 [Environment]::Is64BitOperatingSystem
的小辅助函数,但如果不可用,则恢复为 WMI:
function Get-Architecture {
# What bitness does Windows use
switch ([Environment]::Is64BitOperatingSystem) { # needs .NET 4
$true { 64; break }
$false { 32; break }
default {
(Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture -replace '\D'
# Or do any of these:
# (Get-WmiObject -Class Win32_ComputerSystem).SystemType -replace '\D' -replace '86', '32'
# (Get-WmiObject -Class Win32_Processor).AddressWidth # this is slow...
}
}
}
Get-Architecture # returns either `64` or `32`
要同时拥有 return PowerShell 进程的位数,您可以将其扩展为
function Get-Architecture {
# What bitness does Windows use
$windowsBitness = switch ([Environment]::Is64BitOperatingSystem) { # needs .NET 4
$true { 64; break }
$false { 32; break }
default {
(Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture -replace '\D'
# Or do any of these:
# (Get-WmiObject -Class Win32_ComputerSystem).SystemType -replace '\D' -replace '86', '32'
# (Get-WmiObject -Class Win32_Processor).AddressWidth # slow...
}
}
# What bitness does this PowerShell process use
$processBitness = [IntPtr]::Size * 8
# Or do any of these:
# $processBitness = $env:PROCESSOR_ARCHITECTURE -replace '\D' -replace '86|ARM', '32'
# $processBitness = if ([Environment]::Is64BitProcess) { 64 } else { 32 }
# return the info as object
New-Object -TypeName PSObject -Property @{
'ProcessArchitecture' = "{0} bit" -f $processBitness
'WindowsArchitecture' = "{0} bit" -f $windowsBitness
}
}
我试图使用 PowerShell 检测计算机的体系结构 - 32 位或 64 位。
我曾经使用这个条件,对于 64 位:
if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem){}
我曾经使用这个条件,对于 32 位:
if ([Environment]::Is32BitProcess -ne [Environment]::Is32BitOperatingSystem){}
但最近它不能在 PC 上运行,所以我尝试调试它,但它一次就失败了。所以我打开 PowerShell 并尝试获取这些命令的 returns。事实证明它没有 return 任何 Environment
范围变量。
任何人都可以向我解释为什么会发生这种情况,以及如何解决这个问题吗?
您可以使用一个使用 [Environment]::Is64BitOperatingSystem
的小辅助函数,但如果不可用,则恢复为 WMI:
function Get-Architecture {
# What bitness does Windows use
switch ([Environment]::Is64BitOperatingSystem) { # needs .NET 4
$true { 64; break }
$false { 32; break }
default {
(Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture -replace '\D'
# Or do any of these:
# (Get-WmiObject -Class Win32_ComputerSystem).SystemType -replace '\D' -replace '86', '32'
# (Get-WmiObject -Class Win32_Processor).AddressWidth # this is slow...
}
}
}
Get-Architecture # returns either `64` or `32`
要同时拥有 return PowerShell 进程的位数,您可以将其扩展为
function Get-Architecture {
# What bitness does Windows use
$windowsBitness = switch ([Environment]::Is64BitOperatingSystem) { # needs .NET 4
$true { 64; break }
$false { 32; break }
default {
(Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture -replace '\D'
# Or do any of these:
# (Get-WmiObject -Class Win32_ComputerSystem).SystemType -replace '\D' -replace '86', '32'
# (Get-WmiObject -Class Win32_Processor).AddressWidth # slow...
}
}
# What bitness does this PowerShell process use
$processBitness = [IntPtr]::Size * 8
# Or do any of these:
# $processBitness = $env:PROCESSOR_ARCHITECTURE -replace '\D' -replace '86|ARM', '32'
# $processBitness = if ([Environment]::Is64BitProcess) { 64 } else { 32 }
# return the info as object
New-Object -TypeName PSObject -Property @{
'ProcessArchitecture' = "{0} bit" -f $processBitness
'WindowsArchitecture' = "{0} bit" -f $windowsBitness
}
}