Image

Image

Search This Blog

Tuesday, January 18, 2011

Install Windows printers

' replacepr.vbs - Windows NT Logon Script. (c) 2008-2010 sorin@xxxxxxx Inc.
' VBScript - Silently remove ALL network printers, install new ones with default
' this script require 2 additional files: one defining the printers we need to
' install, one printer per line and a second file defining what default printer
' we use, depending on what IP range this computer belongs the format of this
' file is IP_Range, Default_Printer (IP_Range is defined as, ex: 10.0.IP_Range.9)
' -----------------------------------------------------------------------'

Dim printer, sServer, i, l, default
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNet = CreateObject("WScript.Network")
Set WshShell = CreateObject("WScript.Shell")
Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
Set wmiNameSpace = wmiLocator.ConnectServer(objNet.ComputerName, "root\default")
Set objRegistry = wmiNameSpace.Get("StdRegProv")
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
strComputer = "."
'Define the server where printers reside
sServer = "\\sqlsrv"
'If the OS is win7 we need to install printers from a win 2008 server
Set objWMIService = GetObject("winmgmts:" &_
"{impersonationLevel=impersonate}!\\" & strComputer & _
"\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
if objOperatingSystem.Caption = "Microsoft Windows 7 " then sServer = "\\2k8srv"
'wscript.echo sServer
Next

' If this script alredy run once for this user, then EXIT
userprrf = WshShell.Environment("PROCESS")("UserProfile")
'wscript.Echo userprrf
If (objFSO.FileExists(userprrf & "\sctwashere.txt")) Then
Wscript.Quit
Else
blah = "let's have some fun"
End If

' Deletes RegistryKey with all subkeys in Network printers
sPath = "Printers\Connections"
lRC = DeleteRegEntry(HKEY_CURRENT_USER, sPath)
Function DeleteRegEntry(sHive, sEnumPath)
' Attempt to delete key. If it fails, start the subkey enumration process.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
' The deletion failed, start deleting subkeys.
If (lRC <> 0) Then
' Subkey Enumerator
On Error Resume Next
lRC = objRegistry.EnumKey(HKEY_CURRENT_USER, sEnumPath, sNames)
For Each sKeyName In sNames
If Err.Number <> 0 Then Exit For
lRC = DeleteRegEntry(sHive, sEnumPath & "\" & sKeyName)
Next
On Error Goto 0
' At this point we should have looped through all subkeys, trying to delete the key again.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
End If
End Function
'Now let's recreate the "root" Key we deleted before
objRegistry.CreateKey HKEY_CURRENT_USER,sPath

'Tell something to the user
'with createobject("wscript.shell")
' .popup "All Printers are now erased, Reinstalling...",5, "Printers Manager"
'end with

'Let's see in what ip range we are, so we can choose a default printer
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select IPAddress from Win32_NetworkAdapterConfiguration ")

For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) _
to UBound(IPConfig.IPAddress)
strArray=Split(IPConfig.IPAddress(i),".")
if ubound(strArray) > 2 Then
'WScript.Echo strArray(2)
if strArray(0) = 192 And strArray(1) = 168 Then
Exit For
End if
End If
Next
End If
Next
' Read the file containig the pair ip-range, default-printer
'(the ip range is the 3rd digit in the ip - 10.0.X.3 - gives X as range)
Set objFile = objFSO.OpenTextFile("defaultlist.txt", ForReading)
'WScript.Echo strArray(2)
Do Until objFile.AtEndOfStream
defaultArray = split(objFile.ReadLine,",")
if defaultArray(0) = strArray(2) Then
defaultprt=defaultArray(1)
'wscript.Echo defaultArray(1)
exit do
end if
Loop
objFile.Close

default = sServer & "\" & defaultArray(1)
'wscript.Echo default

'Now let's start installing the printers listed in plist.txt
Set objFile = objFSO.OpenTextFile("plist.txt", ForReading)

Do Until objFile.AtEndOfStream
printer = objFile.ReadLine
printer = sServer & "\" & printer
if printer = default then
strCmd = "rundll32 printui.dll,PrintUIEntry /in /n """ & printer & """ /u /q /Gw"
WshShell.Run strCmd,,true
strCmd = "rundll32 printui.dll,PrintUIEntry /y /n """ & printer & ""
else
strCmd = "rundll32 printui.dll,PrintUIEntry /in /n """ & printer & """ /u /q /Gw"
end if
WshShell.Run strCmd
Loop
objFile.Close


with createobject("wscript.shell")
.popup "All Printers Are Now ReInstalled",15, "Printers Management"
end with

'We're done, let's leave a trace in userprofile, so at next login this script will exit
Set objFile1 = objFSO.CreateTextFile(userprrf & "\sctwashere.txt")

Wscript.Quit

No comments:

Post a Comment

Blog Archive