如何获取已安装设备的驱动程序密钥?

How can I obtain the driver key for an installed device?

不幸的是,这个问题是 4 年前提出的,但还没有答案 How can I obtain the driver key for a driver programmatically?

其实如果有人回答我就不会再问了

我有一个软件依赖于Naps2扫描仪软件。 我想生成 profiles.xml 文件,它取决于 Driver Key 和 DeviceName

profiles.xml 如下所示:

<?xml version="1.0"?>
<ArrayOfScanProfile xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ScanProfile>
    <Device>
      <ID>{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}[=10=]00</ID>
      <Name>HP Scanjet 300</Name>
    </Device>
    <DriverName>wia</DriverName>
    <DisplayName>HP Scanjet 300</DisplayName>
    <IconID>0</IconID>
    <MaxQuality>false</MaxQuality>
    <IsDefault>true</IsDefault>
    <Version>2</Version>
    <UseNativeUI>false</UseNativeUI>
    <AfterScanScale>OneToOne</AfterScanScale>
    <Brightness>0</Brightness>
    <Contrast>0</Contrast>
    <BitDepth>C24Bit</BitDepth>
    <PageAlign>Right</PageAlign>
    <PageSize>Letter</PageSize>
    <Resolution>Dpi200</Resolution>
    <PaperSource>Glass</PaperSource>
    <EnableAutoSave>false</EnableAutoSave>
    <Quality>75</Quality>
    <AutoDeskew>false</AutoDeskew>
    <BrightnessContrastAfterScan>false</BrightnessContrastAfterScan>
    <ForcePageSize>false</ForcePageSize>
    <ForcePageSizeCrop>false</ForcePageSizeCrop>
    <TwainImpl>Default</TwainImpl>
    <ExcludeBlankPages>false</ExcludeBlankPages>
    <BlankPageWhiteThreshold>70</BlankPageWhiteThreshold>
    <BlankPageCoverageThreshold>25</BlankPageCoverageThreshold>
    <WiaOffsetWidth>false</WiaOffsetWidth>
    <WiaRetryOnFailure>false</WiaRetryOnFailure>
    <WiaDelayBetweenScans>false</WiaDelayBetweenScans>
    <WiaDelayBetweenScansSeconds>2</WiaDelayBetweenScansSeconds>
    <WiaVersion>Default</WiaVersion>
    <FlipDuplexedPages>false</FlipDuplexedPages>
  </ScanProfile>
  <ScanProfile>
    <Device>
      <ID>HP Scanjet 300 TWAIN</ID>
      <Name>HP Scanjet 300 TWAIN</Name>
    </Device>
    <DriverName>twain</DriverName>
    <DisplayName>HP Scanjet 300 TWAIN</DisplayName>
    <IconID>0</IconID>
    <MaxQuality>false</MaxQuality>
    <IsDefault>false</IsDefault>
    <Version>2</Version>
    <UseNativeUI>false</UseNativeUI>
    <AfterScanScale>OneToOne</AfterScanScale>
    <Brightness>0</Brightness>
    <Contrast>0</Contrast>
    <BitDepth>C24Bit</BitDepth>
    <PageAlign>Right</PageAlign>
    <PageSize>Letter</PageSize>
    <Resolution>Dpi200</Resolution>
    <PaperSource>Glass</PaperSource>
    <EnableAutoSave>false</EnableAutoSave>
    <Quality>75</Quality>
    <AutoDeskew>false</AutoDeskew>
    <BrightnessContrastAfterScan>false</BrightnessContrastAfterScan>
    <ForcePageSize>false</ForcePageSize>
    <ForcePageSizeCrop>false</ForcePageSizeCrop>
    <TwainImpl>Default</TwainImpl>
    <ExcludeBlankPages>false</ExcludeBlankPages>
    <BlankPageWhiteThreshold>70</BlankPageWhiteThreshold>
    <BlankPageCoverageThreshold>25</BlankPageCoverageThreshold>
    <WiaOffsetWidth>false</WiaOffsetWidth>
    <WiaRetryOnFailure>false</WiaRetryOnFailure>
    <WiaDelayBetweenScans>false</WiaDelayBetweenScans>
    <WiaDelayBetweenScansSeconds>2</WiaDelayBetweenScansSeconds>
    <WiaVersion>Default</WiaVersion>
    <FlipDuplexedPages>false</FlipDuplexedPages>
  </ScanProfile>
</ArrayOfScanProfile>

