Revising Powershell Script that check Print Queue

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

LVL 21
compdigit44Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
#Sleep time in seconds until script resumes (can increase if it takes longer to query printer)
Start-Sleep -s 2

Have you tried increasing this to 5 or 10 to see if the script behaves correctly?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
compdigit44Author Commented:
I increased the Sleep time to 10 which did help but when the scritp hit a queue that is error out on... Access Denied etc... Is seems to get hung up and stops
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Seems as sending Ctrl-F4 to close the (error) window does not work in that case. The script sends the key, and waits until it has been received, which might not happen. Is there another window on top not allowing to get closed with Ctrl-F4?
compdigit44Author Commented:
Let me try this again tomorrow and will let you know... thanks again..
compdigit44Author Commented:
When a error dialog box pops up it does not always stop the script from continue on. There does not appear to be any other dialog box open either. Anyway to have the script continue on when an error is encountered.

Thank you for all of our hlpe
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.