Reputation: 700
I'm attempting to use PowerShell to set a default printer in Windows 2012 R2. This works fine every time whenever I do it locally, however regardless of how I attempt to run it remotely, which I need to do, it always fails with the below error. I've tried this with the Domain Administrator account as well as the user's credentials who I need to change the default printer for, but it still fails.
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'
Any help and guidance greatly appreciated.
Upvotes: 3
Views: 1708
Reputation: 700
After continued searching, I've finally found a version which works. I don't fully understand why, but it works, and that's good enough for me. Fortunately I've already got access to the username and password earlier in the code, otherwise it would still work with 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")
}
Upvotes: 1