Printer VBScript Error - "The filename, directory name or volume syntax is incorrect"

We are experiencing what seems to be a fairly common issue with printer vbscripts; however we have not found a solution.  The error we receive is "The filename, directory name or volume syntax is incorrect". This error does not occur on specific computers and is random.  For instance it may occur on Computer01 one day but Computer02 the next, both Computer01 and Computer02 have the exact same vbscript and image.  The vbscript may produce an error on Computer01, but at the same time work correctly on Computer02. We are running Windows XP Professional and are on a 2003 server environment for our print server.  All authenticated users have rights to see and add the printers from our print server. We have tried the following troubleshooting steps and / or solutions:

First we naturally thought there may be an issue with the vbscript so we tried different vbscripts and even a batch file.  What the second vbscript and the batch file proved is it can add one of the printers but not the other.  One day it would allow  Printer1to map fine but not Printer2, then the second day it was just the opposite.

Original Script
Option Explicit
Dim netPrinter, UNCpath
UNCpath = "\\server\Printer1" 
Set netPrinter = CreateObject("WScript.Network") 
netPrinter.AddWindowsPrinterConnection UNCpath

Dim objNetwork, strUNCPrinter
strUNCPrinter = "\\server\Printer2"
Set objNetwork = CreateObject("WScript.Network") 
objNetwork.AddWindowsPrinterConnection strUNCPrinter

' Here is where we set the default printer to strUNCPrinter
objNetwork.SetDefaultPrinter strUNCPrinter
WScript.Quit

Open in new window


Secondary Script
'On error keep going
ON ERROR RESUME NEXT

'Define the variables
Dim objShell, wshNet
Set wshNet = CreateObject("WScript.Network")
set objShell = WScript.CreateObject("WScript.Shell")

'Stop Printer Spooler
objShell.run "net stop Spooler",0,True

'Wait to 4 seconds continue the script
WScript.sleep 4000

'Start Printer Spooler
objShell.run "net start Spooler",0,True

'Wait to 10 seconds continue the script
WScript.sleep 10000

'Add the specified network printer
wshNet.AddWindowsPrinterConnection "\\Server\Printer1", "HP LaserJet 4350 PCL 6"
wshNet.AddWindowsPrinterConnection "\\Server\Printer2", "HP LaserJet 4350 PCL 6"
wshNet.SetDefaultPrinter "\\Server\Printer1"

Open in new window


Third Script (Batch file as a test)
Start \\Server\Printer1
Start \\Server\Printer2

Open in new window


Second we thought it may be an issue related with our network access controller and the printers being incorrectly moved to the wrong vlan.  However the scripts are not mapping directly to the printers they are mapping the shares from the print server.  On a PC that is having the error we can reach / ping both printers and the server using the host name and FQDN, therefore that told us it was not a networking issue.

Our third thought was a driver issue.  However we removed all of the drivers and ran the script and still got the same error.  The computers automatically pull the drivers from the print server; therefore our thought process is it was trying to replace the driver when the script ran.  For instance user01 logs in and then user02 comes and logs in and it would try to replace the current drive.  However it seems that the problem would be on every computer if this was the case.

Then our fourth thought was to restart the print spooler (which is why we added that code in the second script). This sometimes will fix the issue but not always.

Finally we have come to the conclusion that this only seems to happen on computers that have had a large number of users logging into it.  So we tried deleting all of the user profiles and seeing if the script would run then.  We could delete all but two user profiles on the system.  The two that we couldn’t delete had files under the user profile that we locked.  From here we are thinking that the script isn’t letting the printer map because another user has a thread running under a process, or a locked file that is still making use of the printer.  One solution we thought of was to delete the user profiles upon logout, however if files under it are locked then the user profile cannot be deleted.
LVL 1
trineitAsked:
Who is Participating?
 
RobSampsonCommented:
Perhaps even try creating the printer share on another server (or a workstation if need be) and see if you can map to that too.
0
 
RobSampsonCommented:
Hi, first off, try not specifying the printer driver name.  That is optional, and I never use it.

Secondly, try this code, which contains a loop, just to see whether it does eventually connect the printer, after trying 60 times.

Regards,

