manch03
asked on
Printer Script error vbs file
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\p roduction\ mmm_studen t_map_prn. vbs
Line 15
Char: 2
Error: This network connection does not exist.
Code 800708ca
Source WSHNetwor.RemoveNetworkDri ve
Here is the script:
' define the variables
dim WSHNetwork
dim printers
dim c1
' create the scripting object
Set WSHNetwork = WScript.CreateObject("WScr ipt.Networ k")
' remove all printer connections before adding new ones
Set printers = WSHNetwork.EnumPrinterConn ections()
For c1 = 0 To printers.Count - 1 Step 2
If (Left(printers.Item(c1 + 1), 2) = "\\") then
' remove the printer connection
WSHNetwork.RemovePrinterCo nnection printers.Item(c1 + 1), True, True
End if
Next
' map all the printersWSHNetwork.AddWind owsPrinter Connection
WSHNetwork.AddWindowsPrint erConnecti on "\\srvms1\MS_MEDIA_4240"
WSHNetwork.AddWindowsPrint erConnecti on "\\srvms1\MS_RM_300_HP4050 _PCL"
' set a default printer
select case left(lcase(WSHNetwork.Comp uterName), 4)
case "mms1", "mms2"
WSHNetwork.SetDefaultPrint er "\\srvms1\MS_MEDIA_4240"
case "mms3"
WSHNetwork.SetDefaultPrint er "\\srvms1\MS_RM_300_HP4050 _PCL"
case else
' if (left(lcase(WSHNetwork.Com puterName) , 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.
Script: c:\winnt|sysvol\sysvol\mcs
Line 15
Char: 2
Error: This network connection does not exist.
Code 800708ca
Source WSHNetwor.RemoveNetworkDri
Here is the script:
' define the variables
dim WSHNetwork
dim printers
dim c1
' create the scripting object
Set WSHNetwork = WScript.CreateObject("WScr
' remove all printer connections before adding new ones
Set printers = WSHNetwork.EnumPrinterConn
For c1 = 0 To printers.Count - 1 Step 2
If (Left(printers.Item(c1 + 1), 2) = "\\") then
' remove the printer connection
WSHNetwork.RemovePrinterCo
End if
Next
' map all the printersWSHNetwork.AddWind
WSHNetwork.AddWindowsPrint
WSHNetwork.AddWindowsPrint
' set a default printer
select case left(lcase(WSHNetwork.Comp
case "mms1", "mms2"
WSHNetwork.SetDefaultPrint
case "mms3"
WSHNetwork.SetDefaultPrint
case else
' if (left(lcase(WSHNetwork.Com
'
' else
'
' end if
end select
' destroy the object in memory
Set WSHNetwork = nothing
' end script file
Wscript.quit
This is making us crazy.
Yeah I can tell you what the error is , look at what you typed here :
Code 800708ca
Source WSHNetwor.RemoveNetworkDri ve
It should be :
Code 800708ca
Source WSHNetwork.RemoveNetworkDr ive '<-- 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 :)
Code 800708ca
Source WSHNetwor.RemoveNetworkDri
It should be :
Code 800708ca
Source WSHNetwork.RemoveNetworkDr
Because when you created the WSHNetwork Object you assigned it / declared it as WSHNetwork not WSHNetwor
Try that and see if that helps :)
ASKER
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?
Where should I insert this? And do I insert On error go to 0... line also?
Is that the whole script in its entirety ?
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 )
ASKER
Yes - the first one I posted.
ASKER
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("WScr ipt.Networ k")
' remove all printer connections before adding new ones
Set printers = WSHNetwork.EnumPrinterConn ections()
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.RemovePrinterCo nnection 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
' define the variables
dim WSHNetwork
dim printers
dim c1
' create the scripting object
Set WSHNetwork = WScript.CreateObject("WScr
' remove all printer connections before adding new ones
Set printers = WSHNetwork.EnumPrinterConn
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.RemovePrinterCo
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
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.
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.
OOps, I made a type I see.. it has to be 'on error GoTo 0'.. loose the space between go and to.
ASKER
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
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
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.RemovePrinterCo nnection 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 :)
Why not just do this :
For c1 = 0 To printers.Count - 1 Step 2
' remove the printer connection
WSHNetwork.RemovePrinterCo
End if
Next
That way afterwards you just add the printer connections you want by using the select case statement as per your script :)
ASKER
Line 80 is the "For c1 = 0
ASKER
It still does not like this "For statement - same error
For c1 = 0 To printers.Count - 1 Step 2
' remove the printer connection
WSHNetwork.RemovePrinterCo nnection 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
For c1 = 0 To printers.Count - 1 Step 2
' remove the printer connection
WSHNetwork.RemovePrinterCo
End if
Next
end select
'end script file
Wscript.quit
'destroy the object in memory
'Set WSHNetwork = nothing
End Select
'Set printers = nothing
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.RemovePrinterCo nnection 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
dim i
For i = 0 To printers.Count - 1 Step 2
' remove the printer connection
WSHNetwork.RemovePrinterCo
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
ASKER
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?
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?
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.
ASKER
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
Line: 86
Char: 5
Error: Expected statement
Code: 800A0400
Source: Microsoft VBScript compilation error
End if is line 86
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.Netw ork")
strComputer = WSHNetwork.ComputerName
AName = Left(lcase(strComputer,Len (strComput er)-4))
Select Case AName
Case "mms1", "mms2"
call AddPrinter("\\srvms1\",MSM EDIA)
Case "mms3"
call AddPrinter("\\srvms1\",MSR M)
End Select
Public Function AddPrinter(ByRef Server,ByRef strPrinter)
Set WshNetwork = CreateObject("WScript.Netw ork")
rem ** This will delete existing printers **
On Error Resume Next
Set oPrinters = WshNetwork.EnumPrinterConn ections
For i = 1 to oPrinters.Count - 1 Step 2
WshNetwork.RemovePrinterCo nnection oPrinters.Item(i)
Next
rem ** Setup new printer connection **
WshNetwork.AddWindowsPrint erConnecti on Server & strPrinter
rem ** Pause Script **
WScript.Sleep SleepTime*1000
rem ** Setup default printer **
WshNetwork.SetDefaultPrint er 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 :)
'--------------
Dim SleepTime
Dim MSRM
Dim MSMEDIA
MSRM = "MS_RM_300_HP4050_PCL"
MSMEDIA = "MS_MEDIA_4240"
Dim AName
SleepTime=15
Set WSHNetwork = CreateObject("WScript.Netw
strComputer = WSHNetwork.ComputerName
AName = Left(lcase(strComputer,Len
Select Case AName
Case "mms1", "mms2"
call AddPrinter("\\srvms1\",MSM
Case "mms3"
call AddPrinter("\\srvms1\",MSR
End Select
Public Function AddPrinter(ByRef Server,ByRef strPrinter)
Set WshNetwork = CreateObject("WScript.Netw
rem ** This will delete existing printers **
On Error Resume Next
Set oPrinters = WshNetwork.EnumPrinterConn
For i = 1 to oPrinters.Count - 1 Step 2
WshNetwork.RemovePrinterCo
Next
rem ** Setup new printer connection **
WshNetwork.AddWindowsPrint
rem ** Pause Script **
WScript.Sleep SleepTime*1000
rem ** Setup default printer **
WshNetwork.SetDefaultPrint
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 :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now my users are not getting any printers, so I will give this wisesoft a try and let you know how that works out.
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
Thanks
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.RemovePrinterCo
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