Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Script integrated NIC power managment regedit

Posted on 2014-11-17
9
Medium Priority
?
882 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 12

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 12

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
When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

 

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
 
LVL 12

Accepted Solution

by:
zalazar earned 2000 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 12

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

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

Question has a verified solution.

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

Quality of Service (QoS) options are nearly endless when it comes to networks today. This article is merely one example of how it can be handled in a hub-n-spoke design using a 3-tier configuration.
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

577 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