我同时使用python和Powershell,但我弄不清楚两者之间的差异。显然,一个使用的是WMIC,另一个使用的是Get-ItemProperty -path。
如何从WMIC获取所有可执行文件的路径。Python示例如下:
if use_cached_program_list is False:
# traverse the software list
Data = subprocess.check_output(['wmic', 'product', 'get', 'name'])
program_list = sorted(str(Data).split("\\r\\r\\n"))
# Filter out string that contain no alphanumeric characters
program_list = [x.strip() for x in program_list if re.search('[a-zA-Z0-9]', x) is not None]
# Get absolute path of current file
sp = pathlib.Path(__file__).parent.absolute()
cached_file_path = os.path.join(sp, "Caches", cache_file)
with open(cached_file_path, "w") as fs:
# arrange the string and output to file
for i in range(0, len(program_list)):
item = program_list[i]
fs.write(item + "\n")返回477个“程序”的输出,我在程序上使用引号,因为它返回所有sdk的,无论是扩展还是运行时,如下所示:
Active Directory Authentication Library for SQL Server
Adobe Acrobat Reader DC
Adobe Refresh Manager
Advanced IP Scanner 2.5
Application Verifier x64 External Package
Application Verifier x64 External Package
Application Verifier x64 External Package
Application Verifier x64 External Package当我使用Powershell时,Get-ItemProperty和搜索注册表的代码如下:
Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\*" | Where-Object {$q + $_."(default)" -ne $null} | Select-Object @{ expression={$_.PSChildName}; label='Program'} ,@{ expression={$q + $_."(default)" +$q}; label='CommandLine'} | Export-Csv -Path .\programs.csv -Encoding ascii -NoTypeInformation我没有更新上面的代码来删除所有的双引号,但仍然有相同的结果。
要删除所有双引号,代码为:
Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\*" | Where "(default)" -ne $null |
Select @{ E={$_.PSChildName}; N='Program'} ,@{ E={$_."(default)".Trim('"')}; N='CommandLine'} |
export-csv .\programs.csv -Encoding ascii -NoType下面是输出:
"7zFM.exe","C:\Program Files\7-Zip\7zFM.exe"
"AcroRd32.exe","C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"
"Adobe Audition CC.exe","""C:\Program Files\Adobe\Adobe Audition CC 2019\Adobe Audition CC.exe"""
"Adobe Media Encoder.exe","""C:\Program Files\Adobe\Adobe Media Encoder CC 2019\Adobe Media Encoder.exe"""
"Adobe Premiere Pro.exe","""C:\Program Files\Adobe\Adobe Premiere Pro CC 2019\Adobe Premiere Pro.exe"""
"AfterFX.exe","C:\Program Files\Adobe\Adobe After Effects CC 2019\Support Files\AfterFX.exe"这导致只安装了75个应用程序。原因是它检查卸载项,据我所知,似乎许多开发软件的公司根本不遵守微软关于注册表项的政策。或者没有使用MSI安装程序。
现在,在任何人开始说搜索所需的注册表项之前,
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*无论我是使用这两个注册表路径中的一个,还是同时使用这两个路径,它们返回的结果与32位和64位完全相同。
目标:
我正在尝试让WMIC返回所有可执行文件的路径,稍后我会过滤掉不需要的内容。
有什么想法吗?
谢谢
发布于 2020-11-30 11:37:08
这和powershell中的"wmic product get name“是一样的。这个类往往很慢,因为它验证每个msi。
get-ciminstance win32_product | % name或者你可以这样做(powershell 5.1):
get-package | % namehttps://stackoverflow.com/questions/65066001
复制相似问题