首页 > 解决方案 > 如何使用 WMIC 和 Powershell 定位可执行路径

问题描述

我同时使用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 "Programs" 的输出我在程序上使用引号导致它返回,所有 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 返回所有可执行文件的路径,稍后我将过滤掉不需要的东西。

有任何想法吗?

谢谢

标签: python-3.xwindowspowershell

解决方案


这与 powershell 中的“wmic 产品获取名称”相同。这个类往往很慢,因为它会验证每个 msi。

get-ciminstance win32_product | % name

或者你可以这样做(powershell 5.1):

get-package | % name

推荐阅读