May 27, 2013

Just thought I'd post my powershell script on how to install local printers on remote workstations from a text file.  I know most people use print server nowadays, but there's always one or two that will find this information useful.  In a later post, I'll cover the process on how to make a printer settings file that I use here.


Function AddPrinterDriver($targetComputer, $driverSource, $driverINF, $driverName) {
    $model =  $driverSource.Substring($driverSource.LastIndexOf("\")+1)
    $driverDestination = "\\$targetComputer\c$\temp\printers\$model"
    $psexecPath = "C:\Users\Public\PSTools\PsExec.exe"

    "Driver Destination: " + $driverDestination
    "Driver Source: $driverSource\"
    #Copy folder to remote computer
    if(Test-Path "$driverDestination")
    {
        #code if driver is already there
    } else {       
        if(test-path "$driverSource"){
            Copy-Item "$driverSource\" "$driverDestination" -recurse
        } else {
            "$driverSource does not exist"
        exit
        }

    }
   
   
    # install printer using INF file,  /u -  /m - model /f - file   
    $driverInstallcommand =  "C:\WINDOWS\system32\rundll32.exe printui.dll,PrintUIEntry /ia /u /m ""$driverName"" /f ""$driverDestination\$driverINF"""   
    $installCommand = "\\$targetComputer " + $driverInstallcommand
   
    start-process $psexecPath -ArgumentList $installCommand -NoNewWindow
    start-sleep -s 15

    #Remove folder recursively
    #Remove-Item -Recurse -Force $driverDestination
}

function CreatePrinterPort ($TargetComputer, $PortName, $HostAddress) {
    $port = ([WMICLASS]"\\$TargetComputer\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()
    $port.Name=$PortName
    $port.SNMPEnabled=$false
    $port.Protocol=1
    $port.HostAddress=$HostAddress
    $port.Put()
}

function CreatePrinter ($TargetComputer, $DriverName, $PortName, $Location, $Comment) {
    $print = ([WMICLASS]"\\$TargetComputer\ROOT\cimv2:Win32_Printer").createInstance()
   
    $print.PortName = $PortName
    $print.Shared = $false   
   
    $print.drivername = $DriverName
    $print.Location = $Location
    $print.Comment = $Comment
    $print.DeviceID = $PortName
    
    $print.Put()     
}

function setPrinterSettings($targetComputer, $portName, $binFile, $psExecPath){

    #Add Binfile Settings to Printer
   
    Copy-Item $binFile "\\$targetComputer\c$\temp\"
    $newBin = "\\$targetComputer\c$\temp\" + $portName + ".bin"

    $PrinterSettingsCommand =  "C:\WINDOWS\system32\rundll32.exe printui.dll,PrintUIEntry /Sr /n ""$portName"" /a ""$newBin"" g d"
    $installCommand = "\\$targetComputer " + $PrinterSettingsCommand
   
    $installCommand
    start-process $psexecPath -ArgumentList $installCommand
   
}

$computerList = Get-Content "c:\temp\PrinterInstallList.txt"

$driverINF = "hpc4700b.inf"
$psExecPath = "C:\Users\Public\PSTools\PsExec.exe"
$PortName = "200WELL02P04"
$DriverName = "HP Color LaserJet 4700 PCL 5c"
$driverSource = "\\dtftp\e$\PRINTERS\_Script Installs\200 Wellington\HP4700"
$binFile = "\\dtftp\e$\PRINTERS\_Script Installs\200 Wellington\200well02p04.bin"



Foreach($targetComputer in $computerList)
{   
    #Test if Online
    if(Test-Connection -ComputerName $targetComputer -Count 1 -ErrorAction SilentlyContinue)
    {
        #Test if printer is installed
        $colItems = get-wmiobject -class "Win32_Printer" -namespace "root\CIMV2" -computername $targetComputer | where {$_.name -eq $portName}
        if($colItems)
        {
            "$targetComputer,Already Installed"
        }else{
            AddPrinterDriver -targetComputer $targetComputer -driverSource $driverSource -driverINF $driverINF -driverName $driverName
            CreatePrinterPort -TargetComputer $targetComputer -HostAddress "$PortName.printers.toronto.ca" -PortName $PortName
            CreatePrinter -TargetComputer $targetComputer -PortName $PortName -DriverName $DriverName
            setPrinterSettings -targetComputer $targetComputer -portName $portName -binFile $binFile -psExecPath $psExecpath
            "$targetComputer, Installed"
        }

    } else {
        "$targetComputer,NotOnline"
    }
}