Intermittent ‘Invalid Value’ Error When Adding Printers in PowerShell Script

0
0
Asked By CuriousCoder42 On

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!

2 Answers

Answered By TechSavant88 On

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.

Answered By ScriptNinja99 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.