Issues automating printer driver update (printer settings) and printer preferences in Win7, using a PS,cmd,vbs,etc script?

你说的曾经没有我的故事 提交于 2019-12-25 05:34:15

问题


WMI can do it, but I have an issue, PCs are on, but logged off. If I try to run:

wmic /node:%strIP% printer where DeviceID="lp1" set DriverName="Lexmark Universal v2"

It fails with a message about a "generic failure". I RDP in and then run the same command from my end, and it works. Powershell version I am using is older, so it does not have some of the printer cmdlets, and updating PS is currently out of the question. Is there a way to remotely log someone in, without actually having to RDP in? Via PS, cmd, PSEXEC, etc?

The other avenue I've taken is using regedit, but I'm hitting some hicups with that, namely that I cannot figure out what to copy. In regedit, I can change the drivername and the setting that enable duplex and tray2 (in printer settings), but I cannot figure how to change the settings in printer preferences for printing double sided and doing so along the long edge.

What I did to figure out what to change, I did a find on the printer name in regedit as a data value and exported the keys before changing the settings. Then I exported it again AFTER changing the settings. I then used fc /c /a /u before.reg after.reg to get the changes. I chopped up the .reg to include only the changed values. Running the .reg seems to change everything, but the print both sides, along the long edge settings. It is a lexmark printer, so I am wondering if maybe preferences for it are stored elsewhere.

This is my most up to date PS1 script. I've commented out some lines as I tried different ways of doing things:

$Cred = Get-Credential
$Str = Read-Host "Please select a site ID [###] "
$PC = Read-Host "Please select a PC number [##] "

Clear-Host
$PCNm = "$Str-CCPC-$PC"

function Test-PsRemoting
{
    try
    {
        $errorActionPreference = "Stop"
        $result = Invoke-Command -ComputerName $PCNm { 1 }
    }
    catch
    {
        Write-Verbose $_
        return $false
    }

    if($result -ne 1)
    {
        Write-Verbose "Remoting to $PCNm returned an unexpected result."
        return $false
    }

    $true   
} 

If(!(Test-PsRemoting)){
    PSEXEC \\$PCNm powershell Enable-PSRemoting -force 2>&1 >nul
    Clear-Host
    Write-Host "Enabled PsRemoting"
}else{Write-Host "PsRemoting already enabled"}

Invoke-Command -ComputerName $PCNm -Credential $Cred -ScriptBlock {
    #$lp1 = Get-WMIObject -Query "SELECT * from Win32_Printer Where DeviceID='lp1'"
    $lp1 = Get-WmiObject Win32_Printer | ?{$_.name -eq "lp1"}
    $lp1.Scope.Options.EnablePrivileges = $true
    $lp1.DriverName = "Lexmark Universal v2"
    $lp1R = $lp1.Put()
    #$lp2 = Get-WMIObject -Query "SELECT * from Win32_Printer Where DeviceID='lp2'"
    $lp2 = Get-WmiObject Win32_Printer | ?{$_.name -eq "lp2"}
    $lp2.Scope.Options.EnablePrivileges = $true
    $lp2.DriverName = "Lexmark Universal v2"
    $lp2R = $lp2.Put()
}

#$lp1 = Get-WMIObject -Impersonation Delegate -Authentication Call -Credential $Cred -ComputerName $PCNm -Query "SELECT * from Win32_Printer Where DeviceID='lp1'"
#$lp1.DriverName = "Lexmark Universal v2"
#$lp1.Put()

No matter which way I try it, invoke-command, or get-wmiobject, I get:

Exception calling "Put" with "0" argument(s): "Generic failure "
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
    + PSComputerName        : 150-CCPC-02

回答1:


This doesn't particularly answer your actual question but as a solution for how I do this very same thing I thought I would give you what I threw together to update printer properties. I have not cleaned this up at all as I was porting it from my create printer function.

Function Set-SSPrinter {

Param(
    [Parameter(Mandatory=$true,
        ValueFromPipeline=$True,
        ValueFromPipelineByPropertyName=$True)]
    [ValidateNotNullOrEmpty()]
    [string]$ComputerName,
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]$Name,
    [string]$PortName,
    [string]$DriverName,
    [string]$Comment,
    [string]$Location,
    [bool]$Shared,
    [string]$ShareName = $Name,
    [string]$PermissionSDDL,
    [string]$PrintProcessor,
    [string]$DataType,
    [bool]$RawOnly
    )

try {
    $modprinter = Get-WmiObject Win32_Printer -ComputerName $ComputerName | ?{$_.name -eq $Name}
    $modprinter.Scope.Options.EnablePrivileges = $true

        if($DriverName) {
            $modprinter.DriverName = $DriverName
        }
        if($PortName) {
            $modprinter.PortName = $PortName
        }
        if($Shared) {
            $modprinter.Shared = $Shared
        }
        if($ShareName) {
            $modprinter.ShareName = $ShareName
        }
        if($Location) {
            $modprinter.Location = $Location
        }
        if($Comment) {
            $modprinter.Comment = $Comment
        }
        if($Name) {
            $modprinter.DeviceID = $Name
        }
        if($PrintProcessor) {
            $modprinter.PrintProcessor = $PrintProcessor
           }
        if($DataType) {
            $modprinter.PrintJobDataType = $DataType
        }
        if($RawOnly) {
            $modprinter.RawOnly = $RawOnly  
           }

        $result = $modprinter.Put()

        if($PermissionSDDL) {
            $modprinter.SetSecurityDescriptor($objHelper.SDDLToWin32SD($PermissionSDDL).Descriptor) | Out-Null
        }
        $("Update Complete: " + $Name)
    } catch {
        $("Update Failed: " + $Name)
        Write-Warning $_.Exception.Message 
        $error.Clear()
    }

}

Unfortunately I use the printer name to figure out which device to modify on the remote machine. Your executing credentials from the powershell session you have open must have admin rights on the remote machine. if necessary do a runas different user on powershell.exe

Example usage:

Set-SSPrinter -ComputerName "10.210.20.100" -Name "TestPrinter" -DriverName "Lexmark Universal v2"



回答2:


wmic /node:servername /user:username /password:password path win32_something call methodname

Is how to do it.

Things with users are best done with logon scripts because that is how windows is designed.



来源:https://stackoverflow.com/questions/30570390/issues-automating-printer-driver-update-printer-settings-and-printer-preferenc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!