Link to home
Start Free TrialLog in
Avatar of compdigit44
compdigit44

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of compdigit44
compdigit44

ASKER

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
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?
Let me try this again tomorrow and will let you know... thanks again..
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