Solved

Printer Script error vbs file

Posted on 2006-11-02
24
2,313 Views
Last Modified: 2008-03-10
We have ben using this this print script for years and it has always worked.   Now we are getting this error:

Script:  c:\winnt|sysvol\sysvol\mcs.k12.mi.us\scripts\production\mmm_student_map_prn.vbs
Line 15
Char: 2
Error:  This network connection does not exist.

Code 800708ca
Source   WSHNetwor.RemoveNetworkDrive

Here is the script:

' define the variables
dim WSHNetwork
dim printers
dim c1

' create the scripting object
Set WSHNetwork = WScript.CreateObject("WScript.Network")

' remove all printer connections before adding new ones
Set printers = WSHNetwork.EnumPrinterConnections()
For c1 = 0 To printers.Count - 1 Step 2
    If (Left(printers.Item(c1 + 1), 2) = "\\") then

      ' remove the printer connection
      WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
    End if
Next

' map all the printersWSHNetwork.AddWindowsPrinterConnection
WSHNetwork.AddWindowsPrinterConnection "\\srvms1\MS_MEDIA_4240"
WSHNetwork.AddWindowsPrinterConnection "\\srvms1\MS_RM_300_HP4050_PCL"

' set a default printer
select case left(lcase(WSHNetwork.ComputerName), 4)
case "mms1", "mms2"
      WSHNetwork.SetDefaultPrinter "\\srvms1\MS_MEDIA_4240"
case "mms3"
      WSHNetwork.SetDefaultPrinter "\\srvms1\MS_RM_300_HP4050_PCL"
case else

'      if (left(lcase(WSHNetwork.ComputerName), 10) = "mmsadmin01") then
'
'      else
'
'      end if
end select

' destroy the object in memory
Set WSHNetwork = nothing

' end script file
Wscript.quit

This is making us crazy.
0
Comment
Question by:manch03
  • 9
  • 8
  • 4
24 Comments
 
LVL 4

Expert Comment

by:Shankadude
ID: 17865332
Is this all the vbscript you have? The error seems to point to the removal of a network drive instead of a printer connection.
My guess would be that there is another script(part) that removes a network drive that already has been removed.
Try using the 'on error resume next'

on error resume next
For c1 = 0 To printers.Count - 1 Step 2
    If (Left(printers.Item(c1 + 1), 2) = "\\") then

     ' remove the printer connection
     WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
     If Err.Number <> 0 Then
         ' appearently the connection was already removed
         ' clear error or do something with the message
         err.clear
    End if
Next

On error go to 0 ' all errors are displayed again


0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17865531
Yeah I can tell you what the error is , look at what you typed here :

Code 800708ca
Source   WSHNetwor.RemoveNetworkDrive

It should be :

Code 800708ca
Source   WSHNetwork.RemoveNetworkDrive  '<-- You missed the k off at the end of WSHNetwork

Because when you created the WSHNetwork Object you assigned it / declared it as WSHNetwork not WSHNetwor

Try that and see if that helps :)
0
 

Author Comment

by:manch03
ID: 17865983
I see that the error was trying to remove a network drive, but I did not see anything in the script that was loading a network drive - that was confusing me.

Where should I insert this?   And do I insert  On error go to 0... line also?

0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17866053
Is that the whole script in its entirety ?
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17866068
Have you got server 2003 R2 ?? If not just use R2 for server 2003 and you can manage and deploy printers through the new PMC ( Print Management Console )
0
 

Author Comment

by:manch03
ID: 17866069
Yes - the first one I posted.
0
 

Author Comment

by:manch03
ID: 17866082
I copied and pasted the suggestion from Shankadude under this:

' define the variables
dim WSHNetwork
dim printers
dim c1

' create the scripting object
Set WSHNetwork = WScript.CreateObject("WScript.Network")

' remove all printer connections before adding new ones
Set printers = WSHNetwork.EnumPrinterConnections()
For c1 = 0 To printers.Count - 1 Step 2
    If ((Left(printers.Item(c1 + 1), 2) = "\\") and _
      (instr(1, lcase(printers.Item(c1 + 1)), "athletics02") < 1)) then

      ' remove the printer connection
WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
    End if
Next

and am now getting a compilation error

Line 32
Char 10

Error:  Syntax error
Code:   80003EA
Source:  Microsoft VBScript compilation error


Line 32 is this:   On error go to 0 ' all errors are displayed again
0
 
LVL 4

Expert Comment

by:Shankadude
ID: 17866086
The 'on error resume next' line makes that your script doesn't break on errors anymore, but gives you the ability to handle the errors inside your script.

The 'If Err.Number <> 0 then ' part goes in front of the command you're executing like connecting to a network drive.

The 'on error go to 0' line resets the error handling again. After this your script breaks again when it encounters an error. It's up to you if you want that to happen.

But I do think that somewhere in your scripts there's something that calls the RemoveNetworkDrive.  

