Wednesday, November 01, 2006

Changing Network Provider order in Windows

Problem: I needed to put the "Microsoft Windows Network" network provider on top of the other (Novell Netware) providers. This is to get rid of the 15 second delay the Novell client does when trying to access a Windows file server.

This is pretty easy to do manually (Network Connections->Advanced->Advanced Settings->Provider Order), but a little more difficult to do manually. The registry key "HKLM\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\ProviderOrder" lists the providers as a comma separated string, for example "NetwareWorkstation,RDPNP,LanmanWorkstation,WebClient"

Unfortunately, the installed providers aren't really predictable. For example, if the computer has PointSec drive encryption installed, a provider named "PssoCM32" is listed somewhere in the end of the string.

Solution: Read the string from the registry, split it up, create a new string beginning with "LanmanWorkstation" (which of course is the "Microsoft Windows Network" provider), attach the rest of the providers to the new string, in the same order they were originally listed.

VBScript Code (successfully made unreadable by blogger.com, but is copy-pasteable to Notepad):

'
' ChangeProviderOrder.vbs, by Anders Olsson, Kentor Teknik AB, 2006-10-31
'
' Reads the "Network provider order" from the registry and reorders it putting
' the "Microsoft Windows Networking" provider on top.
'
' Example: Before - "NCredMgr,NetwareWorkstation,RDPNP,LanmanWorkstation,WebClient"
' would become "LanmanWorkstation,NCredMgr,NetwareWorkstation,RDPNP,WebClient" after
' running this script.
'

Set WshShell = WScript.CreateObject("WScript.Shell")

' Read the reg value of the providers
strKey = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\ProviderOrder")

' Split the strings up using comma (ASCII #44) as the delimiter
arrProvs = Split(strKey, chr(44), -1, 1)

' If LanmanWorkstation is already first, we don't have to do anything
If arrProvs(0) = "LanmanWorkstation" Then
Wscript.Quit(0)
end if

' "LanmanWorkstation" should always start the string
strNewProvs = "LanmanWorkstation"

' Loop through the old provider strings, and add them to the new string. Don't
' write LanmanWorkstation, since it's already written at the start of the string.
For Each strProv In arrProvs
Select Case strProv
Case "LanmanWorkstation"
Case Else strNewProvs = strNewProvs & "," & strProv
End Select
Next

' Write the new string back to the registry
WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\ProviderOrder", strNewProvs, "REG_SZ"