[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 479
  • Last Modified:

VBS Script - Modify Remote Registry on Three Servers...

All, I need a VBS Script that:

1 - Will connect to multiple remote servers (defined in the script).
2 - Must connect to the servers using a username and password defined in the script.
3 - Will modify an existing registry key to a new value:

Value to be set:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo]
"ALIASDBSERVER1"="DBMSSOCN,SERVERNAME2"

This should not include any script popups, etc as it will need to run without user intervention.

Any assistance would be greatly appreciated.
0
Darksied9
Asked:
Darksied9
  • 9
  • 6
1 Solution
 
andrewrnzCommented:
Seems like a lot of trouble creating a vbscript to do this for just 3 servers. Can't you just RDP to the servers and do this manually? I am guessing the answer to my question is "no" but I thought it best to ask before I go and spend quite some time writing and testing such a script... You could also do it manually by firing up regedit and then clicking File > Connect Network Registry then type in your server name. If that fails the first time, just make a UNC connection in Explorer first and then try again.
0
 
Darksied9Author Commented:
LOL, unfortunately this is being done to programatically fail over a non-cluster aware set of servers when a database cluster fails over to the secondary server, so yes I could do it manually, but we need the automation.  If you could help me out I would be great-full.
0
 
RobSampsonCommented:
Hi there, this script should do that for you.

Regards,

Rob.
arrServers = Array("Server1","Server2","Server3")
strUsername = "domain\username"
strPassword = "password"
strRegPath = "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo"
strRegKey = "ALIASDBSERVER1"
strNewValue = "DBMSSOCN,SERVERNAME2"
For Each strServer In arrServers
	Set objLocator = CreateObject("WBemScripting.SWbemLocator")
	On Error Resume Next
	Set objWMIService = objLocator.ConnectServer(strServer, "\root\default", strUsername, strPassword)
	If Err.Number <> 0 Then
		MsgBox "Unable to connect to " & strServer & ". Error " & Err.Number & ": " & Err.Description
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0
		objWMIService.Security_.ImpersonationLevel = 3
		objWMIService.Security_.AuthenticationLevel = 6
		Set objReg = objWMIService.Get("StdRegProv")
		objReg.SetStringValue HKEY_LOCAL_MACHINE, strRegPath, strRegKey, strValue
	End If
Next

Open in new window

0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
Darksied9Author Commented:
I get absolutly nothing happening with this VBS.  I do not see any attempted security credential attempts on the target servers, so I suspect it is not getting that far. I removed the username/password variables as well as the same from the connect string and ran it from an account with authority.  Nothing.  Any clue?
0
 
RobSampsonCommented:
OK, I'll put in some messages so you can see where it gets up to.

Regards,

Rob.
arrServers = Array("Server1","Server2","Server3")
strUsername = "domain\username"
strPassword = "password"
strRegPath = "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo"
strRegKey = "ALIASDBSERVER1"
strNewValue = "DBMSSOCN,SERVERNAME2"
For Each strServer In arrServers
	If Ping(strServer) = True Then
		Set objLocator = CreateObject("WBemScripting.SWbemLocator")
		On Error Resume Next
		Set objWMIService = objLocator.ConnectServer(strServer, "\root\default", strUsername, strPassword)
		If Err.Number <> 0 Then
			MsgBox "Unable to connect to " & strServer & ". Error " & Err.Number & ": " & Err.Description
			Err.Clear
			On Error GoTo 0
		Else
			objWMIService.Security_.ImpersonationLevel = 3
			objWMIService.Security_.AuthenticationLevel = 6
			Set objReg = objWMIService.Get("StdRegProv")
			MsgBox "Setting " & strRegPath & "\" & strRegKey & " to " & strNewValue & " on " & strServer
			objReg.SetStringValue HKEY_LOCAL_MACHINE, strRegPath, strRegKey, strNewValue
			If Err.Number <> 0 Then
				MsgBox "Error modifying registry on " & strServer & ". Error " & Err.Number & ": " & Err.Description
				Err.Clear
				On Error GoTo 0
			Else
				On Error GoTo 0
				MsgBox "Registry change successful on " & strServer
			End If
		End If
	Else
		MsgBox strServer & " is offline."
	End If
Next

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 
Darksied9Author Commented:
Another note, I tried running it locally from one of the traget servers and was informed that can not use username for local connections which implies that it is trying to connect.
0
 
RobSampsonCommented:
>> and was informed that can not use username for local connections

That is correct.  If you try to connect to the same computer the script is running from with alternate credentials, it will not work.  If you need to do that, you need to leave out the credentials.

Rob.
0
 
Darksied9Author Commented:
I ran the script with the checks.  It processes all the way through no issues, tells me everything is done and complete.  If I then manually browse to the registry key I see nothing changes.  I am wondering if it is writing it to a virtual reg hive or something...
0
 
RobSampsonCommented:
Have you checked the same value on the pc you have ran it from? Maybe it's changing that instead....I'll run some checks....

Rob.
0
 
Darksied9Author Commented:
I also ran a registry search, and did not find the updated string on any of my three servers.  I validated the following:

WMI Running
Remote Registry Running

I do see this in the WMIFramework Log:

Shell Name Explorer.exe in Registry not found in process list.      04/06/2010 15:03:29.000      thread:3036      [d:\nt\admin\wmi\wbem\providers\win32provider\common\implogonuser.cpp.1019]
Unable to locate Shell Process, Impersonation failed.      04/06/2010 15:03:29.015      thread:3036      [d:\nt\admin\wmi\wbem\providers\win32provider\common\implogonuser.cpp.1027]

Not sure if that applies.
0
 
Darksied9Author Commented:
It is not changing it in the local server I am running it from, so we know the connections strings are good.
0
 
Darksied9Author Commented:
On a side note, it may say that the key is successfully set because there is already a key there that we are updating.  If you are not checking the value, then the validation might not be accurate.
0
 
RobSampsonCommented:
Oh, I know what I did wrong.  I forgot to add the HKEY_LOCAL_MACHINE constant, and also remove the HKLM\ from strRegPath.  I have tested this. It works for me.

Regards,

Rob.
Const HKEY_LOCAL_MACHINE = &H80000002

arrServers = Array("Server1","Server2","Server3")
strUsername = "domain\username"
strPassword = "password"
strRegPath = "SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo"
strRegKey = "ALIASDBSERVER1"
strNewValue = "DBMSSOCN,SERVERNAME2"

For Each strServer In arrServers
	If Ping(strServer) = True Then
		Set objLocator = CreateObject("WBemScripting.SWbemLocator")
		On Error Resume Next
		Set objWMIService = objLocator.ConnectServer(strServer, "\root\default", strUsername, strPassword)
		If Err.Number <> 0 Then
			MsgBox "Unable to connect to " & strServer & ". Error " & Err.Number & ": " & Err.Description
			Err.Clear
			On Error GoTo 0
		Else
			objWMIService.Security_.ImpersonationLevel = 3
			objWMIService.Security_.AuthenticationLevel = 6
			Set objReg = objWMIService.Get("StdRegProv")
			MsgBox "Setting " & strRegPath & "\" & strRegKey & " to " & strNewValue & " on " & strServer
			objReg.SetStringValue HKEY_LOCAL_MACHINE, strRegPath, strRegKey, strNewValue
			If Err.Number <> 0 Then
				MsgBox "Error modifying registry on " & strServer & ". Error " & Err.Number & ": " & Err.Description
				Err.Clear
				On Error GoTo 0
			Else
				On Error GoTo 0
				MsgBox "Registry change successful on " & strServer
			End If
		End If
	Else
		MsgBox strServer & " is offline."
	End If
Next

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 
Darksied9Author Commented:
I had just tried stripping out the HKEY_LOCAL_MACHINE and got an error because I thought it was duplicate.  I am glad your version worked!  I have commented out the Messages for everything except errors and it looks great!  Thank you very much!
0
 
Darksied9Author Commented:
Perfect, if I ever need code again you will be the first I go to.
0
 
RobSampsonCommented:
No problem.  Thanks for the grade.  Next time I'll test it before I provide you code :-)

Rob.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now