Solved

Script integrated NIC power managment regedit

Posted on 2014-11-17
9
523 Views
Last Modified: 2014-11-26
I want to push a regedit to disable the power management feature on the integrated Ethernet network adapter of numerous Windows 7 computers. I found the applicable KB, http://support.microsoft.com/kb/2740020, the problem however is that some of these computers will have multiple physical and virtual network adapters. Device 0000 or 0001 is not always the physical integrated NIC. On my computer for example, it is device 0007. Some computers are laptops and have both Ethernet and WiFi integrated NICs.

That being said, I need a script that can determine which device number is the physical Ethernet integrated NIC and then apply the regedit to the appropriate device in the registry. I've looked at various keys to see how one might isolate the exact device in the registry. It looks to me like "MediaType" and "PhysicalMediaType" combined would point to the right one. I've also noticed that for some device instances, either one or both of those keys are not even present. That would have to be taken into consideration when developing the script. Any help would be appreciated. Before anyone asks, sadly, using a GPO is not an option.
0
Comment
Question by:jcb431
  • 5
  • 4
9 Comments
 
LVL 11

Expert Comment

by:zalazar
ID: 40453889
A value of 14 for "*PhysicalMediaType" stands for Ethernet, 9 = Wireless.
In addition you might also check for the "*SpeedDuplex" value as network speed/duplex is only applicable for physical NICs.
The following script will display the physical NIC's registry device number.
Const HKEY_LOCAL_MACHINE = &H80000002
Dim WshShell, objReg

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

call GetPysicalNIC()

Wscript.Quit 0

'------------------------------------------------------------------------
' --------  SUBS  --------
'------------------------------------------------------------------------
Sub GetPysicalNIC()
  On Error Resume Next
  Dim strKeyName, aSubkeys, sSubkey, strKeyPath
  Dim sSpeedDuplexSetting, vPhysicalMediaType

  Wscript.echo "Reading NIC information"
  strKeyName = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"
  objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyName, aSubkeys
  If IsArray(aSubkeys) = False Then Exit Sub

  For Each sSubkey In aSubkeys
    sSpeedDuplexSetting = ""
    vPhysicalMediaType = -1

    strKeyPath = strKeyName & "\" & sSubkey
    sComponentId = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\ComponentId")
    If LCase(Left(sComponentId, 4)) = "pci\" Then
      vPhysicalMediaType = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\*PhysicalMediaType")
      sSpeedDuplexSetting = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\*SpeedDuplex")
      If vPhysicalMediaType = 14 And sSpeedDuplexSetting <> "" Then
        Wscript.echo "Physical NIC;" & sSubkey & ";HKEY_LOCAL_MACHINE\" & strKeyPath
      End If
    End If
  Next
End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:zalazar
ID: 40453908
If you prefer "MediaType" and "PhysicalMediaType" then please see below:
Const HKEY_LOCAL_MACHINE = &H80000002
Dim WshShell, objReg

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

call GetPysicalNIC()

Wscript.Quit 0

'------------------------------------------------------------------------
' --------  SUBS  --------
'------------------------------------------------------------------------
Sub GetPysicalNIC()
  On Error Resume Next
  Dim strKeyName, aSubkeys, sSubkey, strKeyPath
  Dim vPhysicalMediaType, vMediaType

  Wscript.echo "Reading NIC information"
  strKeyName = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"
  objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyName, aSubkeys
  If IsArray(aSubkeys) = False Then Exit Sub

  For Each sSubkey In aSubkeys
    vPhysicalMediaType = -1
    vMediaType = -1

    strKeyPath = strKeyName & "\" & sSubkey
    sComponentId = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\ComponentId")
    If LCase(Left(sComponentId, 4)) = "pci\" Then
      vPhysicalMediaType = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\*PhysicalMediaType")
      vMediaType = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\*MediaType")
      If vPhysicalMediaType = 14 And vMediaType = 0 Then
        Wscript.echo "Physical NIC;" & sSubkey & ";HKEY_LOCAL_MACHINE\" & strKeyPath
      End If
    End If
  Next
End Sub

Open in new window

0
 

Author Comment

by:jcb431
ID: 40455397
I must be doing something wrong. "Const" is not recognized as an internal or external command, operable program or batch file...I get the same for each ensuing line. Once the device number is isolated, how do I apply the regedit to the appropriate key? It appears from the KB that the value becomes 0x118 (280) for the PnPCapabilities Key.
0
 

Author Comment

by:jcb431
ID: 40455442
Disregard the error. I was not paying attention. But I still need to know how to pipe the result or otherwise merge the appropriate regedit based on the resulting device number.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 11

Accepted Solution

by:
zalazar earned 500 total points
ID: 40460920
From what I read in the kb article http://support.microsoft.com/kb/2740020  the value should actually be 24 (Decimal).

quote/
A conflict happens for the DWORD value for the last step where Option 1 is only checked, if the following steps are done exactly as mentioned below:
•If you check all the boxes, then the value is 256 (0x100).
•If you un-check the box 1, which will grey out the other two, the value becomes 280 (0x118).
•If you check all the boxes except, the 3rd one, PNPCapabilites value becomes 0.
•If step 2 is repeated, the value becomes 24 (0x18).

