powershell - 使用网络打印机添加打印机失败

标签 powershell

当我在本地 PowerShell 环境中使用 Add-Printer -ConnectionName '\\server\Printer' 时,它成功了。

当我使用相同的命令,但像这样包装在 Invoke-Command 中时:

Invoke-Command -ComputerName 'someServer' -Credential $creds -Authentication CredSSP -ScriptBlock { Add-Printer -ConnectionName '\\server\Printer' }

它没有说

'\' is an invalid character.

我猜它与本地和远程可用的不同级别的用户配置文件有关,但我不确定如何解决它。

编辑:为了更新这个,我不得不放弃这种方法,因为我无法让它工作。我的解决方法是使用

RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /c\\computerName /n\\someServer\Printer /q

虽然它的作用与 Add-Printer cmdlet 略有不同,但它有效。

最佳答案

我不是 100% 确定您为什么会看到该错误。我敢肯定,这与它处理字符串传递的方式有关。

但是,您真的不需要通过 Invoke-Command 运行它,因为 Add-Printer 本身有两个选项可以将打印机添加到远程计算机。

可以通过-CompuerName参数指定单机:

Add-Printer -ComputerName 'someServer' -ConnectionName '\\server\Printer'

您还可以使用 -CimSession 参数指定一个(或多个)CimSession,让您可以同时攻击一堆机器。

需要注意的是,此命令仅适用于 Server 2012/Windows 8 及更高版本(包括远程目标)。

关于powershell - 使用网络打印机添加打印机失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41476828/

相关文章:

powershell - Get-AzureDomain、GetAzureADUser 等返回 Authentication_Unauthorized、未找到用户

Powershell 将空字符串视为相当于 switch 语句中的 null 而不是 if 语句

powershell - Powershell保持RemoteDrive session 打开

regex - 如何获取多个正则表达式匹配捕获组

Powershell INI 编辑

azure - 需要 powershell 脚本来启用存储帐户和 key 保管库的诊断日志记录

c# - 在 TFS 构建后通过远程桌面连接自动执行 IISRESET

powershell - 使用 catch 在 PowerShell 中选择前 x 个字符

powershell - 在Powershell中打印参数

powershell - 调用表达式 : Positional parameter cannot be found that accepts argument/s