CODE
On Error Resume Next
'---- ParameterDirectionEnum Values ----
Const adParamInput = &H0001
rem Const adParamOutput = &H0002
'---- CommandTypeEnum Values ----
Const adCmdStoredProc = &H0004
rem 3 tasks
rem 1. Store in Database - In Progress
rem 2. If 100 Full, say 100/ Full - Done
rem 3. If not, say Not Set - Done
rem 4. Fix it - Done
rem 6. Stop & Start the service to make it up-to-date - In Progress
Set StdOut = WScript.StdOut
const HKEY_LOCAL_MACHINE = &H80000002
const IDEAL_SPEED = "100"
const IDEAL_DUPLEX = "FULL"
Class NICInfo
Dim MACAddress
Dim Name
Dim IPAddress
Dim MachineName
Dim Result
Dim Notes
End Class
Dim oNICInfo
Set oNICInfo = new NICInfo
Dim strKeyCurrentPath
strKeyCurrentPath = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}"
Dim strCurrentControlNetwork
strCurrentControlNetwork = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002bE10318}"
Dim strCurrentVersion
strCurrentVersion = "Software\Microsoft\Windows NT\CurrentVersion"
Dim oArrayPropertyType
rem if there are two properties, first property is for 100/10 & second property is for Full/Half
oArrayPropertyType = Array("ConnectionType", "", _
"Duplex", "MediaSpeed", _
"media", "", _
"Media_type", "", _
"MediaSelect", "Duplexmode", _
"MediaSpeed", "", _
"NetworkMedia", "", _
"RequestedMediaType", "", _
"SpeedDuplex", "" )
Dim oReg
Dim oSvc
Dim WshSHell
set WshShell = CreateObject("WScript.Shell")
Dim strComputer
'set strComputer = "."
Set oDictionary = CreateObject("Scripting.Dictionary")
Dim bShowUsage
bShowUsage = True
Dim oArgs
'If RetrieveArguments() = bShowUsage Then
'Call ShowUsage
'Else
Call setnicInfo
'End If
rem Get the NIC Info
Sub setnicInfo
StdOut.Write vbNewLine & _
"----------------------------------------------------------" & vbNewLine & _
"Computer Name" & vbTab & "Result" & vbTab & "Details" & vbNewLine & _
"----------------------------------------------------------" & vbNewLine & _
vbNewLine
'for each oComputer in oDictionary
strComputer = "." ' oDictionary.Item(oComputer)
err.Clear
rem Get WMI object for registry
'Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
On Error Resume Next
Set oReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
If Err.Number <> 0 Then
StdOut.WriteLine "Error: (Unavailable Registry at " & strComputer & ") " & Err.Description
Else If (GetOSVersion() < 5) Then
StdOut.WriteLine "Error: (Computer at " & strComputer & ") Version Less than 5"
Else
rem Get WMI object for registry
On Error Resume Next
Set oSvc = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
If Err.Number <> 0 Then
StdOut.WriteLine "Error: " & Err.Description
Else
Dim oArrayPNPDeviceID
rem for each oInstNWAC in oSvc.execquery("select * from win32_networkadapterConfiguration where IPEnabled = 'True'")
for each oInstNWA in oSvc.execquery("select * from win32_networkadapter where MACAddress <> null")
rem where MACAddress = " & Str(oInstNWAC.MACAddress) & "")
If (oInstNWA.PNPDeviceID <> empty) Then
oArrayPNPDeviceID = Split(oInstNWA.PNPDeviceID, "\", -1, 1)
If (oArrayPNPDeviceID(0) <> "ROOT") Then
For each oInstNWAC in oSvc.execquery("select * from win32_networkadapterConfiguration where IPEnabled = 'True'")
If (oInstNWAC.Caption = oInstNWA.Caption) Then
rem Get the Caption for the Network Adapter
strCaption = GetRealCaptionStr(oInstNWAC.Caption)
rem Continue, only if Connection is Open
rem Dim sPnpInstanceIDValue
rem sPnpInstanceIDValue = empty
rem oReg.GetStringValue HKEY_LOCAL_MACHINE, strCurrentControlNetwork & "\" & oInstNWAC.SettingID & "\Connection", "PnpInstanceID", sPnpInstanceIDValue
Dim sNetworkName
sNetworkName = empty
oReg.GetStringValue HKEY_LOCAL_MACHINE, strCurrentControlNetwork & "\" & oInstNWAC.SettingID & "\Connection", "Name", sNetworkName
rem Continue, if there is no error & PnpInstanceID is non-empty
If (err.number = 0) Then
rem Dim strPropertyDesc
rem strPropertyDesc = ""
rem strPropertyDesc =
rem Print the computer name
StdOut.Write strComputer & vbTab
oNICInfo.MachineName = oInstNWA.SystemName
rem Get the NIC Name
oNICInfo.Name = oInstNWA.Caption
stop
rem Print the MAC Address for the Network Adapter
rem StdOut.Write oInstNWA.MACAddress & vbTab
oNICInfo.MACAddress = oInstNWA.MACAddress
oNICInfo.IPAddress = oInstNWAC.IPAddress(0)
GetAllPropertiesDesc(GetLast4Digit(oInstNWA.Caption))
rem StdOut.Write " (" & strPropertyDesc & ")" & vbTab
If (True = bStoringToDB) Then
StoreToDatabase
If (0 <> err.number) Then
StdOut.WriteLine "Cant Save to Database. Error: " & Err.Description
End If
End If
StdOut.WriteLine
End If
End If
Next
End If
End If
next
End If
End If
End If
'next
End Sub
rem End If
rem Get last 4 digits from Caption String
Function GetLast4Digit(oString)
rem split the sting with ]
Dim oArray
oArray = Split(oString, "]", -1, 1)
rem get the last 4 digits from Caption
GetLast4Digit = Right(oArray(0), 4)
End Function
rem Get Real Caption String from a String received from Registry Value
Function GetRealCaptionStr(oString)
rem split the sting with ]
Dim oArray
oArray = Split(oString, "]", -1, 1)
rem remove the first space too
GetRealCaptionStr = LTrim(oArray(1))
End Function
rem Get the Properties Description & if found, keep the Index
Function GetAllPropertiesDesc(str4Digits)
Dim Result
Result = "FAIL2"
Dim strPropertyDesc
strPropertyDesc = ""
Dim ArrayCount
'Get the Array Count of the Properties
stop
ArrayCount = UBound(oArrayPropertyType, 1) - LBound(oArrayPropertyType, 1)
'Go through all the Property Array Elements
For iCounter = 0 to ArrayCount step 2
strPropertyDesc = GetPropertyDesc(str4Digits, oArrayPropertyType(iCounter))
If (strPropertyDesc <> "") Then
If (oArrayPropertyType(iCounter+1) <> "")Then
strPropertyDesc = strPropertyDesc & "***" & GetPropertyDesc(str4Digits, oArrayPropertyType(iCounter+1))
End If
Exit For
End If
Next
If (strPropertyDesc <> "") Then
rem if strPropertyType1ValuDesc contains 100 & Full string, the result is PASS, otherwise its FAIL
If ((InStr(1, strPropertyDesc, IDEAL_SPEED, 0)>0) and (InStr(1, UCase(strPropertyDesc), IDEAL_DUPLEX, 0)>0) ) Then
StdOut.Write "100/FULL"
oNICInfo.Result = "PASS"
oNICInfo.Notes = "Speed/Duplex is alreay set to 100FULL"
Else
StdOut.Write "Not 100 Full"
oNICInfo.Result = "FAIL"
oNICInfo.Notes = "Speed/Duplex is NOT set to 100FULL"
rem Fix It
SetPropertyDesc str4Digits, iCounter
End If
Else
StdOut.Write "ERROR"
oNICInfo.Result = "FAIL"
oNICInfo.Notes = "Couldnt Find the Registery Value for NIC Properties"
End If
End Function
Function GetPropertyDesc(str4Digits, strPropertyType)
Dim strPropertyDesc
strPropertyDesc = ""
rem get the registry value for the specific type of adapter
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits, strPropertyType, strTempValue
rem get the real string value for the registry value
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits & "\Ndi\params\" & strPropertyType & "\enum", strTempValue, strPropertyDesc
GetPropertyDesc = strPropertyDesc
End Function
Sub SetPropertyDesc(str4Digits, iCoutner)
rem get the real string value for the registry value
oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits & "\Ndi\params\" & oArrayPropertyType(iCoutner) & "\enum", arrValueNames1, arrValueTypes1
rem Clear all the previous errors
Err.Clear
rem Go through all the sub keys
For i=0 To UBound(arrValueNames1)
If ("" = oArrayPropertyType(iCoutner+1)) Then
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits & "\Ndi\params\" & oArrayPropertyType(iCoutner) & "\enum", arrValueNames1(i), arrValue
If ((InStr(1, arrValue, IDEAL_SPEED, 0)>0) and (InStr(1, UCase(arrValue), IDEAL_DUPLEX, 0)>0)) Then
oReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits, oArrayPropertyType(iCoutner), arrValueNames1(i)
rem if any eror print & exit
If Err.Number <> 0 Then
StdOut.WriteLine "FAIL2: Couldnt Fix it for Value of " & oArrayPropertyType(iCoutner) & "Error:" & Err.Description
oNICInfo.Result = "FAIL"
oNICInfo.Notes = "Can NOT fix Speed/Duplex=100FULL; Error:" & Err.Description
Else
StdOut.WriteLine "PASS2"
oNICInfo.Result = "PASS"
oNICInfo.Notes = "Fixed Speed/Duplex=100FULL"
End If
Exit For
End If
Else
oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits & "\Ndi\params\" & oArrayPropertyType(iCoutner+1) & "\enum", arrValueNames2, arrValueTypes2
rem Go through all the sub keys
For j=0 To UBound(arrValueNames2)
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits & "\Ndi\params\" & oArrayPropertyType(iCoutner) & "\enum", arrValueNames2(j), arrValue
If (InStr(1, UCase(arrValue), IDEAL_DUPLEX, 0)>0) Then
oReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits, oArrayPropertyType(iCoutner), arrValueNames2(j)
rem if any eror print & exit
If Err.Number <> 0 Then
StdOut.WriteLine "FAIL2: Couldnt Enter Value of " & oArrayPropertyType(iCoutner+1) & Err.Description
oNICInfo.Result = 0
oNICInfo.Notes = "Can NOT fix Speed/Duplex=100FULL; Error:" & Err.Description
Else
StdOut.WriteLine "PASS2: Could Fix it"
oNICInfo.Result = 1
oNICInfo.Notes = "Fixed Speed/Duplex=100FULL"
End If
Exit For
End if
Next
End If
Next
rem get the registry value for the specific type of adapter
rem oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyCurrentPath & "\" & str4Digits, strPropertyType, strTempValue
End Sub
Function GetOSVersion()
rem Get OS Version from the Current Version Registry Key
oReg.GetStringValue HKEY_LOCAL_MACHINE, strCurrentVersion, "CurrentVersion", strTempValue
GetOSVersion = strTempValue
End Function
Sub ShowUsage
StdOut.WriteLine "---------------------------"
StdOut.Write "setnic.vbs - Sets the NIC's Speed & Duplex to 100 FULL & displays WINS Name, Result, Details" & vbNewLine
StdOut.WriteLine "---------------------------"
StdOut.Write vbNewLine & _
"Help Information: " & vbNewLine & _
"Set NIC's Speed & Duplex to 100 FULL & displays WINS Name, Result, Details(PASS1, if no need to change, FAIL1PASS2 if could fix it, FAIL1FAIL2 if couldnt fix it)" & vbNewLine & _
"setnic.vbs -m/-Machine WINSName1 [WINSName2] [WINSName3] [...]" & vbNewLine & _
"or setnic.vbs -m . for the localmachine" & vbNewLine & _
vbNewLine & _
"Parameters:" & vbNewLine & _
"WINSName1,2,...Use Machine Name(WINS Name) as the argument to get NIC info. " & vbNewLine & _
vbNewLine & _
"WINSFileName Use .dat FileName as the argument to get NIC info. " & vbNewLine & _
vbNewLine & _
"Example:" & vbNewLine & _
"setnic.vbs -m Computername1 Computername2" & vbNewLine & _
"or setnic.vbs -m . " & vbNewLine & _
"or setnic.vbs -h" & vbNewLine & _
vbNewLine & _
"Error:" & vbNewLine & _
"If the computer doesnt exist or user running the script doesnt have permission to access, it will print an error" & vbNewLine
StdOut.WriteLine "---------------------------"
End Sub
Function RetrieveArguments()
Set oArgs = WScript.Arguments
ArgsCount = oArgs.Count
Dim iCounter
iCounter = 0
bGotFile = False
bGotMachine = False
oComputerCount = 0
rem Get all the Arguments in the Computer Name Array
While (iCounter < ArgsCount)
LowerArg = LCase(oArgs(iCounter))
Select Case LowerArg
Case "/?", _
"/help", _
"/h", _
"-h", _
"--help"
RetrieveArguments = True
Exit Function
'Case "-Machine", _
' "-m"
' If (bGotFile = True) or (bGotMachine = True) Then
' RetrieveArguments = True
' Exit Function
' End If
'bGotMachine = True
Case Else
If (bGotMachine = True) Then
oDictionary.Add oComputerCount, oArgs(iCounter)
oComputerCount = oComputerCount + 1
set strComputer = "."
Else
RetrieveArguments = True
Exit Function
End If
End Select
iCounter = iCounter + 1
WEnd
End Function