Situatie
Va propun un script PowerShell pentru instalarea remote a unei imprimante pe mai multe statii aflate in acelasi domeniu. Imprimanta poate sa fie o imprimanta locala, caz in care se folosesc porturile LPT sau USB sau o imprimanta de retea, caz in care se va folosi adresa IP a imprimantei pentru instalare.
Backup
Se evita astfel folosirea consolei ‘mmc.exe’ caz in care se adauga statiile manual si se instaleaza manual pe fiecare statie in parte, sau folosirea Group Policy. Acest caz este folositor daca nu avem acces ca administrator la Group Policy. Se deschide PowerShell cu drepturi de administrator!
Solutie
####################################################
# Mai jos se schimba valorile astfel incat sa fie corespunzatoare cu necesitatile, se modifica valorile marcate cu rosu!
$PrinterIP = “aici se trece IP-ul imprimantei”
$PrinterPort = “9100”
$PrinterPortName = “IP_” + $PrinterIP
$DriverName = “Numele driver-ului de imprimanta”
$DriverPath = “calea unde se afla driver-ul”
$DriverInf = “calea fisierului *.INF de unde se instaleaza”
$PrinterCaption = “aici se trece numele imprimantei”
####################################################
### ComputerList Optiunea 1 ###
#$ComputerList = @(“IP1,IP2,IP3,etc”)
### ComputerList Optiunea 2 ###
$ComputerList = @()
Import-Csv “C:\Temp\statii.csv” | `
% {$ComputerList += $_.Computer}
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”)
$wmi.psbase.scope.options.enablePrivileges = $true
$Printer = $wmi.CreateInstance()
$Printer.Caption = $PrinterCaption
$Printer.DriverName = $DriverName
$Printer.PortName = $PrinterPortName
$Printer.DeviceID = $PrinterCaption
$Printer.Put()
$wmi.Put()
}
foreach ($computer in $ComputerList) {
CreatePrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort `
-PrinterPortName $PrinterPortName -ComputerName $computer
InstallPrinterDriver -DriverName $DriverName -DriverPath `
$DriverPath -DriverInf $DriverInf -ComputerName $computer
CreatePrinter -PrinterPortName $PrinterPortName -DriverName `
$DriverName -PrinterCaption $PrinterCaption -ComputerName $computer
}
####################################################
# } else {
# Write-Host(“$nume,statia este oprita…”)
# }}
Leave A Comment?