salt-states/enable-wakeonlan.ps1

38 lines
1.5 KiB
PowerShell
Raw Permalink Normal View History

# based on https://community.spiceworks.com/topic/2346039-help-with-windows-10-wake-on-lan-magic-packet-script
$ErrorActionPreference = "Stop"
$changes = @{}
foreach ($nic in Get-NetAdapter -Physical) {
$nicPowerWake = Get-WmiObject MSPower_DeviceWakeEnable -Namespace root\wmi | Where-Object {$_.InstanceName -match [regex]::escape($nic.PNPDeviceID) }
If ($nicPowerWake.Enable -ne $True)
{
$changes["$($nic.Name) MSPower_DeviceWakeEnable"] = $True
$nicPowerWake.Enable = $True
$nicPowerWake.psbase.Put() | Out-Null
}
$nicMagicPacket = Get-WmiObject MSNdis_DeviceWakeOnMagicPacketOnly -Namespace root\wmi | Where-Object {$_.InstanceName -match [regex]::escape($nic.PNPDeviceID) }
If ($nicMagicPacket.EnableWakeOnMagicPacketOnly -ne $True)
{
$changes["$($nic.Name) EnableWakeOnMagicPacketOnly"] = $True
$nicMagicPacket.EnableWakeOnMagicPacketOnly = $True
$nicMagicPacket.psbase.Put() | Out-Null
}
}
# TODO: None of the tested computers actually had this property
foreach ($eee in Get-NetAdapterAdvancedProperty | Where-Object DisplayName -eq "Energy-Efficient Ethernet") {
If ($eee.DisplayValue -eq "Enabled") {
$changes["Energy-Efficient Ethernet"] = $True
$eee | Set-NetAdapterAdvancedProperty -DisplayValue "Disabled"
}
}
If ($changes.count -gt 0) {
$changes["comment"] = "Enabled $($changes.keys -join ', ')"
$changes["changed"] = $True
}
write-host "$($changes | ConvertTo-Json -Compress)"