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
以查看哪些计算机没有该软件。上面的代码的工作方式,它应该在拥有它的计算机上返回一个对象,而在没有它的计算机上什么也不返回。
推荐阅读
- android - 显示 Snackbar 时布局会缩小内容
- python - 包含微平均和宏观平均 F1 分数的表
- c# - Is it possible to have two or more active modal windows at the same time?
- javascript - PHP returns whitespace character before anything after AJAX request
- javascript - 如何将 python 语法荧光笔应用于 django 模型 TextField
- configuration-files - 用于自动化属性文件的配置管理工具
- c# - 如何替换主 XML 文件中的值并将其保存为 C# 中的新文件?
- excel - 运行时错误 424:VBA 中的“需要对象”错误
- c# - 我应该缓存 SQL 数据库数据并在本地查询吗?还是让应用程序直接查询数据库?
- reactjs - 无法使用 redux 对未安装的组件执行 React 状态更新