How to change computer NIC Speed/Duplex property to 'Auto'?

In another blog entry

How to find computer NIC's with Speed/Duplex not set to 'Auto'?

I described how to identify computers where the speed duplex is not set to auto. This article explains how to automate correcting those computers where the NIC Speed/Duplex property was not set correctly.

Brief recap, we are able to identify those computers that need to be corrected by inventorying the file type *.flg and creating a collection based on those that are not set to auto detect. Then distribute a advertisement that will run the script to set the NIC property back to Auto. Now, since the script will cause the NIC to temporarily reset, be sure to use the download and execute mode for your advertisement.

This script was written for and tested on Intel and Dell laptop NIC's. It has the added advantage of logging change activity to the local desktop, and recreating the new *.flg file so the corrected computers will fall out of the targeted collection.

As always, test in a lab environment before deploying.

' NIC-Fix-v2.vbs

' Steve Thompson
'
' Detect and revise those network adapters that are not set to auto detect
' v1

Option Explicit
'On Error Resume Next

' Begin log stuff
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim strIN, strTextIn
Dim objFSO, objDictionary, objTextFile, objItem
dim strNextLine
dim i
dim FSOOut
dim Overwrite
dim Unicode
dim FileName, FileOut

Dim strFileIn
Dim strFileOut
Dim f
Dim strMsg
' End log stuff

' Flag file
Dim strFlagFile
Dim fFlag
Dim strSMSRoot

Dim strComputer
dim objReg, strKeyPath, subkey, arrSubKeys, dwValue
dim strValue
dim strDriverValue, strModeValue2, strModeValue3
Dim strSpeedDuplex
Dim strRequestedMediaType
Dim strDriverDesc
Dim strSubkey

Dim strModNewValue, strModSubkey

Const HKEY_LOCAL_MACHINE = &H80000002

' log file
strSMSRoot = "C:\Program Files\sms"
strFileOut = "SMSEng-NIC.log"

' Verify/Create Folders
' Flag folder
Call VerifyCreateFolder(strSMSRoot & "\flg")
Call VerifyCreateFolder(strSMSRoot & "\flg\SMSEng-NIC")

' Delete Any Flag files...
Call DeleteFlagFiles(strSMSRoot & "\flg\SMSEng-NIC\*.flg")

' Log folder
Call VerifyCreateFolder(strSMSRoot & "\log")
Call VerifyCreateFolder(strSMSRoot & "\log\SMSEng-NIC")

' file out
Set FSOOut = WScript.CreateObject("Scripting.FileSystemObject")
Overwrite = False
Unicode = False

' start log file
' open client log file
Call OpenClientLog

strMsg = "Started Process..."
Call WriteClientLog (strMsg)

' flag file to be constructed as SMSEng-NIC-<SpeedDuplex>-<DriverDescription (first 5 char)>

' test
'strFlagFile = "SMSEng-NIC-0-INTEL.flg"
'Call CreateFlagFile (strFlagFile)

strComputer = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "System\Currentcontrolset\Control\Class\{4D36E972-E325-11CE-BFC1-08002be10318}"
strDriverValue = "DriverDesc"
strModeValue2 = "SpeedDuplex"
strModeValue3 = "RequestedMediaType"

objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys

    '0 - Auto Detect
    '1 - 10Mbps \ Half Duplex
    '2 - 10Mbps \ Full Duplex
    '3 - 100Mbps \ Half Duplex
    '4 - 100Mbps \ Full Duplex
    ' Capture Driver Description
    objReg.GetStringvalue HKEY_LOCAL_MACHINE, strKeyPath & "\" & subkey, strDriverValue, strValue
    strDriverDesc = strValue
    strSubkey = subkey

    ' Capture SpeedDuplex
    objReg.GetStringvalue HKEY_LOCAL_MACHINE, strKeyPath & "\" & subkey, strModeValue2, dwValue
    strSpeedDuplex = CStr(dwValue & "")

    ' Capture RequestedMediaType (Broadcom - DELL)
    objReg.GetStringvalue HKEY_LOCAL_MACHINE, strKeyPath & "\" & subkey, strModeValue3, dwValue
    strRequestedMediaType = CStr(dwValue & "")

    '    WScript.Echo " " & strModeValue2 & ": " & strSpeedDuplex
    If Len(strSpeedDuplex & "") > 0 then
      If strSpeedDuplex <> "0" Then
        ' modify the subkey for SpeedDuplex
        strModNewValue = "0"
        strModSubkey = strModeValue2
        Call ModifyDuplexSetting (strModSubkey, strModNewValue)

        ' Get SpeedDuplex, one more time for flag file, should be different!
        objReg.GetStringvalue HKEY_LOCAL_MACHINE, strKeyPath & "\" & subkey, strModeValue2, dwValue
        strSpeedDuplex = CStr(dwValue & "")
      End If

        strFlagFile = "SMSEng-NIC-" & strSpeedDuplex & "-" & LEFT(UCASE(strDriverDesc),5) & ".flg"
        Call CreateFlagFile (strFlagFile)

        ' log entries
        strMsg = "*** Create MODIFIED Flag file: '" &  strFlagFile & "' ***"
        Call WriteClientLog (strMsg)

        strMsg = "... subkey: '" & strSubkey & "' Full Driver Description: '" & strDriverDesc & "'"
        ' wscript.echo "strMsg: " & strMsg
        Call WriteClientLog (strMsg)

    ElseIf Len(strRequestedMediaType & "") > 0 Then
      If strRequestedMediaType <> "0" Then
        ' modify the subkey for RequestedMediaType (Broadcom - DELL)
        strModNewValue = "0"
        strModSubkey = strModeValue3
        Call ModifyDuplexSetting (strModSubkey, strModNewValue)

        ' Get RequestedMediaType (Broadcom - DELL), one more time for flag file
        objReg.GetStringvalue HKEY_LOCAL_MACHINE, strKeyPath & "\" & subkey, strModeValue3, dwValue
        strRequestedMediaType = CStr(dwValue & "")
       End If

        strFlagFile = "SMSEng-NIC-" & strRequestedMediaType & "-" & LEFT(UCASE(strDriverDesc),5) & ".flg"
        Call CreateFlagFile (strFlagFile)

        ' log entries
        strMsg = "*** Create MODIFIED Flag file: '" &  strFlagFile & "' ***"
        Call WriteClientLog (strMsg)

        strMsg = "... subkey: '" & strSubkey & "' Full Driver Description: '" & strDriverDesc & "'"
        ' wscript.echo "strMsg: " & strMsg
        Call WriteClientLog (strMsg)

    Else
      strMsg = "... subkey: '" & strSubkey & "' Full Driver Description: '" & strDriverDesc & "'"
      Call WriteClientLog (strMsg)
    ' wscript.echo "strMsg: " & strMsg
    End If

'  End If
Next

strMsg = "End Process..."
Call WriteClientLog (strMsg)

' Create Flag file subs
Sub CreateFlagFile (strFlagFile)
'   strFileOut = strSMSRoot & "\log\SMSEng-NIC.log"

'   On Error Resume Next

   Set fFlag = FSOOut.OpenTextFile(strSMSRoot & "\flg\SMSEng-NIC\" & strFlagFile, ForAppending, True)
   fFlag.Write Now()
   fFlag.Close

End Sub

' Begin log subs
Sub OpenClientLog

   On Error Resume Next

   Set f = FSOOut.OpenTextFile(strSMSRoot & "\log\SMSEng-NIC\" & strFileOut, ForAppending, True)

End Sub

Sub CloseClientLog

   On Error Resume Next
   f.Close

End Sub

Sub WriteClientLog (LineOut)

    On Error Resume Next
    f.Write Now() & "," & LineOut & vbCRLF

End Sub
' End log subs
Sub VerifyCreateFolder(fldr)
   Dim lfso, lmsg, lf
   Set lfso = CreateObject("Scripting.FileSystemObject")

   On Error Resume Next
   If (lfso.FolderExists(fldr)) Then
'      msg = fldr & " exists."
   Else
'      msg = fldr & " doesn't exist."
      Set lf = lfso.CreateFolder(fldr)
   End If
'   ReportFolderStatus = msg
End Sub

Sub DeleteFlagFiles(FlagFiles)

   Dim lfso, lmsg, lf
   Set lfso = CreateObject("Scripting.FileSystemObject")
   On Error Resume Next
   lfso.DeleteFile(FlagFiles)

End Sub

Sub ModifyDuplexSetting (strModSubkey, strModNewValue)
    On Error Resume Next

    objReg.SetStringvalue HKEY_LOCAL_MACHINE, strKeyPath & "\" & subkey, strModSubkey, strModNewValue
End Sub

Published Monday, August 18, 2008 6:13 PM by sthompson

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems