不幸的是,这个问题是在 4 年前提出的,但目前还没有答案 如何以编程方式获取驱动程序的驱动程序密钥?
其实如果有答案我就不会再问了。
我有一个依赖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}\0000</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 设备后,我发现驱动程序密钥 {6BDD1FC6-810F-11D0-BEC7-08002BE2092F} 在这里:
我需要的是一个 PowerShell 脚本或单行脚本,为我提供一个驱动程序密钥,例如 "{6BDD1FC6-810F-11D0-BEC7-08002BE2092F} ",其中包含零或任何数字,具体取决于连接到计算机的扫描仪数量 (仅限类 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 *
提前致谢
删除我第一个回复中的所有内容(如果您保留它,以后可能对您有用),然后回到
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-...}\0007
DEVPKEY_Device_ConfigFlags 0
...
#>
((Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"')[1].GetDeviceProperties().DeviceProperties).Data[7]
# Results
<#
{1ed2bbf9-...}\0007
#>
# 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 版本/操作系统平台上都可用。
我使用自定义函数根据需要动态获取此类信息。
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-...}\0007
...
#>
(Get-PnpDeviceProperty -InstanceId $DeviceInstanceID)[5].Data
# Results
<#
{1ed2bbf9-...}\0007
#>
我猜你想要的是班级 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 指向命名空间的根,它也往往会更快地查询对象;一定是在幕后做某事。
我刚刚回答了您提到的原始问题,但为了完整起见,我再次回答了。 我最初是为网络适配器编写的,但我认为它可以轻松适应(没有双关语)任何其他设备,包括扫描仪。 我花了很多时间进行研究,所以我希望其他人可能会发现这很有用。
$devObjs = Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"'
foreach ($dev in $devObjs)
{
$guid = $dev.GetDeviceProperties().DeviceProperties | where KeyName -eq 'DEVPKEY_Device_Driver' | select -ExpandProperty 'Data'
Write-Host $guid
}
或者只是...
foreach ($dev in Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "Brother%"')
{
$guid = $dev.GetDeviceProperties().DeviceProperties | where KeyName -eq 'DEVPKEY_Device_Driver' | select -ExpandProperty 'Data'
Write-Host $guid
}