Snippets
Created by
Stephen Valdinger
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | Function Get-XeroxTonerStatus {
<#
.SYNOPSIS
Scans printers on a network via SNMP for toner level percentages
.DESCRIPTION
Pulls the Capacity value, and current value of the toner cartridges, and does math to read back a percentage.
If the percentage is less than or equal to your threshold value, a toast notification is raised on target computer(s).
.PARAMETER AlertThresholdPercentage
The % a toner needs to be at before a toast is raised. Low number recommended (ie, 5)
.PARAMETER SnmpString
The GET snmp string utilized on your network
.PARAMETER Printers
A CSV file containg the following headers:
IP Address,Name,FriendlyName,Type
.PARAMETER Computers
Array of computers to send a toast notification too.
.EXAMPLE
Get-XeroxTonerStatus -Printers "F:\Automation\ToastData.csv" -SnmpString 'Fancy-String' -AlertThresholdPercentage 50
.EXAMPLE
Get-XeroxTonerStatus -Printers "F:\Automation\ToastData.csv" -Computers pc1,pc2,pc3 -SnmpString 'Fancy-String' -AlertThresholdPercentage 50
#>
Param(
[cmdletBinding()]
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullorEmpty()]
[string]$Printers,
[Parameter(Mandatory = $False, Position = 1)]
[string[]]$Computers,
[Parameter(Mandatory, Position = 2)]
[string]$SnmpString,
[Parameter(Mandatory, Position = 3)]
[int]$AlertThresholdPercentage
)
#Pre-reqs
Import-Module BurntToast
If(!(Test-Path C:\usr\bin\snmpget.exe)){
Write-Error "Net-SNMP required. Install: http://www.net-snmp.org/download.html"
Break
}
#region: OID Values. Tested on Xerox Altalink C8035,C8055, Versalink C405,C400,B400. Your may need to adjust these per your models.
$blackFullOID = '.1.3.6.1.2.1.43.11.1.1.8.1.1'
$blackCurrentOID = '.1.3.6.1.2.1.43.11.1.1.9.1.1'
$yellowFullOID = '.1.3.6.1.2.1.43.11.1.1.8.1.2'
$yellowCurrentOID = '.1.3.6.1.2.1.43.11.1.1.9.1.2'
$magentaFullOID = '.1.3.6.1.2.1.43.11.1.1.8.1.3'
$magentaCurrentOID = '.1.3.6.1.2.1.43.11.1.1.9.1.3'
$cyanFullOID = '.1.3.6.1.2.1.43.11.1.1.8.1.4'
$cyanCurrentOID = '.1.3.6.1.2.1.43.11.1.1.9.1.4'
#end region
$Data = Import-Csv $Printers
Foreach ($printer in $Data) {
$ip = $printer.'IP Address'
#region check connectivity and gather snmp data
If (!(Test-Connection $ip -Quiet -Count 1)) {
New-BurntToastNotification -Text "Printer: $($printer.FriendlyName) appears OFFLINE!" -AppLogo "F:\Automation\Images\K.png"
}
Else {
Write-Verbose -Message "Gathering data on: $($printer.FriendlyName)"
If ($printer.Type -notmatch 'BW') {
$blackFullReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $blackFullOID
$blackCurrentReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $blackCurrentOID
$yellowFullReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $yellowFullOID
$yellowCurrentReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $yellowCurrentOID
$magentaFullReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $magentaFullOID
$magentaCurrentReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $magentaCurrentOID
$cyanFullReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $cyanFullOID
$cyanCurrentReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $cyanCurrentOID
}
Else {
$blackFullReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $blackFullOID
$blackCurrentReading = C:\usr\bin\snmpget -v '2c' -c $SnmpString -O v $ip $blackCurrentOID
}
#endregion
#Region extract only digits from snmp string
$blackFull = $blackFullReading.Split(':')[1].Trim()
$blackCurrent = $blackCurrentReading.Split(':')[1].Trim()
$yellowFull = $yellowFullReading.Split(':')[1].Trim()
$yellowCurrent = $yellowCurrentReading.Split(':')[1].Trim()
$magentaFull = $magentaFullReading.Split(':')[1].Trim()
$magentaCurrent = $magentaCurrentReading.Split(':')[1].Trim()
$cyanFull = $cyanFullReading.Split(':')[1].Trim()
$cyanCurrent = $cyanCurrentReading.Split(':')[1].Trim()
#Endregion
#Region: Create objects of each % value
[pscustomobject]$blackPercentRemaining = @{
Value = [math]::Round(([int]$blackCurrent / [int]$blackFull) * 100)
Toner = 'Black'
Friendly = $printer | Select-Object -Expand FriendlyName
}
[pscustomobject]$yellowPercentRemaining = @{
Value = [math]::round(([int]$yellowCurrent / [int]$yellowFull) * 100)
Toner = 'Yellow'
Friendly = $printer | Select-Object -Expand FriendlyName
}
[pscustomobject]$magentaPercentRemaining = @{
Value = [math]::Round(([int]$magentaCurrent / [int]$magentaFull) * 100)
Toner = 'Magenta'
Friendly = $printer | Select-Object -Expand FriendlyName
}
[pscustomobject]$cyanPercentRemaining = @{
Value = [math]::Round(([int]$cyanCurrent / [int] $cyanFull) * 100)
Toner = 'Cyan'
Friendly = $printer | Select-Object -Expand FriendlyName
}
#EndRegion
#Region check values and raise alerts
$readings = @($blackPercentRemaining, $yellowPercentRemaining, $magentaPercentRemaining, $cyanPercentRemaining)
$lowArray = @()
Foreach ($r in $readings) {
If ($r.Value -le $alertThresholdPercentage) {
$lowArray += $r
}
}#value foreach
Foreach ($l in $lowArray) {
If(!($Computers)){
$toastParams = @{
Text = ("Toner Alert for $($l.Friendly)", "$($l.Toner) Toner is at $($l.Value)%! Replace soon!")
Sound = 'Reminder'
AppLogo = "F:\Automation\Images\K.png"
}
New-BurntToastNotification @toastParams
}
Else {
Foreach($C in $Computers){
[pscustomobject]$toastParams = @{
Text = ("Toner Alert for $($l.Friendly)", "$($l.Toner) Toner is at $($l.Value)%! Replace soon!")
Sound = "Reminder"
}
Invoke-Command -Computername $C -Scriptblock { New-BurntToastNotification -Text ($args[0],$args[1])} -Args $toastParams.Text
}#toast foreach
#endregion
}#logic else
}#printer foreach
}#function
Get-XeroxTonerStatus -Printers "F:\Automation\ToastData.csv" -SnmpString 'Test-SNMPSTring' -AlertThresholdPercentage 5 -Verbose
|
Comments (1)
You can clone a snippet to your computer for local editing. Learn more.
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199