Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with network adapter power settings.
Microsoft Scripting Guy, Ed Wilson, is here. Today I want to continue my network adapter series, but first...
PowerShell Saturday #007 will be held in Charlotte, North Carolina on February 8, 2014. This will be an awesome chance to meet and to learn from some of the best PowerShellers around. In fact, five of the speakers are also speakers at the PowerShell Summit this year. There are a limited number of tickets still available for this event, so you’ll want to sign up now. The Scripting Wife wrote a great post that provides a quick overview of the event: Psst...Charlotte PowerShell Saturday Details Leaked.
Note This is the fifth post in a series that examines working with network adapters. You may want to refer to the earlier posts to get a feel for the way the series progresses:
In Windows 7, the network adapter power settings expanded. Some of the improvements are listed here:
For more information, see Power Management for Network Devices in Windows 7.
These settings are configurable via the graphical user interface by clicking the Configure button from the Network Adapter Properties dialog box. The settings are shown in the image that follows:
Some of the network adapter power management settings are configurable via Netsh. For example, to permit ARP packets and NS packets to wake the network adapter, I would use a command such as:
netsh interface ipv4 set interface 12 forcearpndwolpattern=enabled
When the command completes successfully, it returns OK. Keep in mind that this will also cause a network adapter reset. The command and its associated output are shown here:
To query the power management settings for a specific network adapter, use the Get-NetAdapterPowerManagement function and specify the name of the network adapter. An example of this command is shown here:
Get-NetAdapterPowerManagement -Name ethernet
The command and the output associated with the command are shown in the image that follows:
The Get-NetAdapterPowerManagement function only permits the use of the adapter name or interface description as parameters. But the Get-NetAdapter function is much more flexible. I often use Get-NetAdapter to retrieve a specific network adapter, and then pipe it to other functions such as Get-NetAdapterPowerManagement. This technique is shown here:
Get-NetAdapter -InterfaceIndex 4 | Get-NetAdapterPowerManagement
To configure network adapter power management, I use the Set-NetAdapterPowerManagement function. Once again, I want to retrieve my network adapter by interface index number instead of having to type the name or description of the adapter. I pipe the resulting network adapter object to the Set-NetAdapterPowerManagement function and specify a value for the –WakeOnMagicPacket parameter. The command is shown here:
Get-NetAdapter -InterfaceIndex 4 | Set-NetAdapterPowerManagement -WakeOnMagicPacket Enabled
Because no output returns from the command, I use the Get-NetAdapter command a second time to verify that the configuration change took place. The commands and their associated output are shown in the following image:
It so happens that I know what the permissible values are for the parameters. But if I did not know this, I could create the command in the Windows PowerShell ISE and rely on the Intellisense features. When I type a parameter name, the permissible values appear and make it possible to select the correct value from the list, as shown here:
Most of the time, when I need to manage network adapter power management settings, it is because of a new deployment, or because an audit has determined that I have configuration drift. (Hmmm…this would actually be a great thing to use desired configuration management to control.) So, I put all the settings I want to configure into a single command, such as this:
Set-NetAdapterPowerManagement -Name ethernet -ArpOffload Enabled -DeviceSleep
OnDisconnect Disabled -NSOffload Enabled -WakeOnMagicPacket Enabled -WakeOnPattern Enabled -PassThru
The –passthru parameter outputs a configuration management object so I can inspect it to ensure that the proper changes occurred. The command and the output from the command are shown that follows:
To make changes to multiple computers, I first use the New-CimSession cmdlet to make my remote connections. I can specify the computer names and the credentials to use to make the connection. I then store the remote connection in a variable.
Next, I pass that CIM session to the –CimSession parameter. The key to remember is that I must be able to identify the network adapter that I need to use for the management activity. An example of creating a CIM session and using it follows. (This is a two-line command, so if you directly copy it, you must change the computer name and network interface name, and remove spaces until the second command appears on a single line.)
$session = New-CimSession -ComputerName edlt
Set-NetAdapterPowerManagement -CimSession $session -name ethernet -ArpOffload
Enabled -DeviceSleepOnDisconnect Disabled -NSOffload Enabled -WakeOnMagicPacket
Enabled -WakeOnPattern Enabled -PassThru
The command and the output from the command are shown in the image that follows.
Note These commands require that the Windows PowerShell console or the Windows PowerShell ISE is opened with Admin rights. To do this, right-click the Windows PowerShell console icon or the Windows PowerShell ISE icon while holding down the Shift key, and select Run as administrator. Or if you launch it via Windows Search in Windows 8.1, type Windows PowerShell from the Start page, and the Search dialog box appears with the Windows PowerShell icon. Right-click the icon, and select Run as administrator from the Action menu.
If you do not launch Windows PowerShell with Admin rights, an error message appears that states it cannot find the network adapter. An example of the error message is shown here:
Network Adapter Week will wrap-up tomorrow. I will be talking about how to gather network statistics.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Did you test this script? Out of the box, it does not work. Running at an elevated powershell prompt produces:
The term 'Get-NetAdapterPowerManagement' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:30
+ Get-NetAdapterPowerManagement <<<< -Name ethernet
+ CategoryInfo : ObjectNotFound: (Get-NetAdapterPowerManagement:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
@Bill - it is not a script. It is a CmdLet but it is not available in WIndows 95,98,Me,Xp or any other obsolete system.
Upgrade to get satisfaction.
Whenin doubt read the manual:
@Ed - you forgot!
@ Bill Westrup
What OS are you running the cmdlets on? From
Applies To: Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2
Thanks for sharing. Will find time to test.