We help IT Professionals succeed at work.
Get Started

Revising Powershell Script that check Print Queue

compdigit44
compdigit44 asked
on
245 Views
Last Modified: 2015-03-05
A number of months ago ,  EE expert " Joshua Grantom "wrote the script below that checked a printer UNC path and sent the results to CSV listing if it was up or down. I have noticed when the script hist a queue it cannot connect to it stops the script from continuing any suggestions on how to correct this


# Script Author: Joshua Grantom 
# Version: 1.1
# Purpose: Checks a list of Printers by UNC path to see if they are connectable

#Loads Windows Forms Assembly
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Printer List (1 UNC Printer path per line)
$printers = Get-Content "c:\PrintQueues.txt"
#CSV File to Output to
$outputfile = "c:\PrintQueues.csv"
#Adds Header to CSV File
"Printer UNC Path `t Connection Status" | Out-File $outputfile -append
#Tries to connect to each printers queue (does not require drivers to be installed)
foreach ($printer in $printers) {
Invoke-Expression -command "rundll32 printui,PrintUIEntry /o /n '$printer'"
#Sleep time in seconds until script resumes (can increase if it takes longer to query printer)
Start-Sleep -s 2
$window = Get-Process rundll32 | Where-Object {$_.MainWindowTitle -like "*"} | Select MainWindowTitle | ft -HideTableHeaders | Out-String
IF([string]::IsNullOrWhiteSpace($window)){
    #If rundll32 window title is blank, usually means error, write error
    "$printer`t Error Connecting" | Out-File $outputfile -append
}else{
    #If rundll32 window title is not blank, usually means queue is opened, write success
    "$printer`t Successfully Connected" | Out-File $outputfile -append
}
# Closes active window (print queue or error message)
[System.Windows.Forms.SendKeys]::SendWait("%{F4}") 
}
                                          

Open in new window

Comment
Watch Question
IT Consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE