# printinst.ps1 - Printer installation script # ----------------------------------------------------------------------------------- # Parameters: # printername - eg. "prt04" # connectionname - for adding samba share eg. "\\SERVER\PRINTER\" # do not pass other prams when this is passed # drivername - eg. ""Generic / Text Only" # driverversion - for checking and upgrading driver version. If not specified no version check is performed # portaddress - eg. "192.168.7.52" # portqueue - for lpr print servers eg. "P1" # portnumber - for hp style eg. "9100" # porttype - eg. 2 # driverfileprefix - the prefix of zip and sha files on the website # if the file on the website is # hppcl5-6.1.0-32.zip/hppcl5-6.1.0-64.zip eg. "hppcl5-6.1.0-" # driverfileinstaller - the prefix of the exe file # if the installer filename is # hppcl5-6.1.0-32.exe/hppcl5-6.1.0-64.exe eg. "hppcl5-6.1.0-" # driverdodownload - 1 for downloading from web eg. 1 # Note: If driver is found on system no download will occur # ----------------------------------------------------------------------------------- # 06/Oct/16 - Rudimentary support for Windows 7 added # Apr/16 - Created /jma param( [string]$printername ,[string]$connectionname ,[string]$drivername ,[uint64]$driverversion ,[string]$portaddress ,[int]$porttype ,[string]$portqueue ,[UInt32]$portnumber ,[string]$driverfileprefix ,[string]$driverfileinstaller ,[byte]$driverdodownload ) Set-Variable -Name RC_ERROR -Value "-1" -Option Constant -Visibility Public Set-Variable -Name RC_SUCCESS -Value 0 -Option Constant -Visibility Public #---------------------------------------------- # Function: fnprtinst_port_install # Returns: #---------------------------------------------- function fnprtinst_port_install ([string]$portaddress, [int]$ptype, [string]$pqueue, [UInt32]$pnumber) { $pname = fnprtinst_port_name -ptype $ptype -pqueue $pqueue -pnumber $pnumber if ($legacyinstall -eq $true) { if ($ptype -eq 2) { Write-Host "Installing Lpr port (legacy)"$pname cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnport.vbs -a -r $pname -h $portaddress -o lpr -q $pqueue | Write-Host -ForegroundColor Cyan if($? -eq $false) { Write-Host "Error installing port" -BackgroundColor Red return $RC_ERROR } } elseif ($ptype -eq 3) { Write-Host "Installing HP style port (legacy)"$pname cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnport.vbs -a -r $pname -h $portaddress -o raw -n $pnumber | Write-Host -ForegroundColor Cyan if($? -eq $false) { return $RC_ERROR } } } else { if (Get-PrinterPort | Where-Object {$_.Name -eq $pname}) { Write-Host "Port"$pname" already installed" return } switch ($ptype) { 2 { #Generic Write-Host "Installing Lpr port"$pname Add-PrinterPort -Name $pname -LprHostAddress $portaddress -LprQueueName $pqueue if($? -eq $false) { return $RC_ERROR } } 3 { #HP style Write-Host "Installing HP style port"$pname Add-PrinterPort -Name $pname -PrinterHostAddress $portaddress -PortNumber $pnumber if($? -eq $false) { return $RC_ERROR } } } } } #---------------------------------------------- # Function: fnprtinst_printer_install # Returns: Error = $RC_ERROR #---------------------------------------------- function fnprtinst_printer_install ([string]$prtname, [string]$cntname, [string]$drvname, [string]$portname) { if ($prtname -ne "") { if ($legacyinstall -eq $true) { Write-Host "Installing Printer (legacy)"$prtname cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -a -p $prtname -m $drvname -r $portname | Write-Host -ForegroundColor Cyan if($? -eq $false) { Write-Host "Error installing printer" -BackgroundColor Red return $RC_ERROR } } else { if (Get-Printer | Where-Object {$_.Name -eq $prtname}) { Write-Host "Printer"$prtname" already installed" } else { Write-Host "Installing Printer"$prtname Add-Printer -Name $prtname -DriverName $drvname -PortName $portname if($? -eq $false) { return $RC_ERROR } } } } elseif (Get-Printer | Where-Object {$_.PortName -match [regex]::Escape($cntname.TrimEnd("\"))}) { Write-Host "Windows share printer"$cntname" already installed" } else { Write-Host "Installing Windows share printer"$cntname Add-Printer -ConnectionName $cntname if($? -eq $false) { return $RC_ERROR } } return $RC_SUCCESS } #---------------------------------------------- # Function: fnprtinst_port_name # Returns: [string] name of port #---------------------------------------------- function fnprtinst_port_name([int]$ptype, [string]$pqueue, [UInt32]$pnumber) { switch ($ptype) { 2 { #Generic $pname = ($portaddress + "_" + $pqueue) } 3 { #HP style $pname = ($portaddress + "_" + $pnumber) } } return $pname } #---------------------------------------------- # Function: fnprtinst_end # Returns: #---------------------------------------------- function fnprtinst_end([int]$wait) { Write-Host "End of printer installation script" -ForegroundColor White -BackgroundColor DarkGray | Out-Null #if $wait is true (generally used if there was an error) if ($wait -eq $true) { Pause } exit } #---------------------------------------------- Write-Host "Printer installation script (beta)" -ForegroundColor White -BackgroundColor DarkGray cd $PSScriptRoot $legacyinstall = $true if ($legacyinstall -eq $true) { Write-Host "(Legacy Mode)" } # for Windows shares # installing from a windows share may often fail # if the appropiate driver isn't istalled if ($connectionname -ne "") { if ((fnprtinst_printer_install -prtname $null -cntname $connectionname) -eq $RC_ERROR) { fnprtinst_end -wait $true } else { if ($legacyinstall -eq $true) { fnprtinst_end -wait $true } else { fnprtinst_end -wait $false } } } # for print servers # install driver (if not already installed) # (if driver is installed and driver version is specified and it is # higher than the installed version user will be prompted to upgrade) if ((.\prtdrv.ps1 -drivername $drivername -driververnew $driverversion -driverfileprefix $driverfileprefix -driverfileinstaller $driverfileinstaller -driverdodownload $driverdodownload) -eq $RC_ERROR) { #if there was an error fnprtinst_end -wait $true } # install port (if not already installed) if ((fnprtinst_port_install -portaddress $portaddress -ptype $porttype -pqueue $portqueue -pnumber $portnumber) -eq $RC_ERROR) { #if there was an error fnprtinst_end -wait $true } #install printer (if not already installed) if ($printername -ne "") { $portname = fnprtinst_port_name -ptype $porttype -pqueue $portqueue -pnumber $portnumber if((fnprtinst_printer_install -prtname $printername -cntname $null -drvname $drivername -portname $portname) -eq $RC_ERROR) { #if there was an error fnprtinst_end -wait $true } else { if ($legacyinstall -eq $true) { fnprtinst_end -wait $true } else { fnprtinst_end -wait $false } } } fnprtinst_end -wait $true