gpt4 book ai didi

windows - 通过 Get-WmiObject : Exception calling SetDefaultPrinter : Not Supported 设置默认打印机时出错

转载 作者:可可西里 更新时间:2023-11-01 09:32:46 27 4
gpt4 key购买 nike

概览

我正在尝试使用 PowerShell 在 Windows 2012 R2 中设置默认打印机。每当我在本地执行此操作时,它都可以正常工作,但是无论我如何尝试远程运行它(我需要这样做),它总是会失败并出现以下错误。我已经尝试使用域管理员帐户以及我需要为其更改默认打印机的用户凭据,但它仍然失败。

错误

Exception calling "SetDefaultPrinter" : "Not supported " At line:1 char:1 + (Get-WmiObject -Class Win32_Printer -Filter "(Name='Microsoft XPS Document Write ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WMIMethodException

代码示例

在本地运行时有效

$Printer = Get-WmiObject -Class Win32_Printer -Filter "(Name='Microsoft XPS Document Writer')"
$Printer.SetDefaultPrinter()

远程运行失败

$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'

非常感谢任何帮助和指导。

最佳答案

继续搜索后,我终于找到了一个可用的版本。我不完全明白为什么,但它有效,这对我来说已经足够了。幸运的是,我已经在代码的前面访问了用户名和密码,否则它仍然可以使用 Get-Credential:

$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")
}

关于windows - 通过 Get-WmiObject : Exception calling SetDefaultPrinter : Not Supported 设置默认打印机时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42487713/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com