首页 > 解决方案 > 我可以使用 PowerShell 比较多台计算机吗?具体找出哪些有特定的程序,哪些没有

问题描述

我需要比较多台计算机,找出哪些具有特定程序,哪些不使用 PowerShell。

我对此非常陌生(大约一周的 PowerShell 经验),因此将不胜感激。

标签: powershell

解决方案


是的,您可以远程检查是否安装了软件:

# We need to check for both 64-bit and 32-bit software
$regPaths = "HKLM:\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall",
  "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

# Get the name of all installed software registered in the registry with Office in the name
# (you can search for exact strings if you know them for specific versions)
$regPaths | Foreach-Object {
  ( Get-ItemProperty "${_}\*" DisplayName -EA SilentlyContinue ).DisplayName | Where-Object {
    $_ -match 'ADD_REMOVE_PROGRAMS_NAME'
  }
}

它的工作原理是检查已安装软件的 32 位和 64 位注册表。在 行下$_ -match 'ADD_REMOVE_PROGRAMS_NAME'ADD_REMOVE_PROGRAMS_NAME应替换为“添加/删除程序”中的任何软件名称。请注意,此技术不适用于某些通过基于 EXE 的安装程序安装的程序。

您也可以在多台计算机上远程运行它Invoke-Command

$computers = Get-Content "C:\Path\To\File\With\Computer_Names.txt"
$results = Invoke-Command -ComputerName $computers {
    # We need to check for both 64-bit and 32-bit software
    $regPaths = "HKLM:\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall",
      "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

    # Get the name of all installed software registered in the registry with Office in the name
    # (you can search for exact strings if you know them for specific versions)
    $regPaths | Foreach-Object {
      ( Get-ItemProperty "${_}\*" DisplayName -EA SilentlyContinue ).DisplayName | Where-Object {
        $_ -match 'ADD_REMOVE_PROGRAMS_NAME'
      }
    }
}

Invoke-Command可以同时在多台计算机上运行嵌入式ScriptBlock。当列表中的所有计算机都运行该命令时,您可以检查每台计算机的结果(它以数组形式返回,因此您可以检查PSCOMPUTERNAME每台计算机的属性$result以查看哪些计算机没有该软件。上面的代码的工作方式,它应该在拥有它的计算机上返回一个对象,而在没有它的计算机上什么也不返回。


推荐阅读