Rob.
On Error Resume Next
Set objNetwork = CreateObject("WScript.Network")
'Add the specified network printer
intAttempt = 0
intMaxAttempts = 60
Do
	intAttempt = intAttempt + 1
	Err.Clear
	objNetwork.AddWindowsPrinterConnection "\\Server\Printer1"
	If Err.Number = 0 Then
		Exit Do
	Else
		WScript.Sleep 1000
	End If
Loop While intAttempt <= intMaxAttempts
If Err.Number = 0 Then
	WScript.Echo "Printer successfully mapped. Attempt count: " & intAttempt
Else
	WScript.Echo "Failed to map printer. Attempt count: " & intAttempt & VbCrLf & "Error " & Err.Number & ": " & Err.Description
End If

Open in new window

0
 
trineitAuthor Commented:
Since we are trying to map two printers at once in the original scripts that I posted, I created two different VBScripts.  The first script was to add Printer1 and the second script was to add Printer2.  The first script ran fine and added the printer fine on attempt 1, however the second script for Printer2 errored out every loop. Please see image. This is the error the script produces.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
RobSampsonCommented:
OK, I think you may have some other issue with your machines.  Perhaps there's a driver issue, or a user profile issue.  Does the same thing happen on the same computer, but to a different user?  What happens if you remove all current printer connections, and try again?

What if you log in as an admin, go to Printers and Faxes, remove any printers, then go to Server Properties from there, and remove all drivers on the Drivers tab?  Reboot, and try the scripts again.

Also, to use the same script to add multiple printers, you can just add printers the array I have added here.

Regards,

Rob.
On Error Resume Next
arrPrinters = Array("\\Server\Printer1", "\\Server\Printer2")
Set objNetwork = CreateObject("WScript.Network")
For Each strPrinter In arrPrinters
	'Add the specified network printer
	intAttempt = 0
	intMaxAttempts = 60
	Do
		intAttempt = intAttempt + 1
		Err.Clear
		objNetwork.AddWindowsPrinterConnection strPrinter
		If Err.Number = 0 Then
			Exit Do
		Else
			WScript.Sleep 1000
		End If
	Loop While intAttempt <= intMaxAttempts
	If Err.Number = 0 Then
		WScript.Echo "Printer " & strPrinter & " successfully mapped. Attempt count: " & intAttempt
	Else
		WScript.Echo "Failed to map printer " & strPrinter & ". Attempt count: " & intAttempt & VbCrLf & "Error " & Err.Number & ": " & Err.Description
	End If
Next

Open in new window

0
 
trineitAuthor Commented:
Removing the driver has seemed to work on 2 of the PCs that had the issue.  However the drivers could not be removed until the print spooler was restarted.

The issue only occurs on computers that have had multiple users login.  We use DeepFreeze in our environment therefore all of the user profiles are cleared out every night.  I cannot replicate the issue until mid to late afternoon after multiple users have logged into the PC.

I did write a VBScript before posting the question on Experts Exchange that would restart the print spooler then attempt to remove all of the network printer connections from ever user profile, however the script could not successfully remove the printers from all user profiles. This makes me think that a user profile is locking something that is preventing either Printer1 or Printer2 to be added or removed.

Also it should be noted that the problem cannot be replicated once the computer is restarted, until multiple users (IE more than 4 users) login to the PC.

If it is the drivers causing the issue then would the best method be to write a script that does the following steps in order?

1

Restart the printer spooler

2

Remove drivers associated with printers to be added

3

Add Printer1 and Printer2
0
 
RobSampsonCommented:
Yes, that would seem to be the best workaround to the problem I think.  I think you're on the right track about something being locked preventing things from happening.

Have you tried just restarting the print spooler in your script first?  Maybe that would free the lock?

Rob.
On Error Resume Next
Set objShell = CreateObject("WScript.Shell")
objShell.Run "net stop spooler", 0, True
objShell.Run "net start spooler", 0, True
arrPrinters = Array("\\Server\Printer1", "\\Server\Printer2")
Set objNetwork = CreateObject("WScript.Network")
For Each strPrinter In arrPrinters
	'Add the specified network printer
	intAttempt = 0
	intMaxAttempts = 60
	Do
		intAttempt = intAttempt + 1
		Err.Clear
		objNetwork.AddWindowsPrinterConnection strPrinter
		If Err.Number = 0 Then
			Exit Do
		Else
			WScript.Sleep 1000
		End If
	Loop While intAttempt <= intMaxAttempts
	If Err.Number = 0 Then
		WScript.Echo "Printer " & strPrinter & " successfully mapped. Attempt count: " & intAttempt
	Else
		WScript.Echo "Failed to map printer " & strPrinter & ". Attempt count: " & intAttempt & VbCrLf & "Error " & Err.Number & ": " & Err.Description
	End If
