Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Migrating a users printer from one print server to another while retaining users preferences

$
0
0

Our existing file server is also the print server. We brought up another dedicated print server and exported from one and imported to the other. The goal is to bring down print services on the first server after everyone is using the new one. We dont want to visit each user and we want the existing user print preferences to be retained. The following script is run as a user logoff script and preserves users print preferences while changing the printer path references to the new server. Any comments, suggestions and improvements are welcome. The script is not intended to modify local administratively installed or GPO deployed printers.

On Error Resume Next

Const HKEY_CURRENT_USER = &H80000001
Const REG_BINARY = 3
strComputer = "."
strConnectionsKeyPath = "Printers\Connections"
strDevModes2KeyPath = "Printers\DevModes2"
strSettingsKeyPath = "Printers\Settings"

strFs1 = "ord-pdxv-fs1"
strLFs1 = lcase(strFs1)
strUFs1 = ucase(strFs1)
strMFs1 = ucase(left(strFs1, 1))&right(strFs1, len(strFs1)-1)
arrstrFs1=array(strLFs1, strUFs1, strMFs1)
j=0
dim arrBinFs1()
ReDim arrBinFs1(ubound(arrstrFs1))
strBinPs1 = "111,0,114,0,100,0,45,0,112,0,100,0,120,0,118,0,45,0,112,0,115,0,49,0,"
strPs1 = "ord-pdxv-ps1"


ServerValue = "Server"
ProviderValue = "Provider"
LocalConnectionValue = "LocalConnection"
GuidPrinterValue = "GuidPrinter"
DevModeValue = "DevMode"

for each str in arrstrFs1
strBinFs1 = ""
for i = 1 to len(str)
strBinFs1 = strBinFs1 & asc(mid(str, i, 1))&",0,"
next
arrBinFs1(j) = strBinFs1
j = j+1
next

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

objReg.GetStringValue HKEY_CURRENT_USER,"Software\Microsoft\Windows NT\CurrentVersion\Windows","Device", strDefPrinterKey

If Instr(lcase(strDefPrinterKey), strFs1) <> 0 Then
	objReg.SetStringValue HKEY_CURRENT_USER,"Software\Microsoft\Windows NT\CurrentVersion\Windows","Device",replace(lcase(strDefPrinterKey),strFs1, strPs1)
End If

objReg.EnumKey HKEY_CURRENT_USER, strConnectionsKeyPath, arrSubKeys

For Each Printer in arrSubKeys
	If Instr(lcase(Printer), strFs1) <> 0 Then

	PrinterKey = strConnectionsKeyPath&"\"&Printer
	NewPrinterKey = strConnectionsKeyPath&"\"&replace(lcase(Printer), strFs1, strPs1)

		objReg.GetStringValue HKEY_CURRENT_USER,PrinterKey,ServerValue,strServerValue
		objReg.GetStringValue HKEY_CURRENT_USER,PrinterKey,ProviderValue,strProviderValue
		objReg.GetDWORDValue HKEY_CURRENT_USER,PrinterKey,LocalConnectionValue,dwLocalConnectionValue
		objReg.GetStringValue HKEY_CURRENT_USER,PrinterKey,GuidPrinterValue,strGuidPrinterValue
		objReg.GetBinaryValue HKEY_CURRENT_USER,PrinterKey,DevModeValue,arrDevModeValue

		objReg.CreateKey HKEY_CURRENT_USER,NewPrinterKey

		objReg.SetStringValue HKEY_CURRENT_USER,NewPrinterKey,ServerValue,replace(lcase(strServerValue), strFs1, strPs1)
		objReg.SetStringValue HKEY_CURRENT_USER,NewPrinterKey,ProviderValue,strProviderValue
		objReg.SetDWORDValue HKEY_CURRENT_USER,NewPrinterKey,LocalConnectionValue,dwLocalConnectionValue
		objReg.SetStringValue HKEY_CURRENT_USER,NewPrinterKey,GuidPrinterValue,strGuidPrinterValue

		for each strBinFs1 in arrBinFs1
			strDevModeValue = Join(arrDevModeValue,",")
			arrDevModeValue = split(replace(strDevModeValue,strBinFs1,strBinPs1),",")
			objReg.SetBinaryValue HKEY_CURRENT_USER,NewPrinterKey,DevModeValue,arrDevModeValue
		next
		objReg.DeleteKey HKEY_CURRENT_USER,PrinterKey

	End If
Next

objReg.EnumValues HKEY_CURRENT_USER,strDevModes2KeyPath,arrEntryNames,arrValueTypes

For i=0 To UBound(arrEntryNames)
	Select Case arrValueTypes(i)
		Case REG_BINARY
			objReg.GetBinaryValue HKEY_CURRENT_USER, strDevModes2KeyPath, arrEntryNames(i),arrDevMode2Value
			for each strBinFs1 in arrBinFs1
			strDevMode2Value = Join(arrDevMode2Value,",")
					arrDevMode2Value = split(replace(strDevMode2Value,strBinFs1,strBinPs1),",")
					If instr(lcase(arrEntryNames(i)), strFs1) <> 0 Then
						objReg.SetBinaryValue HKEY_CURRENT_USER,strDevModes2KeyPath,lcase(replace(arrEntryNames(i),strFs1,strPs1)),arrDevMode2Value
						objReg.DeleteValue HKEY_CURRENT_USER,strDevModes2KeyPath,arrEntryNames(i)
					Else
						objReg.SetBinaryValue HKEY_CURRENT_USER,strDevModes2KeyPath,arrEntryNames(i),arrDevMode2Value
					End If
			next
	End Select
Next



objReg.EnumValues HKEY_CURRENT_USER,strSettingsKeyPath,arrEntryNames,arrValueTypes

For i=0 To UBound(arrEntryNames)
	Select Case arrValueTypes(i)
		Case REG_BINARY
			objReg.GetBinaryValue HKEY_CURRENT_USER, strSettingsKeyPath, arrEntryNames(i),arrSettingsValue
			for each strBinFs1 in arrBinFs1
			strSettingsValue = Join(arrSettingsValue,",")
					arrSettingsValue = split(replace(strSettingsValue,strBinFs1,strBinPs1),",")
					If instr(lcase(arrEntryNames(i)), strFs1) <> 0 Then
						objReg.SetBinaryValue HKEY_CURRENT_USER,strSettingsKeyPath,lcase(replace(arrEntryNames(i),strFs1,strPs1)),arrSettingsValue
						objReg.DeleteValue HKEY_CURRENT_USER,strSettingsKeyPath,arrEntryNames(i)
					Else
						objReg.SetBinaryValue HKEY_CURRENT_USER,strDevModes2KeyPath,arrEntryNames(i),arrSettingsValue
					End If
			Next
	End Select
Next



Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>