在搜索 windows 设备后,我发现 Driver Key {6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0000 在这里:

我需要的是一个 PowerShell 脚本或单行脚本,它可以给我一个驱动程序密钥,例如 "{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0000"零或任何数字,具体取决于连接到机器的扫描仪数量 (NOT Class 仅 GUID {6BDD1FC6-810F-11D0-BEC7-08002BE2092F}),设备名称如下:

Get-WmiObject Win32_PNPEntity | Where-Object { $_.PNPCLass -eq "Image"} | select *

Get-WmiObject Win32_PnPSignedDriver | Where-Object { $_.PNPCLass -eq "Image" } | select *

提前致谢

我想您要找的是 Class GUID。您还标记了 Posh 2/3/4.0,但是使用 Get-CIMInstance 是可行的方法:在 Powershell 3.0 中引入 - Get-CIMInstance - 为您提供与 Get-WMIObject 相同的功能,而没有那么多的安全问题。

Get-CimInstance -Namespace root/cimv2 -ClassName Win32_PNPEntity | Where-Object PNPClass -EQ "Image" | Select-Object -ExpandProperty ClassGuid

另外,尝试包含 -Namespace 参数。即使 Get-CIMInstance cmdlet 指向命名空间的根,它也倾向于更快地查询对象;一定是在做一些事情。

从我的第一个回复中删除所有内容(如果你保留它,它可能对你以后有用)并返回到 Abraham Zinala 指向你的内容并调整它以获得你想要的价值.

# Using WMI
Get-WmiObject -Class win32_pnpEntity

Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"'

(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"')[1] | 
Select-Object -Property '*'

(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"')[1].GetDeviceProperties().DeviceProperties  | 
Select-Object -Property keyName, data | 
Select-Object -First 7
# Results
<#
keyName                      data                                        
-------                      ----                                        
DEVPKEY_Device_DeviceDesc    Local Print Queue                           
DEVPKEY_Device_HardwareIds   {PRINTENUM\LocalPrintQueue}                 
DEVPKEY_Device_CompatibleIds {GenPrintQueue, SWD\GenericRaw, SWD\Generic}
DEVPKEY_Device_Class         PrintQueue                                  
DEVPKEY_Device_ClassGuid     {1ED2BBF9-...}      
DEVPKEY_Device_Driver        {1ed2bbf9-...}[=10=]07 
DEVPKEY_Device_ConfigFlags   0                                           
...
#>


((Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"')[1].GetDeviceProperties().DeviceProperties).Data[7]
# Results
<#
{1ed2bbf9-...}[=10=]07
#>


# Using CIM
Get-CimInstance -ClassName win32_pnpEntity

Get-CimInstance -ClassName  win32_pnpEntity -Filter 'Name like "Brother%"'

(Get-CimInstance -ClassName  win32_pnpEntity -Filter 'Name like "Brother%"')[1]

(Get-CimInstance -ClassName  win32_pnpEntity -Filter 'Name like "Brother%"')[1] | 
Select-Object -Property '*'

# This however will fail
(Get-CimInstance -ClassName  win32_pnpEntity -Filter 'Name like "Brother%"')[1].GetDeviceProperties().DeviceProperties  | 
Select-Object -Property keyName, data | 
Select-Object -First 7
# Results
<#
Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named 'GetDeviceProperties'
#>

# So, get the device Instance ID to pass to another cmdlet
(Get-CimInstance -Namespace root/cimv2 -ClassName Win32_PNPEntity | 
Where-Object PNPClass -like 'Print*')[2] | 
Format-Table -AutoSize
# Results
<#
Status Class      FriendlyName   InstanceId                                          
------ -----      ------------   ----------                                          
OK     PrintQueue Brother...     SWD\PRINTENUM\...}

#>

($DeviceInstanceID = ((Get-CimInstance -Namespace root/cimv2 -ClassName Win32_PNPEntity | 
                    Where-Object PNPClass -like 'Print*')[2]).InstanceId)
# Results
<#
SWD\PRINTENUM\{0A235CEC-...}
#>

注意事项: 并非所有已知的 cmdlet 在所有 PS versions/OS 平台上都可用。

我使用自定义函数根据需要动态获取此类信息。

Get-CmdletVersion -CmdLetName Get-PnpDeviceProperty
# Results
<#
ModuleType Name      CmdletName_FinctionName PowerShellVersion
---------- ----      ----------------------- -----------------
                                                          
Manifest   PnpDevice Get-PnpDeviceProperty   5.1 
#>


Get-PnpDeviceProperty -InstanceId $DeviceInstanceID
# Results
<#
InstanceId KeyName                                   Type       Data                                                       
---------- -------                                   ----       ----                                                       
SWD\PRI... DEVPKEY_Device_DeviceDesc                 String     Local Print Queue                                          
SWD\PRI... DEVPKEY_Device_HardwareIds                StringList {PRINTENUM\LocalPrintQueue}                                
SWD\PRI... DEVPKEY_Device_CompatibleIds              StringList {GenPrintQueue, SWD\GenericRaw, SWD\Generic}               
SWD\PRI... DEVPKEY_Device_Class                      String     PrintQueue                                                 
SWD\PRI... DEVPKEY_Device_ClassGuid                  Guid       {1ED2BBF9-...}                     
SWD\PRI... DEVPKEY_Device_Driver                     String     {1ed2bbf9-...}[=11=]07                
...
#>

(Get-PnpDeviceProperty -InstanceId $DeviceInstanceID)[5].Data
# Results
<#
{1ed2bbf9-...}[=11=]07
#>