通过Powershell将共享打印机添加到远程计算机



我无法运行调用命令脚本在远程计算机上安装打印机。 我的代码在本地工作,但是一旦我将其通过管道传输到 Invoke-command,我就会收到错误。

当地:

$Printer = "\server1printer1"
(New-Object -Com Wscript.Network).AddWindowsPrinterConnection($Printer)

这很好地增加了打印机。 我可以在远程计算机上执行相同的命令,没有任何问题。 但是当我尝试远程执行命令时,我遇到了问题。

远程:

$compname = "computer"
$Printer = "\server1printer1"
Invoke-Command -ComputerName $CompName -Scriptblock {(New-Object -Com Wscript.Network).AddWindowsPrinterConnection('$Printer')}

返回错误"打印机名称无效"

所以我尝试使用以下代码查看 shell 发送到远程计算机的内容,写入输出中的所有内容看起来都不错,但我仍然收到错误:

Invoke-Command -ComputerName $CompName -Scriptblock {(New-Object -Com Wscript.Network).AddWindowsPrinterConnection('$Printer'); write-host "(New-Object -Com Wscript.Network).AddWindowsPrinterConnection('$Printer')"}

输出:

Exception calling "AddWindowsPrinterConnection" with "1" argument(s): "The printer name is invalid. (Exception from
HRESULT: 0x80070709)"
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
    + PSComputerName        : computer
(New-Object -Com Wscript.Network).AddWindowsPrinterConnection('\server1printer1')

编辑 1/5/2015

所以我尝试了保罗的代码,在参数列表中有许多不同的条目。 到目前为止,所有这些都没有奏效。 我认为前 3 个更接近答案。

-ArgumentList ""\server1printer1""
-ArgumentList ""'\server1printer1'""
-ArgumentList ""\server1printer1""

结果:

Invoke-Command : A positional parameter cannot be found that accepts argument '\server1printer1'.
At line:1 char:1
+ Invoke-Command -ComputerName $CompName -Scriptblock {(New-Object -Com Wscript.Ne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

-ArgumentList "'\server1printer1'"
-ArgumentList '"\server1printer1"'
-ArgumentList ""\server1printer1""
-ArgumentList "\server1printer1"

导致:

Exception calling "AddWindowsPrinterConnection" with "1" argument(s): "The printer name is invalid. (Exception from
HRESULT: 0x80070709)"
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
    + PSComputerName        : sso-mxl327082y

试试这个:

Invoke-Command -ComputerName $CompName -Scriptblock {(New-Object -Com Wscript.Network).AddWindowsPrinterConnection($args[0]); write-host "(New-Object -Com Wscript.Network).AddWindowsPrinterConnection($($args[0]))"} -ArgumentList "\server1printer1"

我认为这是因为您的$printer变量放在单引号之间,单引号之间的变量不会被 powershell 解释。因此,您的函数可能获得的打印机名称是"$printer"。

如果您想知道它在write-host语句中正确打印出来,因为这里的单引号位于字符串内。

你需要

使用 $Using:yourvar 将变量传递给脚本块

$compname = "computer"
$Printer = "\server1printer1"
Invoke-Command -ComputerName $CompName -Scriptblock 
{
(New-Object -Com Wscript.Network).AddWindowsPrinterConnection($Using:$Printer)
}
我认为

这是因为所谓的"双跳问题",即您的身份验证不会传输到共享打印机的下一台远程计算机。我尝试使用添加打印机解决类似的问题,并按照本文双跳问题解决方案。

但是,尽管它适用于 get-childitem 等,但它不适用于添加打印机 cmdlet。

最新更新