Skip to content

Commit fd61b99

Browse files
authored
Add files via upload
1 parent 8d61d01 commit fd61b99

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

Device-Affinity-Checker.ps1

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Check administrator privileges
2+
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
3+
$delay = 2
4+
if (-not $isAdmin) {
5+
[System.Windows.Forms.MessageBox]::Show("Run the script as administrator", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
6+
Start-Sleep -Seconds $delay
7+
Exit
8+
}
9+
Add-Type -AssemblyName System.Windows.Forms
10+
Add-Type -AssemblyName System.Drawing
11+
12+
function Console
13+
{
14+
param ([Switch]$Show,[Switch]$Hide)
15+
if (-not ("Console.Window" -as [type])) {
16+
17+
Add-Type -Name Window -Namespace Console -MemberDefinition '
18+
[DllImport("Kernel32.dll")]
19+
public static extern IntPtr GetConsoleWindow();
20+
21+
[DllImport("user32.dll")]
22+
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
23+
'
24+
}
25+
26+
if ($Show)
27+
{
28+
$consolePtr = [Console.Window]::GetConsoleWindow()
29+
30+
$null = [Console.Window]::ShowWindow($consolePtr, 5)
31+
}
32+
33+
if ($Hide)
34+
{
35+
$consolePtr = [Console.Window]::GetConsoleWindow()
36+
#0 hide
37+
$null = [Console.Window]::ShowWindow($consolePtr, 0)
38+
}
39+
}
40+
41+
function Get-Cores {
42+
param (
43+
[uint32]$TargetSet
44+
)
45+
46+
$CoreNumber = 0
47+
$str = "(invalid assignment)"
48+
$ds = $false
49+
$TS = $TargetSet
50+
51+
while ($TS -gt 0) {
52+
if ($TS % 2 -eq 1) {
53+
if (-not $ds) {
54+
$str = "$CoreNumber"
55+
$ds = $true
56+
} else {
57+
$str += ", $CoreNumber"
58+
}
59+
}
60+
$TS = [math]::Floor($TS / 2)
61+
$CoreNumber++
62+
}
63+
64+
return $str
65+
}
66+
67+
function Load-ListViewData {
68+
$listView.Items.Clear()
69+
$enumKey = "HKLM:\SYSTEM\CurrentControlSet\Enum"
70+
$categoryKeys = Get-ChildItem -Path $enumKey
71+
72+
foreach ($categoryKey in $categoryKeys) {
73+
$deviceKeys = Get-ChildItem -Path $categoryKey.PSPath
74+
foreach ($deviceKey in $deviceKeys) {
75+
$instanceKeys = Get-ChildItem -Path $deviceKey.PSPath
76+
foreach ($instanceKey in $instanceKeys) {
77+
$parametersKeyPath = "$($instanceKey.PSPath)\Device Parameters\Interrupt Management\Affinity Policy - Temporal"
78+
if (Test-Path -Path $parametersKeyPath) {
79+
$temporalKey = Get-Item -Path $parametersKeyPath
80+
if ($temporalKey.GetValue("TargetSet")) {
81+
$targetSet = $temporalKey.GetValue("TargetSet")
82+
$deviceDesc = $instanceKey.GetValue("DeviceDesc")
83+
if ($deviceDesc -like "*;*") {
84+
$deviceDesc = $deviceDesc.Split(";")[1]
85+
}
86+
$cores = Get-Cores -TargetSet $targetSet
87+
$item = New-Object System.Windows.Forms.ListViewItem($deviceDesc)
88+
$item.SubItems.Add($cores)
89+
$listView.Items.Add($item)
90+
}
91+
}
92+
}
93+
}
94+
}
95+
96+
$listView.AutoResizeColumns([System.Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent)
97+
}
98+
99+
# ocultar consola, crear form
100+
Console -Hide
101+
[System.Windows.Forms.Application]::EnableVisualStyles();
102+
$form = New-Object System.Windows.Forms.Form
103+
$form.Text = "Device-Affinity-Checker"
104+
$form.Size = New-Object System.Drawing.Size(800,590)
105+
$form.StartPosition = "CenterScreen"
106+
$form.MaximizeBox = $false
107+
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
108+
109+
$listView = New-Object System.Windows.Forms.ListView
110+
$listView.Size = New-Object System.Drawing.Size(760, 500)
111+
$listView.Location = New-Object System.Drawing.Point(10, 10)
112+
$listView.View = [System.Windows.Forms.View]::Details
113+
$listView.FullRowSelect = $true
114+
$listView.GridLines = $true
115+
116+
$columnHeader1 = New-Object System.Windows.Forms.ColumnHeader
117+
$columnHeader1.Text = "Device Description"
118+
$columnHeader1.Width = -2
119+
$columnHeader2 = New-Object System.Windows.Forms.ColumnHeader
120+
$columnHeader2.Text = "Affinity"
121+
$columnHeader2.Width = -2
122+
123+
$listView.Columns.AddRange(@($columnHeader1, $columnHeader2))
124+
$form.Controls.Add($listView)
125+
126+
$refreshButton = New-Object System.Windows.Forms.Button
127+
$refreshButton.Text = "Refresh"
128+
$refreshButton.Size = New-Object System.Drawing.Size(75, 23)
129+
$refreshButton.Location = New-Object System.Drawing.Point(10, 520)
130+
$refreshButton.Add_Click({ Load-ListViewData })
131+
$form.Controls.Add($refreshButton)
132+
133+
# cargar datos iniciales + flujo de salida nulo
134+
Load-ListViewData | Out-Null
135+
136+
[void]$form.ShowDialog()

0 commit comments

Comments
 (0)