regex - 从 UninstallString 中提取 GUID
问题描述
我想了解安装在 Windows 系统上的任何软件(或至少所有在 Windows 中注册的软件)。使用 Powershell,我可以将数据提取到 gridview 中,然后过滤到 csv 文件中。为此,我使用以下代码:
### Extract x64 registered programs. Excluding KB updates
$data = dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
where {$_.name -notmatch '(\.)?KB\d+'} -pv p |
Get-ItemProperty |
Where {$_.displayname -notmatch "KB\d{5,}"} |
Select @{Name="Path";Expression={$p.name}},Displayname,DisplayVersion,InstallDate,UninstallString
;
### Extract and append to $data the x86 registered programs. Excluding KB updates
$data += dir HKLM:\SOFTWARE\Wow6432Node\Micsosoft\Windows\CurrentVersion\Uninstall |
where {$_.name -notmatch '(\.)?KB\d+'} -pv p |
Get-ItemProperty |
Where {$_.displayname -notmatch "KB\d{5,}"} |
Select @{Name="Path";Expression={$p.name}},DisplayName,DisplayVersion,InstallDate,UninstallString
;
### Acces $data and output to a gridview and further to csv.
$data |
select-Object DisplayName,DisplayVersion,InstallDate,UninstallString |
sort-object -Property DisplayName |
out-gridview -PassTHru |
export-csv -delimiter "," -Path C:\temp\software.csv
示例输出如下所示:
DisplayName DisplayVersion InstallDate UninstallString
7-Zip 9.20 (x64 edition) 9.20.00.0 20190827 MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}
QGIS 3.4.13 'Madeira' 03.04.2013 C:\Program Files\QGIS 3.4\uninstall.exe
Microsoft Office Access ... 14.0.7015. 20190827 MsiExec.exe /X{90140000-0015-0407-0000-0000000FF1CE}
Realtek Card Reader 10.0.10125.... 20190827 "C:\Program Files (x86)\InstallShield Installation Information\{5BC2B5AB-80DE-4E83-B8CF-426902051D0A}\setup.exe" -runfromtemp -removeonly
为了进一步处理,我想附加一列,其中 GUID(如果存在)从 UninstallString 中提取。例子:
DisplayName DisplayVersion InstallDate GUID UninstallString
7-Zip 9.20 (x64 edition) 9.20.00.0 20190827 23170F69-40C1-2702-0920-000001000000 MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}
QGIS 3.4.13 'Madeira' 03.04.2013 C:\Program Files\QGIS 3.4\uninstall.exe
Microsoft Office Access ... 14.0.7015. 20190827 90140000-0015-0407-0000-0000000FF1CE MsiExec.exe /X{90140000-0015-0407-0000-0000000FF1CE}
Realtek Card Reader 10.0.10125.... 20190827 5BC2B5AB-80DE-4E83-B8CF-426902051D0A "C:\Program Files (x86)\InstallShield Installation Information\{5BC2B5AB-80DE-4E83-B8CF-426902051D0A}\setup.exe" -runfromtemp -removeonly
我知道我必须以某种方式使用 RegEx 代码进行操作,但我无法继续进行。有人可以帮助我吗?谢谢
解决方案
您可以使用正则表达式从 UninstallString 中提取 GUID -match
,并将其作为计算属性添加到Select-Object
:
$reGuid = '\{?(([0-9a-f]){8}-([0-9a-f]){4}-([0-9a-f]){4}-([0-9a-f]){4}-([0-9a-f]){12})\}?'
$data | Select-Object DisplayName,DisplayVersion,InstallDate,
@{Name = 'GUID'; Expression = { if ($_.UninstallString -match $reGuid) {$matches[1]}} },
UninstallString |
Sort-Object -Property DisplayName |
Out-GridView -PassThru |
Export-Csv -Delimiter "," -Path 'C:\temp\software.csv' -NoTypeInformation
正则表达式详细信息:
\{ Match the character “{” literally ? Between zero and one times, as many times as possible, giving back as needed (greedy) ( Match the regular expression below and capture its match into backreference number 1 ( Match the regular expression below and capture its match into backreference number 2 [0-9a-f] Match a single character present in the list below A character in the range between “0” and “9” A character in the range between “a” and “f” ){8} Exactly 8 times - Match the character “-” literally ( Match the regular expression below and capture its match into backreference number 3 [0-9a-f] Match a single character present in the list below A character in the range between “0” and “9” A character in the range between “a” and “f” ){4} Exactly 4 times - Match the character “-” literally ( Match the regular expression below and capture its match into backreference number 4 [0-9a-f] Match a single character present in the list below A character in the range between “0” and “9” A character in the range between “a” and “f” ){4} Exactly 4 times - Match the character “-” literally ( Match the regular expression below and capture its match into backreference number 5 [0-9a-f] Match a single character present in the list below A character in the range between “0” and “9” A character in the range between “a” and “f” ){4} Exactly 4 times - Match the character “-” literally ( Match the regular expression below and capture its match into backreference number 6 [0-9a-f] Match a single character present in the list below A character in the range between “0” and “9” A character in the range between “a” and “f” ){12} Exactly 12 times ) \} Match the character “}” literally ? Between zero and one times, as many times as possible, giving back as needed (greedy)
推荐阅读
- javascript - 如何找到两个日期之间的差异?
- swift - 如何从 Swift 中的同一字符串中获取字符串的特定部分?
- spring - 由于创建名为 adminHandlerMapping 的 bean 时出错,Spring-Boot-Admin 应用程序无法启动
- c# - Polly电路关闭时如何实现自定义方法
- ruby-on-rails - RSpec:对于在我调用的函数中实例化的对象,我怎么能不使用`allow_any_instance_of`?
- c++ - 如何根据用户输入创建模板对象?
- postgresql - 无法在 OVH postgres 中创建新架构
- matlab - 从图中的轴获取当前选定的数据点
- mysql - 显示匹配字段外键和主键
- java - 如何在 Spring Boot 或 Java 中将 json 响应列表合并到另一个列表中?