-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoHidSwitch.ps1
129 lines (108 loc) · 3.69 KB
/
AutoHidSwitch.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
param(
[Parameter(Mandatory, HelpMessage="Enter the name of the process that you want to react to")]
[String]$processName,
[Parameter(Mandatory, HelpMessage = "Enter the delay between checks (in seconds)")]
[int]$timeStamp
)
Import-Module Microsoft.PowerShell.Management
Import-Module PnpDevice
# Stage up to Administrator privileges
If (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$Arguments = "& '" + $MyInvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $Arguments
Break
}
function isProcessRunning {
param(
[Parameter(Mandatory)]
[String]$processName
)
process {
try {
$process = Get-Process | where {$_.Name -like $processName}
if($process) {
return $true
}
return $false
}
catch {
return $false
}
}
}
function switchDevicesStatus {
param(
[Parameter(Mandatory)]
[bool]$activateDevices = $true
)
process {
$vendorDevices = (Get-PnpDevice -FriendlyName "*HID-compliant vendor*" -Class "HIDClass")
$consumerDevices = (Get-PnpDevice -FriendlyName "*HID-compliant consumer*" -Class "HIDClass")
if($activateDevices -eq $false) {
foreach($device in $vendorDevices) {
if ($device.Status -eq "OK") {
$id = $device.InstanceId
Disable-PnpDevice "$id" -Confirm:$false
}
}
foreach($device in $consumerDevices) {
if ($device.Status -eq "OK") {
$id = $device.InstanceId
Disable-PnpDevice "$id" -Confirm:$false
}
}
}
else {
foreach($device in $vendorDevices) {
if ($device.Status -eq "Error") {
$id = $device.InstanceId
Enable-PnpDevice "$id" -Confirm:$false
}
}
foreach($device in $consumerDevices) {
if ($device.Status -eq "Error") {
$id = $device.InstanceId
Enable-PnpDevice "$id" -Confirm:$false
}
}
}
}
}
function areDevicesEnabled {
process {
$vendorDevices = (Get-PnpDevice -FriendlyName "*HID-compliant vendor*" -Class "HIDClass")
$consumerDevices = (Get-PnpDevice -FriendlyName "*HID-compliant consumer*" -Class "HIDClass")
foreach($device in $vendorDevices) {
if($device.Status -eq "Error") {
return $false
}
}
foreach($device in $consumerDevices) {
if($device.Status -eq "Error") {
return $false
}
}
return $true
}
}
function mainFunc {
param(
[Parameter(Mandatory)]
[String]$processName,
[int]$timeStep
)
process {
while($true) {
# Wrap this into try/catch and write error to the output
# Also add parameters for the whole script, so user can run this and provide data
[bool]$isRunning = isProcessRunning -processName $processName
[bool]$areDevicesEnabled = areDevicesEnabled
if($isRunning -eq $areDevicesEnabled) {
[bool]$param = -Not $isRunning
switchDevicesStatus -activateDevices $param
}
sleep -Seconds $timeStep
}
}
}
mainFunc -processName $processName -timeStep $timeStamp