Intermittent 'Invalid Value' Error When Adding Printers in PowerShell Script
Asked by
CuriousCoder42
I'm running a PowerShell script to add printers, using a FQDN like 'server' and a printer name 'printerName'. I join these into a printer path and then call the Add-Printer function. The weird part? Sometimes it works perfectly, but about 40% of the time I get the error: "One or more specified parameters for this operation has an invalid value." It's baffling because all my variables seem correct when I print them out, yet the error hits randomly. I even delayed the execution by 30 seconds after user login, but it doesn't help. The script seems to run fine when I trigger it manually. I've put it inside a do-while loop, but it doesn't repeat when the error occurs, which adds to the confusion. I'm really hoping someone can shed some light on this odd issue!
Answers
Sharing your code often helps diagnose issues faster. It's great you added your code in an edit! One thing to watch for is if your printer names have leading backslashes that could be messing up the Join-Path command.
If you consistently get errors at startup but not when you run it manually, it might also be a timing issue. Maybe the network isnโt fully ready to add the printer on startup? Try adding a brief pause or check if the network is accessible before the add command.
Reply to this answer
It sounds like there might be an issue with how the Join-Path functions when you're using leading backslashes. This could be causing your printer names not to resolve correctly. Instead, consider directly concatenating the server name and printer name without Join-Path. Just use something like this:
```
$path = "\$serverName$printerName"
```
Also, if youโre running into a terminating error, wrapping the Add-Printer command in a Try-Catch block may help catch the error without stopping the script entirely.
Reply to this answer
You mentioned that the issue seems sporadic, which usually indicates a problem with either data being inconsistent or network issues. The fact that adding a delay didnโt help might suggest that itโs not just a timing issue. Double-check the server's availability at the time the script runs. Lastly, are you explicitly setting parameter names like -Path and -ChildPath in your Join-Path? It could help clarify for PowerShell what youโre trying to do.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions