Option Explicit

Dim SValueSet, SessionObject, NetServiceObject
dim WbemServices,NetServiceSet,strNetSerObjPath,FinalTargetobj
dim AdapterSets, adapter
dim retVal, Method, inParams, outParams, outParamsISCSIBoot
Dim OSVersion, OSProductType
dim regAccess
dim strKeyPath, strValueName, szLCID
dim retValue

dim bInitializeValues: bInitializeValues = 1
if Wscript.Arguments.Count > 0 then 
	bInitializeValues = CBool(Wscript.Arguments(0))
end if

const KEY_QUERY_VALUE 	= &H0001
const HKEY_LOCAL_MACHINE = &H80000002
Const wbemFlagReturnWhenComplete = &h0

const ETHERNET_ADAPTER_CAPABILITY_INTEL = 47

GetOSVersion()

if (OSVersion <> "6.0")  then
	Set wbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}//./root/IntelNcs2")
else
	set regAccess = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

	strKeyPath = "SOFTWARE\Intel\Network_Services\DMIX"
	strValueName = "LCID"

	regAccess.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName, szLCID
	
	Set wbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}[Locale=ms_" & szLCID & "]//./root/IntelNcs2")	
end if

wbemServices.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
Set SValueSet = CreateObject("WbemScripting.SWbemNamedValueSet")

Set AdapterSets = wbemServices.InstancesOf("IANet_PhysicalEthernetAdapter",0)	


CreateSessions()

' iterate through the adapters
For each adapter in AdapterSets
	If (IsIntelAdapter(adapter) AND (adapter.StatusInfo= 3)) then			
		on error resume next
		' set input parameter to true to initialize the profile setting on the device
		Set inParams = adapter.Methods_.Item("InitializePerformanceProfilesOnDevices").InParameters.SpawnInstance_()

		inParams.bInitializeValues = bInitializeValues

		' call the InitializePerformanceProfilesOnDevices for the current adapter
		set retVal = adapter.ExecMethod_("InitializePerformanceProfilesOnDevices",inParams,0,SValueSet)

		' if there was an error, output an error
		if err.number <> 0 then	
			wscript.echo "Unable to update settings for " & adapter.Caption
		end if
			
		' this function initializes the profile for all devices, no need to continue.			
		exit for		
	End if
Next

ExecApply  wbemServices, strNetSerObjPath, SessionObject 

function IsIntelAdapter(ByVal adapter)
    dim retVal : retVal = false
    dim cap

    for each cap in adapter.Capabilities

        if cap = ETHERNET_ADAPTER_CAPABILITY_INTEL then
            retVal = true
            exit for
        end if
    next

    IsIntelAdapter = retVal
end function
'==================================================================================================
'
' CreateSessions()
' Create sessions to make settings changes
'
'==================================================================================================
Sub CreateSessions()
	Dim IANet_NetServiceClassDescription,cstring
	Dim Method 
	Dim ServiceObject,TempObj

	Set IANet_NetServiceClassDescription = wbemServices.Get("IANet_NetService")
	Set SessionObject = IANet_NetServiceClassDescription.Methods_.Item("Apply").InParameters.SpawnInstance_()  

	Set NetServiceSet = wbemServices.InstancesOf("IANet_NetService")

	For Each NetServiceObject In NetServiceSet	
		strNetSerObjPath = NetServiceObject.Path_.Path	
		Set Method = NetServiceObject.Methods_("BeginApply")			' Save the path for later use
		Set ServiceObject = NetServiceObject.ExecMethod_("BeginApply")
		If ServiceObject.returnValue = 0 Then
			cstring = ServiceObject.ClientSetHandle
		End If
	
	Next

	SValueSet.Add "ClientSetId", cstring
	SessionObject.ClientSetHandle = cstring	
End Sub

'=====================================================================================
'
' Sub:	Sub ExecApply(ByVal wbemServices, ByVal strNetSerObjPath, ByVal SessionObject)
'  
'=====================================================================================
Sub ExecApply(ByVal wbemServices, ByVal strNetSerObjPath, ByVal SessionObject)
	Dim StdOut,oReg,strComputer,strKeyPath,strValueName,strValue
	
	WScript.Echo "Executing apply..."	
	Set NetServiceObject = wbemServices.Get(strNetSerObjPath)
	Set FinalTargetobj = NetServiceObject.ExecMethod_("Apply", SessionObject)
	'WScript.Echo "FinalTargetobj.FollowupAction" & FinalTargetobj.FollowupAction
	if FinalTargetobj.FollowupAction = 1 Then
		WScript.Echo "FinalTargetobj.FollowupAction" & FinalTargetobj.FollowupAction
		strComputer = "."
		Set StdOut = WScript.StdOut
		Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_ 
		strComputer & "\root\default:StdRegProv")
		strKeyPath ="SOFTWARE\INTEL\Network_Services\DMIX"
		strValueName = "RebootReq"
		strValue = "1"
		oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
	End if
End Sub

'=====================================================================================
'
' Sub:	Sub ReleaseHandle(ByVal wbemServices, ByVal strNetSerObjPath, ByVal SessionObject)
'  
'=====================================================================================
Sub ReleaseHandle(ByVal wbemServices, ByVal strNetSerObjPath, ByVal SessionObject)
	Set NetServiceObject = wbemServices.Get(strNetSerObjPath)
	Set FinalTargetobj = NetServiceObject.ExecMethod_("Apply", SessionObject)
	WScript.Echo "Released Session Handle"

End Sub

'=====================================================================================
'
' Sub:	Sub GetOSVersion()

'  
'=====================================================================================
Sub GetOSVersion()
	Dim objWMI, objItem, colItems

	Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
	Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)

	For Each objItem in colItems
	  OSVersion = Left(objItem.Version,3)

	  ' Stop errors from causing the script to fail. 
	  ' ProductType is not in the Windows 2000 or NT4 WMI
	  On Error Resume Next
	    OSProductType = Left(objItem.ProductType,2)
	
	    ' If there was an error accessing this property, we know the OS is Windows 2000 or NT4
	    if err.number <> 0 then
		' Set OSProductType to an arbitrary value.  It is only checked if the OSVersion is 5.2
		OSProductType = -1
 	    end if
	  On Error Goto 0  ' Allow errors to halt the script again
	Next		
End Sub