-
Notifications
You must be signed in to change notification settings - Fork 1
/
Detect_WOL.ps1
218 lines (186 loc) · 9.47 KB
/
Detect_WOL.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
function Get-WOLSettings {
<#
.SYNOPSIS
Function to retrieve the Wake-on-Lan (WOL) settings of a Windows device from the UEFI configuration.
.DESCRIPTION
Function checks the manufacturer of the device and attempts to get the WOL state. Supports Dell, HP, and Lenovo devices.
Updates PS Gallery, PsGet modules,
It attempts to install the required module for the manufacturer if it's not already installed.
If the manufacturer is Dell, it uses DellBIOSProvider to get the WOL state.
If the manufacturer is HP, it uses HPCMSL to get the WOL state.
If the manufacturer is Lenovo, it uses WMI to get the WOL state.
The function also checks the WOL state at OS level for each network interface card (NIC).
Returns the WOL state at BIOS and OS level.
The work is based on:
Kelvin Tegelaar's posts on Reddit and Cyberdrain:
https://www.reddit.com/r/msp/comments/fp7dhq/monitoring_with_powershell_monitoring_and
https://www.cyberdrain.com/monitoring-with-powershell-monitor-and-enabling-wol-for-hp-lenovo-dell/
Additional references:
https://learn.microsoft.com/en-us/archive/blogs/jimriekse/changing-hp-biosuefi-settings-with-biosconfigutility64-exe
http://ftp.hp.com/pub/caps-softpaq/cmit/HP_BCU.html
https://powershell.one/code/11.html
https://www.recastsoftware.com/resources/configmgr-docs/configmgr-topics/manufacturer-tools/hp-management-via-powershell/
.EXAMPLE
Get-WOLSettings
# This will check the Wake-on-Lan settings for the local computer based on its manufacturer.
#>
[CmdletBinding()]
param ()
$result = 0
$detectSummary = ""
# Check and install necessary dependencies
$PPNuGet = Get-PackageProvider -ListAvailable | Where-Object { $_.Name -eq "Nuget" }
if (!$PPNuget) {
Write-Host "Installing Nuget provider" -foregroundcolor Green
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
$detectSummary += "Installed Nuget. "
}
$PSGallery = Get-PSRepository -Name PsGallery
if (!$PSGallery) {
Write-Host "Installing PSGallery" -foregroundcolor Green
Set-PSRepository -InstallationPolicy Trusted -Name PSGallery
$detectSummary += "Installed PsGallery. "
}
$PsGetVersion = (get-module PowerShellGet).version
if ($PsGetVersion -lt [version]'2.0') {
try {
# Attempt to install the latest version of PowerShellGet
Write-Host "Installing latest version of PowerShellGet provider" -foregroundcolor Green
Install-Module -Name PowerShellGet -MinimumVersion 2.2 -Force -AllowClobber -ErrorAction Stop
# Attempt to reload the modules
Write-Host "Reloading Modules" -foregroundcolor Green
Remove-Module -Name PowerShellGet -Force -ErrorAction Stop # Removes the currently loaded PowerShellGet module
Remove-Module -Name PackageManagement -Force -ErrorAction Stop # Removes the currently loaded PackageManagement module
Import-Module -Global -Name PowerShellGet -MinimumVersion 2.2 -Force -ErrorAction Stop # Imports the newly installed PowerShellGet module
# Attempt to update PowerShellGet
Write-Host "Updating PowerShellGet" -foregroundcolor Green
Install-Module -Name PowerShellGet -MinimumVersion 2.2.3 -Force -AllowClobber -ErrorAction Stop
# Inform the user that they need to rerun the script because PowerShellGet was out of date
Write-Host "You must rerun the script to succesfully get the WOL status. PowerShellGet was found out of date." -ForegroundColor red
$detectSummary += "Updated PsGet. " # Add a note to the detection summary
$result = 1 # Set the result to 1, indicating error
}
catch {
# An error occurred while attempting to update PowerShellGet
Write-Host "An error occurred while updating PowerShellGet: $_" -ForegroundColor Red # Print the error message to the console
$detectSummary += "Error updating PsGet. " # Add a note to the detection summary
$result = 1 # Set the result to 1, indicating an error occurred
}
}
# Check the manufacturer and get the WOL status
if ($result -eq 0) {
Write-Host "Checking Manufacturer" -foregroundcolor Green
$Manufacturer = (Get-CimInstance -ClassName Win32_ComputerSystem).Manufacturer
if ($Manufacturer -like "*Dell*") {
$detectSummary += "Dell system. "
Write-Host "Manufacturer is Dell. Installing Module and trying to get WOL state" -foregroundcolor Green
Write-Host "Installing Dell Bios Provider if needed" -foregroundcolor Green
$Mod = Get-Module DellBIOSProvider
if (!$mod) {
Install-Module -Name DellBIOSProvider -Force
$detectSummary += "Installed Dell BIOS Provider. "
}
Import-Module -Global DellBIOSProvider
try {
$WOLMonitor = get-item -Path "DellSmBios:\PowerManagement\WakeOnLan" -ErrorAction SilentlyContinue
$detectSummary += "Dell WoL value: $($WOLMonitor.currentvalue). "
if ($WOLMonitor.currentvalue -eq "LanOnly") {
$WOLState = "Healthy"
}
else {
$detectSummary += "Dell WoL incorrectly configured. " #WOL needs to be enabled in BIOS and OS.
$result = 1
}
}
catch {
write-host "an error occured. Could not get WOL setting."
$detectSummary += "Error getting Dell WoL setting. "
$result = -1
}
}
elseif ($Manufacturer -like "*HP*" -or $Manufacturer -like "*Hewlett*") {
$detectSummary += "HP system. "
Write-Host "Manufacturer is HP. Installing module and trying to get WOL State." -foregroundcolor Green
Write-Host "Installing HP Provider if needed." -foregroundcolor Green
$Mod = Get-Module HPCMSL
if (!$mod) {
Install-Module -Name HPCMSL -Force -AcceptLicense
$detectSummary += "Installed HP BIOS provider. "
}
Import-Module -Global HPCMSL
try {
$WolTypes = get-hpbiossettingslist | Where-Object { $_.Name -like "*Wake On Lan*" }
$WOLState = ForEach ($WolType in $WolTypes) {
write-host "Setting WOL Type: $($WOLType.Name)"
get-HPBIOSSettingValue -name $($WolType.name) -ErrorAction Stop
}
$detectSummary += "HP WoL value: $($WOLState). "
}
catch {
write-host "an error occured. Could not find WOL state"
$detectSummary += "Error getting HP WoL setting. "
$result = ¨-1
}
}
elseif ($Manufacturer -like "*Lenovo*") {
$detectSummary += "Lenovo system. "
Write-Host "Manufacturer is Lenovo. Trying to get via WMI" -foregroundcolor Green
try {
Write-Host "Getting BIOS." -foregroundcolor Green
$currentSetting = (Get-WmiObject -ErrorAction Stop -class "Lenovo_BiosSetting" -namespace "root\wmi") | Where-Object { $_.CurrentSetting -ne "" }
$WOLStatus = $currentSetting.currentsetting | ConvertFrom-Csv -Delimiter "," -Header "Setting", "Status" | Where-Object { $_.setting -eq "Wake on lan" }
$WOLStatus = $WOLStatus.status -split ";"
if ($WOLStatus[0] -eq "Primary") { $WOLState = "Healthy" }
$detectSummary += "Lenovo WoL value: $($WOLState). "
}
catch {
write-host "an error occured. Could not find WOL state"
$detectSummary += "Error getting Lenovo WoL setting. "
$result = -1
}
}
else {
$detectSummary += "$($Manufacturer) not supported by script. "
$result = -2
}
# Check if WOL is enabled at the OS level
$NicsWithWake = Get-CimInstance -ClassName "MSPower_DeviceWakeEnable" -Namespace "root/wmi" | Where-Object { $_.Enable -eq $False }
if (!$NicsWithWake) {
$NICWOL = "Healthy - All NICs enabled for WOL within the OS."
$detectSummary += "NICs with WoL enabled in OS. "
}
else {
$NICWOL = "Unhealthy - NIC does not have WOL enabled in OS."
$detectSummary += "NIC(s) not WoL enabled in OS. "
$result = 1
}
$detectSummary += "OS WoL value: $($NICWOL) "
}
# Return the WOL settings
return @{
Result = $result
NICWOL = $NICWOL
WOLState = $WOLState
Summary = $detectSummary
}
}
#To make it easier to read in AgentExecutor Log.
Write-Host `n`n
$WoLSettings = Get-WOLSettings
$WoLResult = $WoLSettings.Result
$WoLSummary = $WoLSettings.Summary
Write-Host "BIOS WoL status: $($WoLSettings.WOLState)"
Write-Host "OS WoL status: $($WoLSettings.NICWOL)"
#Return result
if ($WoLResult -eq 0) {
Write-Host "OK $([datetime]::Now) : $($WoLSummary)"
Exit 0
}
elseif ($WoLResult -eq 1) {
Write-Host "WARNING $([datetime]::Now) : $($WoLSummary)"
Exit 1
}
else {
Write-Host "NOTE $([datetime]::Now) : $($WoLSummary)"
Exit 0
}