20 April 2016

NIC Power Management Properties

I wrote and published a script that enabled wake on lan two years ago. Since then, I have needed to write new scripts to modify the advanced tab of the NIC properties. I began by looking at my old script to see about adding the new settings to it and I decided to update the old one and republish it here. Thanks to Sapien Technology's PowerShell Studio, I have updated the script with much documentation.

This script will check or uncheck the three boxes in the screenshot below:


To use the script, I have set four parameters which I have set defaults to, which you can easily change:

  • ConsoleTitle - Title displayed in the PowerShell Console
  • TurnOffDeviceAllow the computer to turn off this device to save power. Select true to check this or false to uncheck it
  • WakeComputer - Allow this device to wake the computer. Select true to check this or false to uncheck it
  • AllowMagicPacketsOnly - Only allow a magic packet to wake the computer. Select true to check this or false to uncheck it
You can download the script from here.

Here is the script:


1:  <#  
2:       .SYNOPSIS  
3:            NIC Power Management  
4:         
5:       .DESCRIPTION  
6:            Configure NIC Power Management settings  
7:         
8:       .PARAMETER ConsoleTitle  
9:            Title displayed in the PowerShell Console  
10:         
11:       .PARAMETER TurnOffDevice  
12:            Allow the computer to turn off this device to save power. Select true to check this or false to uncheck it.  
13:         
14:       .PARAMETER WakeComputer  
15:            Allow this device to wake the computer. Select true to check this or false to uncheck it.  
16:         
17:       .PARAMETER AllowMagicPacketsOnly  
18:            Only allow a magic packet to wake the computer. Select true to check this or false to uncheck it.  
19:         
20:       .EXAMPLE  
21:            powershell.exe -executionpolicy bypass -file NICPowerManagement.ps1 -ConsoleTitle "NIC Power Management" -TurnOffDevice $true -WakeComputer $true -AllowMagicPacketsOnly $true  
22:         
23:       .NOTES  
24:            ===========================================================================  
25:            Created with:      SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.119  
26:            Created on:       4/20/2016 11:20 AM  
27:            Created by:       Mick Pletcher  
28:            Organization:  
29:            Filename:        NICPowerManagement.ps1  
30:            ===========================================================================  
31:  #>  
32:  [CmdletBinding()]  
33:  param  
34:  (  
35:            [Parameter(Mandatory = $false)][string]$ConsoleTitle = 'NIC Power Management',  
36:            [bool]$TurnOffDevice = $true,  
37:            [bool]$WakeComputer = $true,  
38:            [bool]$AllowMagicPacketsOnly = $true  
39:  )  
40:    
41:  function Exit-PowerShell {  
42:  <#  
43:       .SYNOPSIS  
44:            Exit PowerShell  
45:         
46:       .DESCRIPTION  
47:            Exit out of the PowerShell script and return an error code 1 if errors were encountered  
48:         
49:       .PARAMETER Errors  
50:            True or false if errors were encountered  
51:         
52:       .NOTES  
53:            Additional information about the function.  
54:  #>  
55:         
56:       [CmdletBinding()]  
57:       param  
58:       (  
59:                 [bool]$Errors  
60:       )  
61:         
62:       If ($Errors -eq $true) {  
63:            Exit 1  
64:       }  
65:  }  
66:    
67:  function Get-RelativePath {  
68:  <#  
69:       .SYNOPSIS  
70:            Get the relative path  
71:         
72:       .DESCRIPTION  
73:            Returns the location of the currently running PowerShell script  
74:         
75:       .NOTES  
76:            Additional information about the function.  
77:  #>  
78:         
79:       [CmdletBinding()][OutputType([string])]  
80:       param ()  
81:         
82:       $Path = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent) + "\"  
83:       Return $Path  
84:  }  
85:    
86:  function Get-PhysicalNICs {  
87:  <#  
88:       .SYNOPSIS  
89:            Retrieve the Physical NICs  
90:         
91:       .DESCRIPTION  
92:            Find the physical NICs that are currently being used and return the information from the function  
93:         
94:       .EXAMPLE  
95:            PS C:\> Get-PhysicalNICs  
96:         
97:       .NOTES  
98:            Additional information about the function.  
99:  #>  
100:         
101:       [CmdletBinding()]  
102:       param ()  
103:         
104:       # Get all physical ethernet adaptors  
105:       $NICs = Get-WmiObject Win32_NetworkAdapter -filter "AdapterTypeID = '0' `  
106:       AND PhysicalAdapter = 'true' `  
107:       AND NOT Description LIKE '%Centrino%' `  
108:       AND NOT Description LIKE '%wireless%' `  
109:       AND NOT Description LIKE '%virtual%' `  
110:       AND NOT Description LIKE '%WiFi%' `  
111:       AND NOT Description LIKE '%Bluetooth%'"  
112:       Return $NICs  
113:  }  
114:    
115:  function Set-ConsoleTitle {  
116:  <#  
117:       .SYNOPSIS  
118:            Console Title  
119:         
120:       .DESCRIPTION  
121:            Sets the title of the PowerShell Console  
122:         
123:       .NOTES  
124:            Additional information about the function.  
125:  #>  
126:         
127:       [CmdletBinding()]  
128:       param ()  
129:         
130:       $host.ui.RawUI.WindowTitle = $ConsoleTitle  
131:  }  
132:    
133:  function Set-NICPowerManagement {  
134:  <#  
135:       .SYNOPSIS  
136:            Enable NIC Power Management  
137:         
138:       .DESCRIPTION  
139:            A detailed description of the Set-NICPowerManagement function.  
140:         
141:       .PARAMETER NICs  
142:            Physical NICs  
143:         
144:       .NOTES  
145:            Additional information about the function.  
146:  #>  
147:         
148:       [CmdletBinding()][OutputType([bool])]  
149:       param  
150:       (  
151:                 $NICs  
152:       )  
153:         
154:       foreach ($NIC in $NICs) {  
155:            $Errors = $false  
156:            Write-Host "NIC:"$NIC.Name  
157:            #Allow the computer to turn off this device  
158:            Write-Host "Allow the computer to turn off this device....." -NoNewline  
159:            $NICPowerManage = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | Where-Object { $_.instancename -match [regex]::escape($nic.PNPDeviceID) }  
160:            If ($NICPowerManage.Enable -ne $TurnOffDevice) {  
161:                 $NICPowerManage.Enable = $TurnOffDevice  
162:                 $HideOutput = $NICPowerManage.psbase.Put()  
163:            }  
164:            If ($NICPowerManage.Enable -eq $TurnOffDevice) {  
165:                 Write-Host "Success" -ForegroundColor Yellow  
166:            } else {  
167:                 Write-Host "Failed" -ForegroundColor Red  
168:                 $Errors = $true  
169:            }  
170:            # Allow this device to wake the computer  
171:            Write-Host "Allow this device to wake the computer....." -NoNewline  
172:            $NICPowerManage = Get-WmiObject MSPower_DeviceWakeEnable -Namespace root\wmi | Where-Object { $_.instancename -match [regex]::escape($nic.PNPDeviceID) }  
173:            If ($NICPowerManage.Enable -ne $WakeComputer) {  
174:                 $NICPowerManage.Enable = $WakeComputer  
175:                 $HideOutput = $NICPowerManage.psbase.Put()  
176:            }  
177:            If ($NICPowerManage.Enable -eq $WakeComputer) {  
178:                 Write-Host "Success" -ForegroundColor Yellow  
179:            } else {  
180:                 Write-Host "Failed" -ForegroundColor Red  
181:                 $Errors = $true  
182:            }  
183:            # Only allow a magic packet to wake the computer  
184:            Write-Host "Only allow a magic packet to wake the computer....." -NoNewline  
185:            $NICPowerManage = Get-WmiObject MSNdis_DeviceWakeOnMagicPacketOnly -Namespace root\wmi | Where-Object { $_.instancename -match [regex]::escape($nic.PNPDeviceID) }  
186:            If ($NICPowerManage.EnableWakeOnMagicPacketOnly -ne $AllowMagicPacketsOnly) {  
187:                 $NICPowerManage.EnableWakeOnMagicPacketOnly = $AllowMagicPacketsOnly  
188:                 $HideOutput = $NICPowerManage.psbase.Put()  
189:            }  
190:            If ($NICPowerManage.EnableWakeOnMagicPacketOnly -eq $AllowMagicPacketsOnly) {  
191:                 Write-Host "Success" -ForegroundColor Yellow  
192:            } else {  
193:                 Write-Host "Failed" -ForegroundColor Red  
194:                 $Errors = $true  
195:            }  
196:       }  
197:       Return $Errors  
198:  }  
199:    
200:  Clear-Host  
201:  Set-ConsoleTitle  
202:  $PhysicalNICs = Get-PhysicalNICs  
203:  $Errors = Set-NICPowerManagement -NICs $PhysicalNICs  
204:  Start-Sleep -Seconds 5  
205:  Exit-PowerShell -Errors $Errors  
206:    

4 comments:

  1. Awsome! Does this script do it on wifi adapters aswell?

    ReplyDelete
    Replies
    1. No, but you can go to line 108 and delete it. That filters out wireless

      Delete
  2. If I have a list of computer names this needs to be done for, is it possible to do integrate that? Basically I have been tasked with making sure WOL is enabled for about 1200 PC's. All I have been able to find is doing one at a time and I need a bulk change.

    ReplyDelete
  3. I noticed after running the script that the "Wake on Magic Packet" option in the Advanced tab of the NIC properties remains disabled. Is this option required in order for WOL to work properly?

    ReplyDelete