
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-23-2008 07:28 PM
I found the script below online and was wondering if it could be modified to be a lansweeper action to set a default printer.
'Set variables ( remote installation -=Network Printers=- )
strPrintServer = "servername"
StrPrinterName = "displayname printer" ' <--- display-name on the server!
sAsDefault = "yes" ' <--- choose "yes" or "no" if make it the default printer for the logged-on user.
strInputFile = "C:\computers.txt"
'*****************************************************************
'* Get SID from currently logged-on DomainUser
'*****************************************************************
'\
'script: "RemoteEditRegistry.vbs"
'created on 7 march 07 by Remco Simons [nl]
'
' This version 1.0
' (http://www.petri.co.il/forums/showthread.php?t=14109)
'The script can only be runned by a member of the 'Domain Admins' group!
'/
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
On Error Resume Next
'Get the Computernames for processing from file (an other option would be a LDAP-query AD for computernames)
Set objController = CreateObject("WshController")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTSIn = objFSO.OpenTextFile(strInputFile)
Do Until objTSIn.AtEndOfStream '<---------------[begin loop, to process computers from list one-by-one]
strComputer = objTSIn.ReadLine
who = "winmgmts:{impersonationLevel=impersonate}!//"& strComputer &""
Set Users = GetObject( who ).InstancesOf ("Win32_ComputerSystem")
for each User in Users
strUsr = right(User.UserName,len(User.UserName)-InStr(User.UserName,"\"))
strDomain = left(User.UserName,InStr(User.UserName,"\")-1)
exit For
next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colAccounts = objWMIService.ExecQuery _
("select * From Win32_UserAccount where name = '" & strUsr & "' AND domain = '" & strDomain & "'")
For each objAccount in colAccounts
strSID = objAccount.SID
exit For
Next
'You can now use the SID to edit the HKEY_USERS\{SID}-key of the logged-on Domain User...
'(you must end the main part of the script with: Loop [enter] objTSIn.Close )
'*****************************************************************
' -=Add and connect the network printer=-
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
'Parameters Key(4x) and values(5x)
strKeyPath1 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices"
ValueName1 = "\\" & strPrintServer & "\" & StrPrinterName
'***Count the already installed 'net'port printers for the next available number
Set oMethod = objReg.Methods_("EnumValues")
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_USERS
oInParam.sSubKeyName = strKeyPath1
Set oOutParam = objReg.ExecMethod_("EnumValues", oInParam)
xCount = 0
For i=0 To UBound(oOutParam.Properties_("sNames"))
sName = oOutParam.Properties_("sNames")(i)
objReg.GetStringValue HKEY_USERS,strKeyPath1,sName,dwValue
If Not InStr(1,dwValue,"winspool,LPT",0) = 0 Then
'LPT port
ELSE 'Ne.. ports
If sName = ValueName1 Then
strValue1 = dwValue
ELSE
xCount = xCount + 1
End If
End If
Next
sNe = "Ne"& 0&xCount &":"
If not strValue1 = dwValue then strValue1 = "winspool," & sNe
strKeyPath2 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
ValueName2 = ValueName1
strValue2 = strValue1 & ",15,45"
strKeyPath3 = strSID & "\Printers\Connections\,," & strPrintServer & "," & StrPrinterName
ValueName3a = "Server"
strValue3a = "\\" & strPrintServer
ValueName3b = "Provider"
strValue3b = "win32spl.dll"
strKeyPath4 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows"
ValueName4 = "Device"
strValue4 = ValueName1 & "," & strValue1
'Add the printer to the 'CURRENT'_USER's path
objReg.CreateKey HKEY_USERS, strKeyPath3
objReg.SetStringValue HKEY_USERS, strKeyPath2, ValueName2, strValue2
objReg.SetStringValue HKEY_USERS, strKeyPath1, ValueName1, strValue1
objReg.SetStringValue HKEY_USERS, strKeyPath3, ValueName3a, strValue3a
objReg.SetStringValue HKEY_USERS, strKeyPath3, ValueName3b, strValue3b
'Make it the default printer Y/N?
If LCase(sAsDefault) = "yes" Then
objReg.SetStringValue HKEY_USERS, strKeyPath4,ValueName4, strValue4
End If
Loop
objTSIn.Close '<--- End of list computernames, close InputFile
On Error GoTo 0
wscript.echo "Done!"
wscript.quit
'Set variables ( remote installation -=Network Printers=- )
strPrintServer = "servername"
StrPrinterName = "displayname printer" ' <--- display-name on the server!
sAsDefault = "yes" ' <--- choose "yes" or "no" if make it the default printer for the logged-on user.
strInputFile = "C:\computers.txt"
'*****************************************************************
'* Get SID from currently logged-on DomainUser
'*****************************************************************
'\
'script: "RemoteEditRegistry.vbs"
'created on 7 march 07 by Remco Simons [nl]
'
' This version 1.0
' (http://www.petri.co.il/forums/showthread.php?t=14109)
'The script can only be runned by a member of the 'Domain Admins' group!
'/
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
On Error Resume Next
'Get the Computernames for processing from file (an other option would be a LDAP-query AD for computernames)
Set objController = CreateObject("WshController")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTSIn = objFSO.OpenTextFile(strInputFile)
Do Until objTSIn.AtEndOfStream '<---------------[begin loop, to process computers from list one-by-one]
strComputer = objTSIn.ReadLine
who = "winmgmts:{impersonationLevel=impersonate}!//"& strComputer &""
Set Users = GetObject( who ).InstancesOf ("Win32_ComputerSystem")
for each User in Users
strUsr = right(User.UserName,len(User.UserName)-InStr(User.UserName,"\"))
strDomain = left(User.UserName,InStr(User.UserName,"\")-1)
exit For
next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colAccounts = objWMIService.ExecQuery _
("select * From Win32_UserAccount where name = '" & strUsr & "' AND domain = '" & strDomain & "'")
For each objAccount in colAccounts
strSID = objAccount.SID
exit For
Next
'You can now use the SID to edit the HKEY_USERS\{SID}-key of the logged-on Domain User...
'(you must end the main part of the script with: Loop [enter] objTSIn.Close )
'*****************************************************************
' -=Add and connect the network printer=-
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
'Parameters Key(4x) and values(5x)
strKeyPath1 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices"
ValueName1 = "\\" & strPrintServer & "\" & StrPrinterName
'***Count the already installed 'net'port printers for the next available number
Set oMethod = objReg.Methods_("EnumValues")
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_USERS
oInParam.sSubKeyName = strKeyPath1
Set oOutParam = objReg.ExecMethod_("EnumValues", oInParam)
xCount = 0
For i=0 To UBound(oOutParam.Properties_("sNames"))
sName = oOutParam.Properties_("sNames")(i)
objReg.GetStringValue HKEY_USERS,strKeyPath1,sName,dwValue
If Not InStr(1,dwValue,"winspool,LPT",0) = 0 Then
'LPT port
ELSE 'Ne.. ports
If sName = ValueName1 Then
strValue1 = dwValue
ELSE
xCount = xCount + 1
End If
End If
Next
sNe = "Ne"& 0&xCount &":"
If not strValue1 = dwValue then strValue1 = "winspool," & sNe
strKeyPath2 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
ValueName2 = ValueName1
strValue2 = strValue1 & ",15,45"
strKeyPath3 = strSID & "\Printers\Connections\,," & strPrintServer & "," & StrPrinterName
ValueName3a = "Server"
strValue3a = "\\" & strPrintServer
ValueName3b = "Provider"
strValue3b = "win32spl.dll"
strKeyPath4 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows"
ValueName4 = "Device"
strValue4 = ValueName1 & "," & strValue1
'Add the printer to the 'CURRENT'_USER's path
objReg.CreateKey HKEY_USERS, strKeyPath3
objReg.SetStringValue HKEY_USERS, strKeyPath2, ValueName2, strValue2
objReg.SetStringValue HKEY_USERS, strKeyPath1, ValueName1, strValue1
objReg.SetStringValue HKEY_USERS, strKeyPath3, ValueName3a, strValue3a
objReg.SetStringValue HKEY_USERS, strKeyPath3, ValueName3b, strValue3b
'Make it the default printer Y/N?
If LCase(sAsDefault) = "yes" Then
objReg.SetStringValue HKEY_USERS, strKeyPath4,ValueName4, strValue4
End If
Loop
objTSIn.Close '<--- End of list computernames, close InputFile
On Error GoTo 0
wscript.echo "Done!"
wscript.quit
Labels:
- Labels:
-
Custom Actions
2 REPLIES 2

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2009 12:30 AM
Good thinking, while I don't think I will use it, it may be handy some day.
The problem is that you are not telling it to look at other computers.
Attached is a script I use to run various programs on other computers, if you take the guts from this script and fit them in yours it should work remotly.
The problem is that you are not telling it to look at other computers.
Attached is a script I use to run various programs on other computers, if you take the guts from this script and fit them in yours it should work remotly.
If Wscript.Arguments.Count = 0 Then
strComputer = "."
else
strComputer = Wscript.Arguments(0)
end if
strCommand = "\\XXXXXX\XXXXX\script.exe"
Const INTERVAL = "n"
Const MINUTES = 1
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)
If errReturn = 0 Then
Wscript.Echo "xxxx was started with a process ID: " & intJobID
Else
Wscript.Echo "xxxx could not be started due to error: " & errReturn
End If

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-24-2008 04:53 PM
OK, we messed with the script a bit, but now we are stuck. It works on the machine you are running it on, but not on a remote machine like defaultprinter.vbs {computer}, which is what we would need to do to make it a Lansweeper action. The cool part is my friend figured out how to allow you to see a list of your print servers, enter the name of the printserver and then see a list of printers on that server and then enter the printer name. Not a drop down, but cool nonetheless. So, if we could get past the broken remote computer part (the script we started with used a file with computer names) it would be nice. Here is what we have so far in case anyone has any ideas or wants to use it.
'Set variables ( remote installation -=Network Printers=- )
'strprintserver = inputbox("Name of Server on which desired printer resides:","Adding Default Printer")
set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://cn=sites," & _
objRootDSE.Get("ConfigurationNamingContext") & ">;"
strFilter = "(objectcategory=server);"
strAttrs = "distinguishedName;"
strScope = "subtree"
set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
objRS.MoveFirst
strServers = "Enter below the name of the print server to check:" & vbcrlf
while Not objRS.EOF
strservers = strservers & left(objRS.Fields(0).Value,15) & vbcrlf
'Wscript.Echo objRS.Fields(0).Value
objRS.MoveNext
wend
strprintserver = inputbox(strservers,"Adding Printers","")
'StrPrinterName = "" ' <--- display-name on the server!
'strPrinterName = inputbox("Display name of desired printer:","Adding Default Printer")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strPrintServer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer WHERE PortName LIKE '%10.%' ")
strMenu = "Enter below one of these printer names:" & vbCrLf
c = 0
For Each objPrinter in colInstalledPrinters
strmenu = strmenu & objPrinter.Name
c = c + 1
if c MOD 2 =0 then
strmenu = strmenu & vbcrlf
else
strmenu = strmenu & ", "
end if
Next
strPrintername = inputbox(strmenu,"Menu","")
'wscript.echo strprinter
sasdefault = inputbox("Make Default Printer?","Adding Printer","yes")
'sAsDefault = "yes" ' <--- choose "yes" or "no" if make it the default printer for the logged-on user.
if sasDefault <> "yes" then sasDefault = "no"
'*****************************************************************
'* Get SID from currently logged-on DomainUser
'*****************************************************************
'\
'script: "RemoteEditRegistry.vbs"
'created on 7 march 07 by Remco Simons [nl]
'
' This version 1.0
' (http://www.petri.co.il/forums/showthread.php?t=14109)
'The script can only be runned by a member of the 'Domain Admins' group!
'/
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
On Error Resume Next
strComputer = "WAMN-MIS-NIKE2"
who = "winmgmts:{impersonationLevel=impersonate}!//"& strComputer &""
Set Users = GetObject( who ).InstancesOf ("Win32_ComputerSystem")
for each User in Users
strUsr = right(User.UserName,len(User.UserName)-InStr(User.UserName,"\"))
strDomain = left(User.UserName,InStr(User.UserName,"\")-1)
exit For
next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colAccounts = objWMIService.ExecQuery _
("select * From Win32_UserAccount where name = '" & strUsr & "' AND domain = '" & strDomain & "'")
For each objAccount in colAccounts
strSID = objAccount.SID
exit For
Next
'You can now use the SID to edit the HKEY_USERS\{SID}-key of the logged-on Domain User...
'(you must end the main part of the script with: Loop [enter] objTSIn.Close )
'*****************************************************************
' -=Add and connect the network printer=-
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
'Parameters Key(4x) and values(5x)
strKeyPath1 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices"
ValueName1 = "\\" & strPrintServer & "\" & StrPrinterName
'***Count the already installed 'net'port printers for the next available number
Set oMethod = objReg.Methods_("EnumValues")
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_USERS
oInParam.sSubKeyName = strKeyPath1
Set oOutParam = objReg.ExecMethod_("EnumValues", oInParam)
xCount = 0
For i=0 To UBound(oOutParam.Properties_("sNames"))
sName = oOutParam.Properties_("sNames")(i)
objReg.GetStringValue HKEY_USERS,strKeyPath1,sName,dwValue
If Not InStr(1,dwValue,"winspool,LPT",0) = 0 Then
'LPT port
ELSE 'Ne.. ports
If sName = ValueName1 Then
strValue1 = dwValue
ELSE
xCount = xCount + 1
End If
End If
Next
sNe = "Ne"& 0&xCount &":"
If not strValue1 = dwValue then strValue1 = "winspool," & sNe
strKeyPath2 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
ValueName2 = ValueName1
strValue2 = strValue1 & ",15,45"
strKeyPath3 = strSID & "\Printers\Connections\,," & strPrintServer & "," & StrPrinterName
ValueName3a = "Server"
strValue3a = "\\" & strPrintServer
ValueName3b = "Provider"
strValue3b = "win32spl.dll"
strKeyPath4 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows"
ValueName4 = "Device"
strValue4 = ValueName1 & "," & strValue1
'Add the printer to the 'CURRENT'_USER's path
objReg.CreateKey HKEY_USERS, strKeyPath3
objReg.SetStringValue HKEY_USERS, strKeyPath2, ValueName2, strValue2
objReg.SetStringValue HKEY_USERS, strKeyPath1, ValueName1, strValue1
objReg.SetStringValue HKEY_USERS, strKeyPath3, ValueName3a, strValue3a
objReg.SetStringValue HKEY_USERS, strKeyPath3, ValueName3b, strValue3b
'Make it the default printer Y/N?
If LCase(sAsDefault) = "yes" Then
objReg.SetStringValue HKEY_USERS, strKeyPath4,ValueName4, strValue4
End If
On Error GoTo 0
wscript.echo "Done!"
wscript.quit
'Set variables ( remote installation -=Network Printers=- )
'strprintserver = inputbox("Name of Server on which desired printer resides:","Adding Default Printer")
set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://cn=sites," & _
objRootDSE.Get("ConfigurationNamingContext") & ">;"
strFilter = "(objectcategory=server);"
strAttrs = "distinguishedName;"
strScope = "subtree"
set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
objRS.MoveFirst
strServers = "Enter below the name of the print server to check:" & vbcrlf
while Not objRS.EOF
strservers = strservers & left(objRS.Fields(0).Value,15) & vbcrlf
'Wscript.Echo objRS.Fields(0).Value
objRS.MoveNext
wend
strprintserver = inputbox(strservers,"Adding Printers","")
'StrPrinterName = "" ' <--- display-name on the server!
'strPrinterName = inputbox("Display name of desired printer:","Adding Default Printer")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strPrintServer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer WHERE PortName LIKE '%10.%' ")
strMenu = "Enter below one of these printer names:" & vbCrLf
c = 0
For Each objPrinter in colInstalledPrinters
strmenu = strmenu & objPrinter.Name
c = c + 1
if c MOD 2 =0 then
strmenu = strmenu & vbcrlf
else
strmenu = strmenu & ", "
end if
Next
strPrintername = inputbox(strmenu,"Menu","")
'wscript.echo strprinter
sasdefault = inputbox("Make Default Printer?","Adding Printer","yes")
'sAsDefault = "yes" ' <--- choose "yes" or "no" if make it the default printer for the logged-on user.
if sasDefault <> "yes" then sasDefault = "no"
'*****************************************************************
'* Get SID from currently logged-on DomainUser
'*****************************************************************
'\
'script: "RemoteEditRegistry.vbs"
'created on 7 march 07 by Remco Simons [nl]
'
' This version 1.0
' (http://www.petri.co.il/forums/showthread.php?t=14109)
'The script can only be runned by a member of the 'Domain Admins' group!
'/
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
On Error Resume Next
strComputer = "WAMN-MIS-NIKE2"
who = "winmgmts:{impersonationLevel=impersonate}!//"& strComputer &""
Set Users = GetObject( who ).InstancesOf ("Win32_ComputerSystem")
for each User in Users
strUsr = right(User.UserName,len(User.UserName)-InStr(User.UserName,"\"))
strDomain = left(User.UserName,InStr(User.UserName,"\")-1)
exit For
next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colAccounts = objWMIService.ExecQuery _
("select * From Win32_UserAccount where name = '" & strUsr & "' AND domain = '" & strDomain & "'")
For each objAccount in colAccounts
strSID = objAccount.SID
exit For
Next
'You can now use the SID to edit the HKEY_USERS\{SID}-key of the logged-on Domain User...
'(you must end the main part of the script with: Loop [enter] objTSIn.Close )
'*****************************************************************
' -=Add and connect the network printer=-
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
'Parameters Key(4x) and values(5x)
strKeyPath1 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices"
ValueName1 = "\\" & strPrintServer & "\" & StrPrinterName
'***Count the already installed 'net'port printers for the next available number
Set oMethod = objReg.Methods_("EnumValues")
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_USERS
oInParam.sSubKeyName = strKeyPath1
Set oOutParam = objReg.ExecMethod_("EnumValues", oInParam)
xCount = 0
For i=0 To UBound(oOutParam.Properties_("sNames"))
sName = oOutParam.Properties_("sNames")(i)
objReg.GetStringValue HKEY_USERS,strKeyPath1,sName,dwValue
If Not InStr(1,dwValue,"winspool,LPT",0) = 0 Then
'LPT port
ELSE 'Ne.. ports
If sName = ValueName1 Then
strValue1 = dwValue
ELSE
xCount = xCount + 1
End If
End If
Next
sNe = "Ne"& 0&xCount &":"
If not strValue1 = dwValue then strValue1 = "winspool," & sNe
strKeyPath2 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
ValueName2 = ValueName1
strValue2 = strValue1 & ",15,45"
strKeyPath3 = strSID & "\Printers\Connections\,," & strPrintServer & "," & StrPrinterName
ValueName3a = "Server"
strValue3a = "\\" & strPrintServer
ValueName3b = "Provider"
strValue3b = "win32spl.dll"
strKeyPath4 = strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows"
ValueName4 = "Device"
strValue4 = ValueName1 & "," & strValue1
'Add the printer to the 'CURRENT'_USER's path
objReg.CreateKey HKEY_USERS, strKeyPath3
objReg.SetStringValue HKEY_USERS, strKeyPath2, ValueName2, strValue2
objReg.SetStringValue HKEY_USERS, strKeyPath1, ValueName1, strValue1
objReg.SetStringValue HKEY_USERS, strKeyPath3, ValueName3a, strValue3a
objReg.SetStringValue HKEY_USERS, strKeyPath3, ValueName3b, strValue3b
'Make it the default printer Y/N?
If LCase(sAsDefault) = "yes" Then
objReg.SetStringValue HKEY_USERS, strKeyPath4,ValueName4, strValue4
End If
On Error GoTo 0
wscript.echo "Done!"
wscript.quit
