printer.ps1 - powershell script by ActiveXperts Software
printer.ps1 checks whether a printer is up and running.
Use printer.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select printer.ps1. Configure the required parameter, or press 'Load a working sample'.
In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.
printer.ps1 script code
################################################################################# # ActiveXperts Network Monitor PowerShell script, © ActiveXperts Software B.V. # For more information about ActiveXperts Network Monitor, visit the ActiveXperts # Network Monitor web site at http://www.activexperts.com ################################################################################# # Script # Printer.ps1 # Description: # This function checks if the printer, indicated by strPrinter, is up and running on # the machine specified by strHost. # Declare Parameters: # 1) strHost (string) - Hostname or IP address of the computer you want to check # 2) strPrinter (string) - Name of the printer # 3) strAltCredentials (string, optional) - Specify an empty string to use Network Monitor service credentials. # To use alternate credentials, enter a server that is defined in Server Credentials table. # (To define Server Credentials, choose Tools->Options->Server Credentials) # Usage: # .\Printer.ps1 '<Hostname | IP>' '<Printername>' '[alt-credentials]' # Sample: # .\Printer.ps1 'localhost' 'HP LaserJet 2300 Series PS' ################################################################################# # -- Declare Parameters param( [string]$strHost = '', [string]$strPrinter = '', [string]$strAltCredentials = '' ) # -- Use _activexperts.ps1 with common functions . 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1' ################################################################################# # // --- Main script --- ################################################################################# # -- Clear screen and clear error cls $Error.Clear() # -- Validate parameters, return on parameter mismatch if( $strHost -eq '' -or $strPrinter -eq '' ) { $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\Printer.ps1 "<Hostname | IP>" "<Printername>" "[alt-credentials]"' echo $res exit } # Declare local variables by assigning an initial value to it $objAltCredentials = $null $strExplanation = '' # If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings if( $strAltCredentials -ne '' ) { # Get the Alternate Credentials object. Function "AxGetCredentials" is implemented in "activexperts.ps1" if( ( AxGetCredentials $strHost $strAltCredentials ([ref]$objAltCredentials) ([ref]$strExplanation) ) -ne $AXSUCCESS ) { echo $strExplanation exit } } # -- Get WMI object $strWmi = 'Win32_Printer' if( $objAltCredentials -eq $null ) { $objWmi = Get-WmiObject -ComputerName $strHost -Class $strWmi -ErrorVariable Error -ErrorAction SilentlyContinue } else { $objWmi = Get-WmiObject -ComputerName $strHost -Class $strWmi -Credential $objAltCredentials -ErrorVariable Error -ErrorAction SilentlyContinue } # If anything went wrong Powershell sets the error in the global array $Error if( $Error -ne '' ) { $res = 'UNCERTAIN: ' + $Error echo $res exit } if( $objWmi -eq $null ) { $res = 'ERROR: Unable to access "' + $strHost + '". Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down' echo $res exit } foreach( $strWmiPrinter in $objWmi ) { if( $strWmiPrinter.name -eq $strPrinter ) { $status = $strWmiPrinter.PrinterStatus $result = '' switch( $status ) { 1{ $result = 'Other' } 2{ $result = 'Unknown' } 3{ $result = 'Running/Full Power' } 4{ $result = 'Warning' } 5{ $result = 'In Test' } 6{ $result = 'Not Applicable' } 7{ $result = 'Power Off' } 8{ $result = 'Off Line' } 9{ $result = 'Off Duty' } 10{ $result = 'Degraded' } 11{ $result = 'Not Installed' } 12{ $result = 'Install Error' } 13{ $result = 'Power Save - Unknown' } 14{ $result = 'Power Save - Low Power Mode' } 15{ $result = 'Power Save - Standby' } 16{ $result = 'Power Cylce' } 17{ $result = 'Power Save - Warning' } default{ $result = 'Unknown' } } } } if( $result -ne $null ) { $res = 'SUCCESS: Printer found (' + $result + ') DATA: ' + $status } else { $res = 'ERROR: Printer [' + $strPrinter + '] not found on print server [' + $strHost + ']' } # -- Print script result echo $res exit ################################################################################# # // --- Catch script exceptions --- ################################################################################# trap [Exception] { $res = 'UNCERTAIN: ' + $_.Exception.Message echo $res exit }