Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Removing Network Printers - Two Quick Questions Please

Reply
 
Old 03-05-2009   #1 (permalink)
Cary Shultz


 
 

Removing Network Printers - Two Quick Questions Please

Good morning!

I have the following VBS script that is working flawlessly in 3.5 of the
four parts.


Here is the short version to my two questions. Please find below the longer
version of each question along with the logon scrtpt itself.

Q1: when I remove a printer via the logon script it works - the first
time - just fine. However, once it is removed everytime the users logon
they receive a "The network connection does not exist" - which is expected.
How do I build in logic to first check to see if it exists and if it does,
remove it...if it does not move on?

Q2: possible to remove all printers that are homed on one server? So, all
printers that start out with UNC \\server1\...... would be removed? Is that
a smart thing to do? Seems like it would be.

Please find below the logon script the long questions! First, though,
please find the script below:

-------------------------------------------------------------------------------------
Option Explicit
Dim objFSO, objNet, objShell, objReg
Dim strPrinter1, strPrinter2, strPrinter3, strPrinter4
Dim bForce, bUpdateProfile
const HKCU = &H80000001

'On Error Resume Next

Set objNet = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReg =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''Remove all Drive
Mappings''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

If objFSO.DriveExists("O:")=True Then
objNet.RemoveNetworkDrive "O:",True,True
objReg.DeleteKey HKCU, "Network\" & Left("O:", 1)
End If

If objFSO.DriveExists("P:")=True Then
objNet.RemoveNetworkDrive "P:",True,True
objReg.DeleteKey HKCU, "Network\" & Left("P:", 1)
End If

If objFSO.DriveExists("Q:")=True Then
objNet.RemoveNetworkDrive "Q:",True,True
objReg.DeleteKey HKCU, "Network\" & Left("Q:", 1)
End If


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''Add Drive
Mappings''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

objNet.MapNetworkDrive "M:", "\\server\share1"
objNet.MapNetworkDrive "N:", "\\server\share2"
objNet.MapNetworkDrive "O:", "\\server\share3"


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''Remove Old
Printers'''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''Add New
Printers''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

strPrinter1 = "\\newserver\HP LaserJet 4050 Series PCL 5e"
strPrinter2 = "\\newserver\HP Color LaserJet 4500 PCL 5c"
strPrinter3 = "\\oldserver\Kyocera 3050"
strPrinter4 = "\\oldserver\HP LaserJet 4250 PCL 5e"
objNet.RemovePrinterConnection strPrinter3, True, True
objNet.RemovePrinterConnection strPrinter4, True, True
objNet.AddWindowsPrinterConnection strPrinter1
objNet.AddWindowsPrinterConnection strPrinter2
objNet.SetDefaultPrinter strPrinter1



Set objShell = Nothing
Set objNet = Nothing
Set objFSO = Nothing
Set objReg = Nothing

------------------------------------------------------------------------------------------


The part that is giving me problems is the removal of network printers.
Well, actually, that part is not giving me problems I can do that. The
problem is that once the printers are actually removed (the first time the
user logs on to the machine and the logon script is invoked) every
subsequent logon results in an error (something to the effect that "this
network connection does not exist"!). Now, that happens - as expected -
when I have commented out the On Error Resume Next line. If I 'put it back'
(so to speak) then there are no errors and everything is fine and dandy.

What I would like to do is to build in some logic - like I have for the
drive mappings - for the removal of the printers. Something to the effect
of "If this printer exists, remove it. If not, move on". I am good enough
with VBScript to write some basic things and to find scripts (Thank you,
Richard!) and modify those to fit the various envioronments that we manage.
I just can not quite find the answer to this question! I would like to NOT
rely on 'On Error Resume Next'........

Second question - is there a way to simply remove all printers that are
shared from "OldServer"? My colleagues have installed a new server at one
of our clients and the server that it is replacing is currently the Print
Server. Instead of removing each of the five printers that is shared off of
"oldserver" individually (read: there would be five entries) I would like
to know if it is possible to "simply" remove - in one fell swoop - all of
the printers that start out \\oldserver\. I am sure that this can be done.
I am just not smart enough to figure it out!

Yes, another novel from me!!!!!!!!!! Sorry everyone. I can not help it!


My System SpecsSystem Spec
Old 03-05-2009   #2 (permalink)
T Lavedas


 
 

Re: Removing Network Printers - Two Quick Questions Please

On Mar 5, 6:12*am, "Cary Shultz" <cshu...@xxxxxx> wrote:
Quote:

> Good morning!
>
> I have the following VBS script that is working flawlessly in 3.5 of the
> four parts.
>
> Here is the short version to my two questions. *Please find below the longer
> version of each question along with the logon scrtpt itself.
>
> Q1: *when I remove a printer via the logon script it works - the first
> time - just fine. *However, once it is removed everytime the users logon
> they receive a "The network connection does not exist" - which is expected.
> How do I build in logic to first check to see if it exists and if it does,
> remove it...if it does not move on?
>
> Q2: *possible to remove all printers that are homed on one server? *So, all
> printers that start out with UNC \\server1\...... would be removed? *Isthat
> a smart thing to do? *Seems like it would be.
>
> Please find below the logon script the long questions! *First, though,
> please find the script below:
>
> -------------------------------------------------------------------------------------
> Option Explicit
> Dim objFSO, objNet, objShell, objReg
{snip}
Quote:

> ------------------------------------------------------------------------------------------
>
> The part that is giving me problems is the removal of network printers.
> Well, actually, that part is not giving me problems *I can do that. *The
> problem is that once the printers are actually removed (the first time the
> user logs on to the machine and the logon script is invoked) every
> subsequent logon results in an error (something to the effect that "this
> network connection does not exist"!). *Now, that happens - as expected -
> when I have commented out the On Error Resume Next line. *If I 'put it back'
> (so to speak) then there are no errors and everything is fine and dandy.
>
> What I would like to do is to build in some logic - like I have for the
> drive mappings - for the removal of the printers. *Something to the effect
> of "If this printer exists, remove it. If not, move on". * I am good enough
> with VBScript to write some basic things and to find scripts (Thank you,
> Richard!) and modify those to fit the various envioronments that we manage.
> I just can not quite find the answer to this question! *I would like toNOT
> rely on 'On Error Resume Next'........
>
> Second question - is there a way to simply remove all printers that are
> shared from "OldServer"? *My colleagues have installed a new server at one
> of our clients and the server that it is replacing is currently the Print
> Server. *Instead of removing each of the five printers that is shared off of
> "oldserver" individually (read: *there would be five entries) I would like
> to know if it is possible to "simply" remove - in one fell swoop - all of
> the printers that start out \\oldserver\. *I am sure that this can be done.
> I am just not smart enough to figure it out!
>
> Yes, another novel from me!!!!!!!!!! *Sorry everyone. *I can not helpit!
Try something like ...

'...
sServer = "\\oldserver"
with CreateObject("WScript.Network").EnumPrinterConnections
For i = 0 to .Count - 1 Step 2
if instr(lcase(.Item(i+1)), sServer) <> 0 then
objNet.RemovePrinterConnection .Item(i+1), true, true
end if
Next
end with
'...

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Removing network printers Vista General
Re: Removing network printers Vista print fax & scan
Removing Quick Launch buttons Vista hardware & devices
USB Printer Ports still there after removing printers Vista print fax & scan
Removing Printers PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46