-
Notifications
You must be signed in to change notification settings - Fork 0
/
lspci.ps1
172 lines (156 loc) · 6.29 KB
/
lspci.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<#
PCIUtils FOR POWERSHELL
This is open-source software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SWARM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
If you use my code, please include my github: https://github.com/maynardminer
If you find it useful - Consider a BTC donation: 1FpuMha1QPaWS4PTPZpU1zGRzKMevnDpwg
#>
Using namespace Microsoft.Win32;
## A single device
class Device {
[string]$HardwareID
[string]$CompatIds
[string]$iRev
[string]$iTitle
[string]$iVendor
[string]$imanufacturer
[string]$iDevice
[string]$iDeviceId
[string]$iLocation
Device([string]$id, [string]$loc, [string]$compat) {
$this.HardwareID = $id;
$this.CompatIds = $compat;
$location_map = $loc.split(";")[2].TrimStart("(").TrimEnd(")").split(",");
[int]$get_busid = $location_map[0];
[int]$get_deviceID = $location_map[1]
[int]$get_functionId = $location_map[2]
$new_busid = "{0:x2}" -f $get_busid
$new_deviceID = "{0:x2}" -f $get_deviceID
$this.iLocation = "$new_busid`:$new_deviceID`.$get_functionId"
}
}
## USE CIM to get to PnP Devices- To my knowledge this should not require administrator,
## but you know...Windows...
## Scratch that- Get-CimInstace Win32_PnPEntity is garbage. I found it missing PCI devices.
#$Devices = Get-CimInstance -class Win32_PnPEntity | Where { $_.PNPDeviceID -match "PCI\\*" } | Select -Unique
## Use registry entries instead
Set-Location (Split-Path $script:MyInvocation.MyCommand.Path)
. .\device\pci_ids.ps1
$Devices = @()
$PCI_List = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Enum\PCI"
$Registry = [RegistryKey]::OpenBaseKey([RegistryHive]::LocalMachine, [RegistryView]::Default).OpenSubKey("SYSTEM\CurrentControlSet\Enum\PCI")
$PCI_List = $Registry.GetSubKeyNames()
foreach ($item in $PCI_List) {
$pci_key = $Registry.OpenSubKey($item)
$sub_names = $pci_key.GetSubKeyNames()
foreach ($sub_value in $sub_names) {
$Sub_key = $pci_key.OpenSubKey($sub_value)
$key_names = $Sub_key.GetSubKeyNames()
if ("Control" -in $key_names -or $key_names.count -eq 1) {
$names = $Sub_key.GetValueNames()
if (
"HardwareID" -in $names -and
"LocationInformation" -in $names -and
"CompatibleIDs" -in $names
) {
$HID = $sub_key.GetValue("HardwareID") | Where { $_ -like "*REV*" } | Select -First 1;
$LOC = $sub_key.GetValue("LocationInformation") | Select -First 1
$COM = $sub_key.GetValue("CompatibleIDs") | Where { $_ -like "*CC*" } | Select -First 1;
$Devices += [Device]::New($HID, $LOC, $COM)
}
}
}
}
## This is a json list of pci.ids.
$pci_id = [PCI_ID]::New()
## IF using parsable argument to get a single device
if ($args[0] -eq "-vmms") {
$Devices = $Devices | Where ilocation -eq $args[1]
}
else {
$Devices = $Devices | Where ilocation -ne $null
}
## I only have so many devices to test with
## but so far I haven't found a device yet
## that doesn't match pci.ids
foreach ($Device in $Devices) {
$IDs = $Device.HardwareID.Split('&')
$vendorId = $IDs[0].Substring($IDs[0].Length - 4)
$deviceId = $IDs[1].Substring($IDs[1].Length - 4)
$deviceSubsysId = $IDs[2].Substring($IDs[2].Length - 4) + ' ' + $IDs[2].Substring($IDs[2].Length - 8, 4)
$manufacturerId = $IDs[2].Substring($IDs[2].Length - 4)
$vendor = $pci_id.info.keys | Where { $_.substring(0, 4) -eq $vendorId }
$device_name = $pci_id.info.$vendor.keys | Where { $_.substring(0, 4) -eq $deviceId }
if ($device_name -and $vendor) {
$ideviceSubsys = $pci_id.info.$vendor.$device_name.$deviceSubsysId
}
if ($null -eq $ideviceSubsys) {
$ideviceSubsys = if ($device_name) { $device_name.split(" ")[1] } else { "Device $deviceId" }
}
if ($device_name) {
$sub_device_id = "Device $($device_name.split(" ")[0])"
}
else {
$sub_device_id = "Device $deviceId"
}
$manufacturer = $pci_id.info.keys | Where { $_.substring(0, 4) -eq $manufacturerId }
$CC = $device.CompatIds | Where { $_ -like "*CC_*" } | Select -First 1
$CC = $CC.Substring(13 + 3, 4)
$Code = $CC.Substring(0, 2)
$Code_Id = $CC.Substring(2, 2)
$title = $pci_id.info.keys | Where { $_.substring(0, 4) -eq "C $Code" }
if ($pci_id.info.$title.keys) {
$get_title = $pci_id.info.$title.keys | Where { $_.substring(0, 2) -eq $Code_Id }
if($get_title) {
$title = $get_title
}
}
if ($null -eq $vendor) {
$vendor = $sub_device_id
}
else {
$vendor = $vendor.split(" ")[1]
}
$rev = $Device.HardwareID.IndexOf("REV_")
$revision = $Device.HardwareID.substring($rev + 4, 2)
$new_rev = "{0:x2}" -f $revision
$Device.Irev = $new_rev.ToLower()
$Device.iTitle = ($title.split(" ")[1])
$Device.iVendor = $vendor
$Device.IDevice = $ideviceSubsys
$Device.IDeviceId = $sub_device_id
$Device.iManufacturer = if ($manufacturer) { ($manufacturer.split(" ")[1]) }
}
## Print single view
if ($args[0] -eq "-vmms") {
$Devices | % {
Write-Host "Slot:`t$($_.ilocation)"
Write-Host "Class:`t$($_.ititle)"
Write-Host "Vendor:`t$($_.ivendor)"
Write-Host "Device:`t$($_.IDevice)"
if ($_.imanufacturer) {
Write-Host "SVendor:`t$($_.imanufacturer)"
Write-Host "SDevice:`t$($_.IDeviceId)"
}
Write-Host "Rev:`t$($_.irev)"
}
}
## Print list just like PCIUtils
else {
$Devices | Sort-Object ilocation | ForEach-Object {
$a = " $($_.IDevice)"
$b = " (rev $($_.irev))"
Write-Host "$($_.ilocation) $($_.ititle): $($_.ivendor)$a$b"
}
}
## I have found this to be slightly slower than lscpi
## mainly because its not using compiled code,
## but still seems to work fine for me.