【问题标题】:powershell determine os architecturepowershell 确定操作系统架构
【发布时间】:2022-01-22 10:55:50
【问题描述】:

我对这段代码有疑问。它打印“x86 操作系统”,即使写入主机 $OSArchitecture 声明架构是 64 位。

$OSArchitecture = (Get-WmiObject -Class Win32_OperatingSystem | Select-Object    OSArchitecture -ErrorAction Stop).OSArchitecture
write-host = $OSArchitecture

if ($OSArchitecture -eq '*64*') 
{
    Write-Host "x64 operating system" 
    $Version = Get-ChildItem hklm:\software\wow6432node\microsoft\windows\currentversion\uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where-Object {
    $_.DisplayName -Eq 'Microsoft Lync 2013'} | Select-Object DisplayVersion
} 
else 
{
    Write-Host "x86 operating system"
    $Version = Get-ChildItem hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where-Object {
    $_.DisplayName -Eq 'Microsoft Lync 2013'} | Select-Object DisplayVersion 
}

更新: 我收到此错误:Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32"

【问题讨论】:

  • 您不能将通配符与-eq 运算符一起使用。您可以使用-like '*64*'-match '64'
  • 如果你要使用通配符(*),那么操作符应该是-Like而不是-eq
  • 我仍然得到:无法将“System.Object[]”类型的“System.Object[]”值转换为“System.Int32”类型。在 line:6 char:21
  • 如果我删除 Wow6432Node,它确实有效.. 有什么想法吗?
  • @Bendo1984 你能在你的问题中添加完整的错误吗?

标签: powershell


【解决方案1】:

在 x64 系统上,[Environment]::Is64BitOperatingSystem 提供 $true。因此,您可以这样做:

if ([Environment]::Is64BitOperatingSystem) 
{
    Write-Host "x64 operating system" 
    $Version = Get-ChildItem hklm:\software\wow6432node\microsoft\windows\currentversion\uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where-Object {
    $_.DisplayName -Eq 'Microsoft Lync 2013'} | Select-Object DisplayVersion
} 
else 
{
    Write-Host "x86 operating system"
    $Version = Get-ChildItem hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where-Object {
    $_.DisplayName -Eq 'Microsoft Lync 2013'} | Select-Object DisplayVersion 
}

【讨论】:

  • 你将如何在我的脚本中使用它?
  • @Bendo1984 我更新了 Novakov 的答案以显示用法。希望这会有所帮助。
【解决方案2】:

更正的代码。

$OSArchitecture=Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop | Select-Object -ExpandProperty OSArchitecture
if ($OSArchitecture -eq "64-bit") {
  Write-Output "x64 operating system" 
  $Path="HKLM:\software\wow6432node\microsoft\windows\currentversion\uninstall"
} 
else {
  Write-Output "x86 operating system"
  $Path="HKLM:\software\microsoft\windows\currentversion\uninstall"
}

$Version=Get-ChildItem -Path "HKLM:\software\wow6432node\microsoft\windows\currentversion\uninstall" | ForEach-Object { Get-ItemProperty $_.pspath } | Where-Object { $_.DisplayName -eq 'Microsoft Lync 2013'} | Select-Object -ExpandProperty DisplayVersion

【讨论】:

    【解决方案3】:

    你可以用这个来做这个工作

    if ([System.IntPtr]::Size -eq 4) { "32-bit" } else { "64-bit" }
    

    或更改您的代码

    if ($OSArchitecture -match '64') 
    

    【讨论】:

    • 我仍然得到:无法将“System.Object[]”类型的“System.Object[]”值转换为“System.Int32”类型。在 line:6 char:21
    • 如果我删除 Wow6432Node,它确实有效.. 有什么想法吗?
    • 不表示操作系统是x64还是x86。它仅显示进程位数。在 PowerShell x86 中键入它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2010-09-05
    • 2014-04-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2010-12-05
    相关资源
    最近更新 更多