Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have gone through many blogs for finding solution for this issue, but never find a solution.

$CompName = "test.domain.com"
$Printer = "\\122.21.10.11\PRINTER-NAME-1"
Invoke-Command -ComputerName $CompName -Scriptblock {
  Param($p)
  (New-Object -Com Wscript.Network).AddWindowsPrinterConnection($p)
} -ArgumentList $Printer

I have even tried PSRemoting and tried to execute the ps1 file from remote machine. the same script is working in local and not working remote.

Enter-PSSession -ComputerName "testserver.testdomain.com" -Authentication Credssp -Credential Get-Credential

Error:

Exception calling "AddWindowsPrinterConnection" with "1" argument(s): "The printer name is invalid. (Exception from HRESULT: 0x80070709)"

Updates:

Working without CredSSP:

Invoke-Command -ComputerName remotemac.mydomain.com -ScriptBlock {
  Get-ChildItem C:\Users\myusername\Desktop

Working with CredSSP:

Invoke-Command -ComputerName remotemac.mydomain.com -ScriptBlock {
  Get-ChildItem \\sharedmac\sharef
} -Authentication Credssp -Credential mydomain\myusername

Script is working, but not showing network printers:

Invoke-Command -ComputerName remotemac.mydomain.com -ScriptBlock {
  (New-Object -ComObject WScript.Network).EnumPrinterConnections()
} -Authentication Credssp -Credential mydomain\myusername

Script gives error: Invalid Printer name

$Printer = "\\172.26.30.13\SDB1-FLOOR1-B2" # I have tried this input in single, double, with and without escape character
Invoke-Command -ComputerName remotemac.mydomain.com -ScriptBlock {
  Param($p)
  (New-Object -Com WScript.Network).AddWindowsPrinterConnection("$p")
} -ArgumentList $Printer -Authentication Credssp -Credential mydomain\myusername

I'm totally stuck.

I heard we can use Group Policy, is there anyway we can automate this using group policy?

"remotemac"? Are you trying to run this on a Mac? Did you configure CredSSP on server and client as described in the article I linked to? What does your network topology actually look like? – Ansgar Wiechers Aug 25, 2016 at 9:52 not mac OS machine, its just a dummy name. Yes, CredSSP configured properly. it works fine for other scripts, i have verified it. both machines are in the same domain. ports are opened. nothing is blocking. should we have to implement remoting with the Printer Server (where printers are available. )$Printer = "\\172.26.30.13\SDB1-FLOOR1-B2" this server: 172.26.30.13 – Rahul Nair Aug 25, 2016 at 10:07 google://group+policy+add+printer yields a metric ton of results. Please do some legwork yourself. – Ansgar Wiechers Aug 26, 2016 at 10:44

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.