-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlw.ps1
157 lines (134 loc) · 6.62 KB
/
lw.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
###############################################################################
# LEGION WATCHER
# -----------------------------------------------------------------------------
# Watches for WMI Events and runs associated Batch and Powershell scripts
###############################################################################
$host.ui.RawUI.WindowTitle = "Legion Watcher"
$ScriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
$UserScriptsDir = "$ScriptDir\scripts"
$lenovoClasses = Get-WmiObject -Namespace "ROOT\WMI" -LIST | Where-Object {$_.name -match "LENOVO"}
function onLenovoWmiEvent($className, [ScriptBlock]$handler)
{
$wmiClass = $lenovoClasses | Where-Object {$_.name -eq $className} | Select-Object -First 1
if ($wmiClass)
{
Register-WMIEvent `
-Namespace "ROOT\WMI" `
-query "select * From $className" `
-sourceIdentifier "LegionWatcher_$ClassName" `
-action $handler
}
}
function RunPs1($PsFullPath, $arg1, $arg2)
{
Write-Host Starting External Powershell Script $PsFullPath
Write-Host "-----------------------"
& $psFullPath $arg1 $arg2
Write-Host "-----------------------"
}
function RunBat($batFullPath, $arg1, $arg2)
{
$batFileName = Split-Path -Path $batFullPath -Leaf
$workingDir = Split-Path -Path $batFullPath -Parent
$command = "cmd.exe"
$args = "/c $batFileName $arg1 $arg2"
Write-Host Starting Process $command $args
Write-Host "-----------------------"
$p = Start-Process $command -ArgumentList $args -Wait -NoNewWindow -Passthru -WorkingDirectory $UserScriptsDir
Write-Host "-----------------------"
Write-Host Process exited with Exit Code $p.ExitCode
}
function HandleEvent($type, $info)
{
$dateTime = get-date -Format "dd.MM.yyyy HH:mm:ss"
Write-Host "`n"
Write-Host "-----------------------"
Write-Host $dateTime
Write-Host "NEW EVENT"
Write-Host "-----------------------"
Write-Host "Type: $type"
Write-Host "Info: $info"
Write-Host "-----------------------"
# Find all .bat, .ps1 scripts which are prefixed with the event type
# Eg: utility_keypress.bat, utility_keypress__log_activity.bat
$eventHandlersFiles = Get-ChildItem -Path "$userScriptsDir\*" -File -Include "$type*.bat","$type*.ps1" | Sort-Object -Property name
foreach($file in $eventHandlersFiles)
{
switch($file.Extension)
{
".bat" {RunBat $file.FullName $info $type}
".ps1" {RunPs1 $file.FullName $info $type}
}
}
# Find all .bat, .ps1 scripts which are prefixed with "all_"
# Eg: all.bat, all__write_event.ps1
$eventHandlersFiles = Get-ChildItem -Path "$userScriptsDir\*" -File -Include "all*.bat","all*.ps1" | Sort-Object -Property name
foreach($file in $eventHandlersFiles)
{
switch($file.Extension)
{
".bat" {RunBat $file.FullName $info $type}
".ps1" {RunPs1 $file.FullName $info $type}
}
}
}
###############################################################################
# SMART FAN MODE EVENT
# -----------------------------------------------------------------------------
# Fired when user presses FN+Q
###############################################################################
onLenovoWmiEvent "LENOVO_GAMEZONE_SMART_FAN_MODE_EVENT" {HandleEvent "smart_fan_mode_change" $EventArgs.NewEvent.mode}
###############################################################################
# POWER CHARGE MODE EVENT
# -----------------------------------------------------------------------------
# Fired when AC adaptor is connected / disconnected
# Two different event names based on the version of Vantage installed
###############################################################################
onLenovoWmiEvent "LENOVO_GAMEZONE_POWER_CHARGE_MODE_EVENT_EVENT" {HandleEvent "charge_mode_change" $EventArgs.NewEvent.mode}
onLenovoWmiEvent "LENOVO_GAMEZONE_POWER_CHARGE_MODE_EVENT" {HandleEvent "charge_mode_change" $EventArgs.NewEvent.mode}
###############################################################################
# POWER CHANGE EVENT
# -----------------------------------------------------------------------------
# Fired when any Windows Power Plan is set
###############################################################################
Register-WMIEvent `
-query "select * From __InstanceCreationEvent Where TargetInstance ISA 'Win32_NTLogEvent'
AND TargetInstance.LogFile='System'
AND TargetInstance.SourceName='Microsoft-Windows-UserModePowerService'
AND TargetInstance.EventCode=12" `
-sourceIdentifier "LegionWatcher_Win32_NTLogEvent" `
-action {
$type = "power_plan_change"
$info = (Get-WmiObject -Class win32_powerplan -Namespace root\cimv2\power -Filter "isActive='true'").InstanceID.toString().SubString(21,36)
HandleEvent $type $info
}
###############################################################################
# LIGHT PROFILE CHANGE EVENT
# -----------------------------------------------------------------------------
# Fired when Light Profile is changed? (Not tested / Unable to trigger)
###############################################################################
onLenovoWmiEvent "LENOVO_GAMEZONE_LIGHT_PROFILE_CHANGE_EVENT" {HandleEvent "light_profile_change" ""}
###############################################################################
# FAN COOLING EVENT
# -----------------------------------------------------------------------------
# Fired when cooling has completed ? (Not Tested / Unable to trigger)
###############################################################################
onLenovoWmiEvent "LENOVO_GAMEZONE_FAN_COOLING_EVENT" {HandleEvent "fan_cooling_change" ""}
###############################################################################
# Thermal Mode Event
# -----------------------------------------------------------------------------
# Fired when Power Plan is changed to a Lenovo Power Plan
###############################################################################
onLenovoWmiEvent "LENOVO_GAMEZONE_THERMAL_MODE_EVENT" {HandleEvent "thermal_mode_change" $EventArgs.NewEvent.mode}
###############################################################################
# Utility Event
# -----------------------------------------------------------------------------
# Fired on press of a Lenovo Utility Key
# -----------------------------------------------------------------------------
# 1 - FN+F12 (Vantage Launcher)
# 2 - FN+Esc (Function Lock Enabled)
# 3 - FN+Esc (Function Lock Disabled)
# 4 - FN+PrtSc (Snip Sketch Launcher)
# 16 - FN+R (Refresh Rate toggle)
###############################################################################
onLenovoWmiEvent "LENOVO_UTILITY_EVENT" {HandleEvent "utility_keypress" $EventArgs.NewEvent.PressTypeDataVal}