3
votes

Overview

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.

Error

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

Code Example

Works when run locally

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

Fails when run remotely

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

Also fails when run remotely

Invoke-Command -ComputerName "MyRemoteComputer" -ScriptBlock {(Get-WmiObject -Class Win32_Printer -Filter "(Name='Microsoft XPS Document Writer')").SetDefaultPrinter()}

Third failure attempt

Get-WmiObject -ComputerName "MyRemoteComputer" -Class Win32_Printer -Filter "(Name='Microsoft XPS Document Writer')" | Invoke-WmiMethod -Name 'SetDefaultPrinter'

Any help and guidance greatly appreciated.

1
I am not sure but this maybe related to the process context where remote powershell script is being run. Try running same script from say Task Manager on remote machine. Make sure to select "Run only when user is logged on". If it succeeds - then your command requires user-interactive process token to run. Also upgrading powershell to the latest version is worth trying.Anton Krouglov
Thanks @AntonKruglov - I wondered if it was something like that, but couldn't seem to get any combination of options working. I've managed to work it out now though :)AndyHerb

1 Answers

1
votes

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