If you want to be sure that you use the correct variables you declared you can start the script with:
Option Explicit

After this every variable that you use has to be declared with a dim or const command. This way you can easily spot typo's in your scripts like forgetting a 'k' in network etc.

Hope this helps.
0
 
LVL 4

Expert Comment

by:Shankadude
ID: 17866097
OOps, I made a type I see.. it has to be 'on error GoTo 0'.. loose the space between go and to.
0
 

Author Comment

by:manch03
ID: 17866173
For c1 = 0 To printers.Count - 1 Step 2
    If (Left(printers.Item(c1 + 1), 2) = "\\") then

Now it does  not like this   For statement

Error:

Line:    80
Char:    8
Error:   Invalid 'for' loop control variable
Code:    800A0410
Source:  Microsoft VBScript compilation error

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17866215
In the for loop why are you checking for the 2 back slashes ie \\ ??

Why not just do this :

For c1 = 0 To printers.Count - 1 Step 2
     ' remove the printer connection
     WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
    End if
Next

That way afterwards you just add the printer connections you want by using the select case statement as per your script :)
0
 

Author Comment

by:manch03
ID: 17866220
Line 80   is the "For c1 = 0
0
 

Author Comment

by:manch03
ID: 17866281
It still does not like this "For statement - same error

For c1 = 0 To printers.Count - 1 Step 2
     ' remove the printer connection
     WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
    End if
Next

 end select

'end script file
Wscript.quit

'destroy the object in memory
'Set WSHNetwork = nothing
   End    Select
'Set printers = nothing
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17866320
Instead of assigning and declaring c1 as the variable for the for loop why not just make it

dim i

For i = 0 To printers.Count - 1 Step 2
     ' remove the printer connection
     WSHNetwork.RemovePrinterConnection printers.Item(i), True, True
    End if
Next

I am not sure why you were adding 1 to the Item value so I just changed that to i but if it does not work then just put it to i + 1
0
 

Author Comment

by:manch03
ID: 17866371
Someone else set the scripts up and they have been working for several years and all of a sudden we were getting the errors.


But c1 is my variable and it works every place else in the script - this is the only place I am getting that error - do I just change it there and leave the others alone?
0
 
LVL 4

Expert Comment

by:Shankadude
ID: 17866380
Most wise thing to do is to use variables specific for what you're doing. In this case use the variable only for deleting the printers. That way you won't mixup and the type of the variable isn't changed to an object for example by accident by another command.
0
 

Author Comment

by:manch03
ID: 17867749
Now I am getting this error:

  Line:   86
Char:      5
Error:     Expected statement
Code:       800A0400
Source:   Microsoft VBScript compilation error

   End if   is line 86

0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17868479
Instead of the script you have try this one out ( I knocked this together some time ago whilst at work as we needed one ). Let me know if this works how you expected it to.

'--------------

Dim SleepTime

Dim MSRM
Dim MSMEDIA

MSRM = "MS_RM_300_HP4050_PCL"
MSMEDIA = "MS_MEDIA_4240"

Dim AName

SleepTime=15

Set WSHNetwork = CreateObject("WScript.Network")

strComputer = WSHNetwork.ComputerName

AName = Left(lcase(strComputer,Len(strComputer)-4))

Select Case AName

    Case "mms1", "mms2"

      call AddPrinter("\\srvms1\",MSMEDIA)

    Case "mms3"

      call AddPrinter("\\srvms1\",MSRM)

End Select

Public Function AddPrinter(ByRef Server,ByRef strPrinter)

Set WshNetwork = CreateObject("WScript.Network")

rem ** This will delete existing printers **

On Error Resume Next

Set oPrinters = WshNetwork.EnumPrinterConnections

        For i = 1 to oPrinters.Count - 1 Step 2

               WshNetwork.RemovePrinterConnection oPrinters.Item(i)

        Next

rem ** Setup new printer connection **

WshNetwork.AddWindowsPrinterConnection Server & strPrinter

rem ** Pause Script **

WScript.Sleep SleepTime*1000

rem ** Setup default printer **

WshNetwork.SetDefaultPrinter Server & strPrinter

End Function

'------------------

The strings that are assigned to MSRM and MSMEDIA should be the share names ( at least thats what they were when I used the above script so I am assuming thats what those are. If you have any issues please post back :)
0
 
LVL 23

Accepted Solution

by:
gecko_au2003 earned 500 total points
ID: 17868518
If your still having issues with the script then get this ( It is free as well )

http://www.wisesoft.co.uk/Products/ScriptBuilder/Default.aspx

Registration is free and you can register on the site.
0
 

Author Comment

by:manch03
ID: 17897600
Now my users are not getting any printers, so I will give this wisesoft a try and let you know how that works out.
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 18045090
I gave 2 reasonable solutions and have no feedback from the asker so I would either like feedback from the asker so if there are any issues with either the script builder or the script provided that we can ( myself and the asker ) attempt to rectify any issue(s) that the asker has with the provided possible solutions.

Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now