Windows 通过Get-WmiObject设置默认打印机时出错:调用SetDefaultPrinter时异常:不支持 概述
我正在尝试使用PowerShell在Windows 2012 R2中设置默认打印机。每当我在本地运行它时,它都可以正常工作,但是不管我如何尝试远程运行它(我需要这样做),它总是会失败,并出现以下错误。我用域管理员帐户以及我需要为其更改默认打印机的用户凭据尝试了此操作,但仍然失败 错误 调用“SetDefaultPrinter”时出现异常:“不支持” 第1行字符:1 +(获取WmiObject-类Win32_打印机-过滤器)(Name='Microsoft XPS文档写入。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:NotSpecified:(:)[],MethodInvocationException +FullyQualifiedErrorId:WMIMethodException 代码示例 在本地运行时工作 远程运行时失败 远程运行时也会失败 第三次失败尝试Windows 通过Get-WmiObject设置默认打印机时出错:调用SetDefaultPrinter时异常:不支持 概述,windows,powershell,printing,Windows,Powershell,Printing,我正在尝试使用PowerShell在Windows 2012 R2中设置默认打印机。每当我在本地运行它时,它都可以正常工作,但是不管我如何尝试远程运行它(我需要这样做),它总是会失败,并出现以下错误。我用域管理员帐户以及我需要为其更改默认打印机的用户凭据尝试了此操作,但仍然失败 错误 调用“SetDefaultPrinter”时出现异常:“不支持” 第1行字符:1 +(获取WmiObject-类Win32_打印机-过滤器)(Name='Microsoft XPS文档写入。。。 + ~~~~~~~
非常感谢您的帮助和指导。在继续搜索之后,我终于找到了一个有效的版本。我不完全理解为什么,但它有效,这对我来说已经足够好了。幸运的是,我已经在代码的前面访问了用户名和密码,否则它仍然可以使用
获取凭据
:
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "DOMAIN\Username", (ConvertTo-SecureString -String "ClearTextPassword" -AsPlainText -Force)
Invoke-Command -ComputerName "MyRemoteComputer" -Credential $Credential -ScriptBlock {
$net = new-Object -com WScript.Network
$net.SetDefaultPrinter("Microsoft XPS Document Writer")
}
我不确定,但这可能与运行远程powershell脚本的进程上下文有关。请尝试从远程计算机上的任务管理器运行相同的脚本。请确保选择“仅在用户登录时运行”“。如果成功-则您的命令需要用户交互进程令牌才能运行。此外,将powershell升级到最新版本也是值得尝试的。感谢@AntonKruglov-我想知道是否是这样的,但似乎无法让任何选项组合起作用。不过,我现在已经设法解决了:)
$Printer = Get-WmiObject -ComputerName "MyRemoteComputer" -Class Win32_Printer -Filter "(Name='Microsoft XPS Document Writer')"
$Printer.SetDefaultPrinter()
Invoke-Command -ComputerName "MyRemoteComputer" -ScriptBlock {(Get-WmiObject -Class Win32_Printer -Filter "(Name='Microsoft XPS Document Writer')").SetDefaultPrinter()}
Get-WmiObject -ComputerName "MyRemoteComputer" -Class Win32_Printer -Filter "(Name='Microsoft XPS Document Writer')" | Invoke-WmiMethod -Name 'SetDefaultPrinter'
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "DOMAIN\Username", (ConvertTo-SecureString -String "ClearTextPassword" -AsPlainText -Force)
Invoke-Command -ComputerName "MyRemoteComputer" -Credential $Credential -ScriptBlock {
$net = new-Object -com WScript.Network
$net.SetDefaultPrinter("Microsoft XPS Document Writer")
}