Now, the values are different for the same setting because the way it has been achieved.
For deployment purpose, to keep the value for Allow the Computer to turn off this device to save power cleared, one needs to use the value 24 (0x18). Reason being, by default the option 1 and 2 are checked, which is same as DWORD value 0 of this key (even though the key does not exist in the registry by default). Hence, creating this key with a value 24 (0x18) in the deployment script/build process will inject this entry in the registry which in turn should uncheck the first box during server startup.

/quote

I have also checked the MicrosoftFixit55016.msi file which is in the KB article.
When unpacking the MSI and opening scriptfile "Binary.DisableAdapterPower".
The following line is present:
  Const strValueName = "PnPCapabilities"
  objReg.SetDWORDValue HKEY_LOCAL_MACHINE,targetKey & "\" & deviceNumber,strValueName,24

Below please find the script I created.
By modifying the variables you can adjust the settings if needed.
valPhysicalMediaType = 14
valMediaType = 0
valPnPCapabilities = 24
booSpeedDuplexCheck = "yes"

Const HKEY_LOCAL_MACHINE = &H80000002
Dim WshShell, objReg
Dim valPhysicalMediaType, valMediaType, valPnPCapabilities, booSpeedDuplexCheck

valPhysicalMediaType = 14    ' 14=Ethernet, 9=Wireless
valMediaType = 0
valPnPCapabilities = 24
booSpeedDuplexCheck = "yes"    ' Set to yes to check if *SpeedDuplex is set, set to no for no *SpeedDuplex check

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

call DisableNICPowerMgt()

Wscript.Quit 0

'------------------------------------------------------------------------
' --------  SUBS  --------
'------------------------------------------------------------------------
Sub DisableNICPowerMgt()
  On Error Resume Next
  Dim strKeyName, aSubkeys, sSubkey, strKeyPath
  Dim vPhysicalMediaType, vMediaType, vPnPCapabilities, sDriverDesc, sSpeedDuplex

  Wscript.echo "Reading NIC information"
  strKeyName = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"
  objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyName, aSubkeys
  If IsArray(aSubkeys) = False Then Exit Sub

  For Each sSubkey In aSubkeys
    vPhysicalMediaType = -1
    vMediaType = -1
    vPnPCapabilities = -1
    sDriverDesc = ""
    sSpeedDuplex = ""

    strKeyPath = strKeyName & "\" & sSubkey
    sComponentId = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\ComponentId")
    If LCase(Left(sComponentId, 4)) = "pci\" Then
      vPhysicalMediaType = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\*PhysicalMediaType")
      vMediaType = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\*MediaType")
      vPnPCapabilities = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\PnPCapabilities")
      sDriverDesc = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\DriverDesc")
      If booSpeedDuplexCheck = "yes" Then
        sSpeedDuplex = WshShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\*SpeedDuplex")
      Else
        sSpeedDuplex = "0"
      End If
      Err.Clear
      ' *PhysicalMediaType=14 (Ethernet), *MediaType=0, PnPCapabilities value is present, PnPCapabilities is not already 24, *SpeedDuplex should be present (if booSpeedDuplexCheck is set to yes)
      If vPhysicalMediaType = valPhysicalMediaType And vMediaType = valMediaType And vPnPCapabilities <> -1 And vPnPCapabilities <> 24 And sSpeedDuplex <> "" Then
        Wscript.echo "Configuring power management for physical NIC|" & sDriverDesc & "|" & sSubkey & "|HKEY_LOCAL_MACHINE\" & strKeyPath
        WshShell.RegWrite "HKEY_LOCAL_MACHINE\" & strKeyPath & "\PnPCapabilities", valPnPCapabilities, "REG_DWORD"
        If Err.Number = 0 Then
          Wscript.echo Now & "|SUCCESS|Configured power management|PnPCapabilities|" & valPnPCapabilities & "|" & CStr(Err.Number)
        Else
          Wscript.echo Now & "|ERROR|Configuring power management|PnPCapabilities|" & CStr(Err.Number)
        End If
      End If
    End If
    Err.Clear
  Next
End Sub

Open in new window

0
 

Author Comment

by:jcb431
ID: 40463445
I see the value change as you described using the script you've provided. Let me verify what happens in my test environment. I'll reply again tomorrow.
0
 

Author Comment

by:jcb431
ID: 40467263
When I run the script, the desired result is achieved, but not without a reboot of the target computer. That's perfectly fine and I really appreciate what you have done. Just out of curiosity though, do you think that the necessary reboot has something to do with the value that was selected or do you think that it is a necessity born of applying this particular registry edit?
0
 

Author Closing Comment

by:jcb431
ID: 40467928
Thank you for you zalazar!
0
 
LVL 11

Expert Comment

by:zalazar
ID: 40467960
Good to know that it's working fine and thanks for the appreciation.
About your question. I assume that when it's changed via the GUI some additional procedures will be executed to "inform" the OS about the change.
When the value is only written into the registry the OS will not detect the change in setting and will only load the new setting at boot.
Thank you very much for the points and grade !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now