使用Powershell在远程PC上安装IP打印机

时间:2016-11-29 22:43:22

标签: powershell network-printers

我正在尝试获取PowerShell代码,以便在远程PC上安装基于IP的打印机。下面是我迄今为止从多个来源汇总的代码。如果有人可以告诉我为什么它没有安装或帮我找出一种不同的安装方式,那就太好了。如果重要的话我在win7环境中并且安装了PowerShell v5

$PrinterIP = “192.168.100.50”
$PrinterPort = “9100”
$PrinterPortName = “IP_” + $PrinterIP
$DriverName = “Canon iR-ADV 4225/4235 PCL6”
$DriverPath = “\\myservername\Drivers\Canon\ir\Multi\x86\pcl6”
$DriverInf = “\\myservername\Drivers\Canon\ir\Multi\x86\pcl6\CNP60U.inf”
$PrinterCaption = “Copier”
$ComputerName = "Remote PC to install on"

Function CreatePrinterPort {
param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
$wmi = [wmiclass]”\\$ComputerName\root\cimv2:win32_tcpipPrinterPort”
$wmi.psbase.scope.options.enablePrivileges = $true
$Port = $wmi.createInstance()
$Port.name = $PrinterPortName
$Port.hostAddress = $PrinterIP
$Port.portNumber = $PrinterPort
$Port.SNMPEnabled = $false
$Port.Protocol = 1
$Port.put()
}

Function InstallPrinterDriver {
Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
$wmi = [wmiclass]”\\$ComputerName\Root\cimv2:Win32_PrinterDriver”
$wmi.psbase.scope.options.enablePrivileges = $true
$wmi.psbase.Scope.Options.Impersonation = `
[System.Management.ImpersonationLevel]::Impersonate
$Driver = $wmi.CreateInstance()
$Driver.Name = $DriverName
$Driver.DriverPath = $DriverPath
$Driver.InfName = $DriverInf
$wmi.AddPrinterDriver($Driver)
$wmi.Put()
}

Function CreatePrinter {
param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
$wmi = ([WMIClass]”\\$ComputerName\Root\cimv2:Win32_Printer”)
$Printer = $wmi.CreateInstance()
$Printer.Caption = $PrinterCaption
$Printer.DriverName = $DriverName
$Printer.PortName = $PrinterPortName
$Printer.DeviceID = $PrinterCaption
$Printer.Put()
}

CreatePrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort -PrinterPortName $PrinterPortName 


InstallPrinterDriver -DriverName $DriverName -DriverPath $DriverPath -DriverInf $DriverInf 


CreatePrinter -PrinterPortName $PrinterPortName -DriverName $DriverName -PrinterCaption $PrinterCaption  

0 个答案:

没有答案