Add-Printer with Network Printer fails
I'm not 100% sure why you're seeing that error. I'm sure it'll be something to do with how it's handling passing through the string.
However, you shouldn't really need to run it through Invoke-Command
as Add-Printer
has two options within itself to add printers to remote computers.
You can specify single machine with the -CompuerName
parameter:
Add-Printer -ComputerName 'someServer' -ConnectionName '\\server\Printer'
You can also specify a (or multiple) CimSessions with the -CimSession
parameter, allowing you to hit a bunch of machines at once.
A caveat with this command to be aware of is that it only works on Server 2012/ Windows 8 and above (including the remote target).
I ran into a similar problem, but I was trying to loop through a list of shared printers from a print server then add all of them to the local machine. Jump to the bottom if you want to see my solution.
The following returns all of the shared printers as objects:
$printerList = Get-Printer -ComputerName PrintServer | where Shared -eq $true
The following should have looped through all my printers and added each one:
foreach ($printer in $printerList) { Add-Printer -ConnectionName "\\PrintServer\$printer.SharedName" }
Instead, I supposedly run into the same error as the OP:
Add-Printer : The specified server does not exist, or the server or printer name is invalid. Names may not contain ',' or '\' characters. At line:1 char:38 + ... nterList) { Add-Printer -ConnectionName "\PrintServer\$printer.SharedNam ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (MSFT_Printer:ROOT/StandardCimv2/MSFT_Printer) [Add-Printer], CimException + FullyQualifiedErrorId : HRESULT 0x80070709,Add-Printer
Needless to say, I'm very puzzled by those results because when I test what is going on, I see the following:
Write-Host $printer.ShareNameprinterName
Great! Why doesn't it work in double quotes though? I am intentionally using double quotes to return the contents of the $printer variable:
Write-Host "$printer.ShareName"
MSFT_Printer (Name = "printerName", ComputerName = "PrintServer", DeviceType = 0, Type = 0).ShareName
Rather than mess with a CimException likely due to typecasting or who knows what (and yes, the ShareName property is a simple string already, it's not a hash table or a nested funky datatype of sorts), I did a simple ToString()
as a workaround:
$printerList = Get-Printer -ComputerName PrintServer | where Shared -eq $trueforeach ($printer in $printerList) { $printerStr = $printer.ShareName.ToString() Add-Printer -ConnectionName "\\PrintServer\$printerStr" }
I ran into this issue as well ToString()
didn't resolve it for me. It was the "not required" parameter -ComputerName
. I was running remove-PrinterPort -Name $port
, which resulted in the following error:
The specified server does not exist, or the server or printer name is invalid. Names may not contain ',' or '\' characters.
After banging my head, and Googlin', I finally added the computer name as a test like this $env:computername
, I tried using a "." before, because I thought that meant local computer but, it didn't work, just stalled.
After adding the computer name, the ports, and drivers were removed without error.
get-help Remove-PrinterPort
shows that -Computer
name is not required but, it does seem to be required:
-ComputerName [<String>] Specifies the name of the computer from which to remove the printer port. Required? false Position? named Default value none Accept pipeline input? false Accept wildcard characters? false