Prior to building the code, we need to get the name of the printer in Devices and Printers in Control Panel.
Change (Name of Printer) to the actual name of the target printer and set all custom printing options.
$ExportPath = Path of Drop Folder
$OutputPath = Temporary holding for printing (2Printer.exe grabs and prints files from here)
1. This will get the status of the printer prior to printing:
[code language=”powershell”]$GetPrinterStatus = Get-Printer -Name "(Name of Printer)" | Select-Object -Property PrinterStatus[/code]
2. We check the printer status to make sure the printer is in normal operation and not in an error mode:
[code language=”powershell”]If($GetPrinterStatus.PrinterStatus -eq "Normal"){[/code]
3. Now we check to make sure there are no jobs:
[code language=”powershell”]$GetJobCount = Get-Printjob -PrinterName "(Name of Printer)" | Select ID
IF($GetJobCount -like "*"){[/code]
Lets put it all together:
[code language=”powershell”]$GetPrinterStatus = Get-Printer -Name "(Name of Printer)" | Select-Object -Property PrinterStatus
If($GetPrinterStatus.PrinterStatus -eq "Normal"){
echo "Printer Normal"
$GetJobCount = Get-Printjob -PrinterName "(Name of Printer)" | Select ID
IF($GetJobCount -like "*"){
echo "Currently Printing"
}
Else {
echo "Not Printing"
$ExportPath = "C:\(Drop Folder)"
$File = Get-ChildItem $ExportPath
If ($File) {
echo "Move File"
$OutputPath = "C:\(Name of Printer)"
$InputPath = "C:\(Drop Folder)"
$InputContent1 = Get-ChildItem -Path $InputPath -Force -File | Select-Object -First 1
$Filein = Get-ChildItem $OutputPath
If($Filein) {
echo "File Exists"
}
Else {
Move-Item -Path $InputPath\$InputContent1 -Destination $OutputPath
}
$FileOut = Get-ChildItem $OutputPath
If ($FileOut) {
echo "Print File"
& "C:\Program Files (x86)\2Printer\2Printer.exe" -s $OutputPath\*.* -prn "(Name of Printer)" -delsrc -alerts_no -duplex none -color no -silent
If ($FileOut) {
echo "Delete File"
Remove-Item $OutputPath\*.*
}
Else {
echo "No File to Delete"
}
}
Else {
print "No File to Print"
#exit
}
}
Else {
print "No File Exists"
}
}
}
Else {
echo "Printer Defective"
}[/code]