powershell - 使用通配符比较两个数组
问题描述
我正在尝试比较计算机上的添加/删除程序,并希望将其与文件共享上的软件列表进行比较,以便帮助台知道他们需要安装哪些许可软件。我创建了以下脚本,由于某种原因,我收到了我缺少括号的错误。我认为语法很好,或者我错过了什么?
我真的很感谢你的帮助
$approvedSoftware = @(Get-ChildItem -Path '\\fileshare\Applications'| Select-Object Name)
$computer= @(Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName)
foreach ($program in $computer)
{
if ("*$program*" -like $approvedSoftware)
{
Write-Host "$($program)"
}
}
解决方案
我已经从评论中稍微更新了您建议的输入数据,因此两个数组中都有 1 个相同
样本输入数据:
$computer = "LogMeIn Rescue Technician Console;Microsoft Visual C++ 2013,Redistributable (x64) - 12.0.40649;Microsoft Visual C++ 2019 X86 Additional Runtime - 14.28.29910;MSXML 4.0 SP2 Parser;SDK".split(";")
$approvedSoftware = "Microsoft .NET Framework 4.7.1;Microsoft C++ Build Tools 2019;Microsoft Office Professional Plus 2016;Microsoft Office Project Professional 2010;Microsoft Visual C++ 2013,Redistributable (x64) - 12.0.40649".split(";")
输入数据需要是 2 个字符串数组或具有相同属性名称的对象进行比较。
在这些数组上运行Compare-Object
后,您可以选择任何标准来根据SideIndicator
属性过滤结果:
$Result = Compare-Object $computer $approvedSoftware -IncludeEqual
$Result
InputObject SideIndicator
----------- -------------
Microsoft Visual C++ 2013,Redistributable (x64) - 12.0.40649 ==
Microsoft .NET Framework 4.7.1 =>
Microsoft C++ Build Tools 2019 =>
Microsoft Office Professional Plus 2016 =>
Microsoft Office Project Professional 2010 =>
LogMeIn Rescue Technician Console <=
Microsoft Visual C++ 2019 X86 Additional Runtime - 14.28.29910 <=
MSXML 4.0 SP2 Parser <=
SDK <=
$Result | ?{$_.SideIndicator -eq "=="}
InputObject SideIndicator
----------- -------------
Microsoft Visual C++ 2013,Redistributable (x64) - 12.0.40649 ==
根据您列出已安装软件的代码和批准列表中的代码,您需要执行以下操作:
$approvedSoftware = (Get-ChildItem -Path '\\fileshare\Applications').Name
$computer = (Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*).DisplayName
$installedApprovedSoftware = (Compare-Object $computer $approvedSoftware -IncludeEqual `
| Where {$_.SideIndicator -eq "=="}).InputObject
$installedApprovedSoftware | Foreach {Write-Host $_}
Microsoft Visual C++ 2013,Redistributable (x64) - 12.0.40649
我不确定您对基于输入数据的通配符的要求,但提供了有关如何-like
使用通配符的参考。
参考:
Compare-Object
文档:
通配符比较的详细答案:
推荐阅读
- php - CodeIgniter 自动注销用户
- angular - Angular 6 - 如何从打字稿中提取翻译
- javascript - 如何在输入类型时间内设置自动对焦
- powershell - 用于从远程计算机中删除几个本地用户的 Powershell 脚本
- selenium - 如何为找到的 xpath 搜索父级(具有某些父级属性)?
- android - 这个布局是否嵌套了权重?
- java - glassfish 5 无法开始使用命令行
- jquery - 当日期为 0000-00-00 时设置 flatpickr 文本
- google-cloud-platform - 为 Google Cloud Storage 中存储桶中的所有对象设置元数据
- c# - WPF 用户控件绑定