spastro
-
Total Posts
:
5
- Scores: 0
-
Reward points
:
0
- Joined: 1/12/2008
|
VBS Printer update script Help....
-
Saturday, January 12, 2008 3:24 PM
Hello All.... I dont know if i am posting this is the right section so if its not can a mod please move it to the appropriete area. Basically we have recently installed a new server at the business i work for. It will be used as the new printer server amongst other things. Currently all users have printer connections mapped to the old print server which is still in operation for now. I have found a vbs script online that when used as a log on script will automatically scan the users added printers and find the exact same printer installed on the new server, add the connection to the new server and then delete the old connection. All this works fine however we have a few locally installed printers and during testing of the script i found that when a user logs in to the domain with a locally installed printer the script will not find the printer installed on the new print server. It gives the error " error: -2147023095:" and then deletes the connection to the local printer. I am trying to modify the script slightly so that if the printer is not found on the new server then the current printer connection is left installed on the users printers and faxes connections folder. Can anyone please help? I have included the script below. Thank you -------------------------------------------- On Error Resume Next Function GetDefaultPrinter() sRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device" sDefault = "" On Error Resume Next sDefault = objShell.RegRead(sRegVal) sDefault = Left(sDefault ,InStr(sDefault, ",") - 1) On Error Goto 0 GetDefaultPrinter = sDefault End Function Set objNetwork = CreateObject ("Wscript.Network") Set objShell = CreateObject ("WScript.Shell") Set objFSO = CreateObject ("Scripting.FileSystemObject") LogonServer = objShell.ExpandEnvironmentStrings("%logonserver%") UserName = objShell.ExpandEnvironmentStrings("%username%") strComputer = "." PrintServer = "our printer server" PrintServer = LCase (PrintServer) Err.Clear Set objWMIService = GetObject("winmgmts:" & _ "{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2") If Err.Number Then wscript.echo ("Error : " & Err.Number & ": " & Err.Description & VbCrLf) Err.Clear Else ImpDefault = GetDefaultPrinter Set colInstalledPrinters = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Printer") For Each objPrinter in colInstalledPrinters PrinterArray = Split (objPrinter.Name , "\") If (LCase(objPrinter.ServerName) <> "") and (LCase(objPrinter.ServerName) <> "\\" & PrintServer) then objNetwork.AddWindowsPrinterConnection "\\" & PrintServer & "\" & PrinterArray(3) If Err.Number Then wscript.echo ("Error : " & Err.Number & ": " & Err.Description & VbCrLf) Err.Clear End If If ImpDefault = objPrinter.Name then objNetwork.SetDefaultPrinter ("\\" & PrintServer & "\" & PrinterArray(3)) End If objNetwork.RemovePrinterConnection objPrinter.Name End If Next End If
|
|
dm_4ever
-
Total Posts
:
3687
- Scores: 82
-
Reward points
:
0
- Joined: 6/29/2006
- Location: Orange County, California
|
RE: VBS Printer update script Help....
-
Saturday, January 12, 2008 4:51 PM
If you do not want to impact local printers change your query.... SELECT * FROM Win32_Printer Where Local = False
|
|
spastro
-
Total Posts
:
5
- Scores: 0
-
Reward points
:
0
- Joined: 1/12/2008
|
RE: VBS Printer update script Help....
-
Saturday, January 12, 2008 6:15 PM
hmmm.... Thanks for your suggestion. I gave it a try but it gave me the same error code and then removed the printer connection. One thing i just realised is that the local printer is also being shared on the network. The reason for this is that the users who have local printers (without network ports) on there desk log into the network from various locations and print to there printer. When i test this script i am just adding a locally installed shared printer from someone elses desk to my printer folder along with other network printers. Is there a way i can modify the script so that it only removes the printer connection if it finds it on the server or if a new connection is made? Im very very rusty with vbs scripts and am totally lost. Thanks again
<message edited by spastro on Saturday, January 12, 2008 6:17 PM>
|
|
dm_4ever
-
Total Posts
:
3687
- Scores: 82
-
Reward points
:
0
- Joined: 6/29/2006
- Location: Orange County, California
|
RE: VBS Printer update script Help....
-
Saturday, January 12, 2008 6:29 PM
Oh I see, when you said local I thought you meant local....as in printer directly connected to the workstation. I'll take a look tomorrow...now time for me to sleep.
|
|
spastro
-
Total Posts
:
5
- Scores: 0
-
Reward points
:
0
- Joined: 1/12/2008
|
RE: VBS Printer update script Help....
-
Saturday, January 12, 2008 7:36 PM
well currently there are about 15 network printers and about 5 local printers. The local printers are installed locally via usb on a few systems around the company however they are also shared over the network in case a user wants to print somthing directly to the desk of the user (Usually someone from managment) which has the locally installed printer. So i need the script to not remove those local printer connections if added under any account. Thats why i wanted to modify the script so that it only removes printers that it found on the new server, and all other printer that it did not find would stay. Thanks so much for your help.
|
|
dm_4ever
-
Total Posts
:
3687
- Scores: 82
-
Reward points
:
0
- Joined: 6/29/2006
- Location: Orange County, California
|
RE: VBS Printer update script Help....
-
Sunday, January 13, 2008 4:19 AM
This hasn't been fully tested...plus there is other examples in the "post a script" forum on this site....
Option Explicit
Dim strComputer : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & _
strComputer & "\root\cimv2")
Dim colPrinters : Set colPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer")
Dim strOldPrintServer : strOldPrintServer = "OLDPRINTSERVER"
Dim strNewPrintServer : strNewPrintServer = "NEWPRINTSERVER"
Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
Dim objPrinter, strOldPrinter, strNewPrinter
For Each objPrinter In colPrinters
strOldPrinter = UCase(objPrinter.DeviceID) ' this will be \\servername\printername
If InStr(strOldPrinter, UCase(strOldPrintServer)) Then 'check to see if old printer server name is in the printer path
strNewPrinter = Replace(strOldPrinter, UCase(strOldPrintServer), UCase(strNewPrintServer)) 'replace old printer name with new one
On Error Resume Next
objNetwork.AddWindowsPrinterConnection strNewPrinter ' attempt to add new printer connectiong
If Err.Number = 0 Then
If objPrinter.Default Then
objNetwork.SetDefaultPrinter strNewPrinter ' if old printer was the default and no errors, make the new one the default
End If
objNetwork.RemovePrinterConnection strOldPrinter ' if no errors; remove old printer connection
Else
WScript.Echo Err.Number & " - " & Err.Description
End If
On Error GoTo 0
End If
Next
|
|
spastro
-
Total Posts
:
5
- Scores: 0
-
Reward points
:
0
- Joined: 1/12/2008
|
RE: VBS Printer update script Help....
-
Sunday, January 13, 2008 9:49 AM
MATE.......... YOUR A FREEKEN LEGEND!!!!!!!!!! I have tested it once. and it worked a charm. Im going to go now and test it a few more times trying different things so that when it goes live it does not cause me grief because i forgot to test. THANK YOU SO MUCH
|
|