Next

Open in new window

0
 
trineitAuthor Commented:
Yes we have tried that in previous VBScripts.  While restarting the print spooler will sometimes work it is not a dependable / reliable fix as it does not work 100% of the time.
0
 
RobSampsonCommented:
Hmmm, if the drivers *are* causing issues for some reason, try downloading the Fix It executable from here:
http://support.microsoft.com/gp/printing_problems

Which was linked to from here:
http://support.microsoft.com/kb/324757

And if you can run that EXE on an affected system, see if resolves the problem as well.

Regards,

Rob.
0
 
trineitAuthor Commented:
The Microsoft Fix it did not work.  Also I wrote the script below which restarts the print spooler, deletes network printers (off all profiles), removes the printer drivers, then adds the network printers.  However this script did not work either.  It will add Printer1 but not Printer2 (or in some cases the opposite).

This still seems like a user profile issue as the computers that have the issue always have at least 4 to 7 user profiles existing from user's that have logged in previously.  Since the computers are frozen with Deep Freeze the computers only have 2 user profiles in the morning.  Default User and Administrator.  The issue sometimes will not occur at all even when 15 people have logged into the machine, but when it does occur it is after multiple people have logged into the machine.

'On error keep going
ON ERROR RESUME NEXT

'Define the variables
Dim objShell, wshNet
Set wshNet = CreateObject("WScript.Network")
set objShell = WScript.CreateObject("WScript.Shell")

'Stop Printer Spooler
objShell.run "net stop Spooler",0,True

'Wait to 4 seconds continue the script
WScript.sleep 4000

'Start Printer Spooler
objShell.run "net start Spooler",0,True

'Wait to 4 seconds continue the script
WScript.sleep 4000

Function AddQuotes(strText) 
    AddQuotes = Chr(34) & strText & Chr(34) 
End Function

'Quietly delete a network printer - per machine
objShell.run "rundll32 printui.dll PrintUIEntry /q /gd /n " & AddQuotes("\\server\Printer1")
objShell.run "rundll32 printui.dll PrintUIEntry /q /gd /n " & AddQuotes("\\server\Printer2")

'Quietly delete a specific printer driver
objShell.run "rundll32 printui.dll,PrintUIEntry /q /dd /m " & AddQuotes("Driver")

'Add the specified network printer
wshNet.AddWindowsPrinterConnection "\\server\Printer1"
wshNet.AddWindowsPrinterConnection "\\server\Printer2"
wshNet.SetDefaultPrinter "\\server\Printer2"

Open in new window

0
 
RobSampsonCommented:
It's odd....does your printer's share name have any spaces in it?  Could you create another printer object on the server (to the same printer IP is fine), but make the share name one without spaces, and try to map to that?

Rob.
0
 
RobSampsonCommented:
Can you check the security applied to this registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Providers

Users should have Read access, and SYSTEM should have Full Control.

Rob.
0
 
trineitAuthor Commented:
There are no spaces in the printer share names. Permissions are set correctly for HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Providers .  We will take a shot at the printer share on another server.
0
 
RobSampsonCommented:
I've had a case of one particular printer that just wouldn't work on a server, no matter what we did, so we had to put that one printer on another server....no idea why, but it was the only resolution for us....
0
 
trineitAuthor Commented:
It seems so far that we cannot replicate the issue when the script is pointing to a different server.  We are trying the same script now on the original server but have created new printer shares to see if we can replicate the issue.  At this point it's looking like we either have it narrowed down to a server configuration problem, or a printer share problem.  We will post results as soon as possible.
0
 
trineitAuthor Commented:
Turns out that simply recreating the entire printer share fixed the issue.  Thanks for all your help.
0
 
RobSampsonCommented:
Thanks for clarifying that.  It's strange that that's all it took, but I'm glad it was a simple fix.

Regards,

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.