Get-PC/Private/SHSPrinter.ps1

418 lines
20 KiB
PowerShell
Raw Normal View History

2024-06-11 18:27:55 +00:00
# other powershell snmp information
# https://solvedbypowershell.blogspot.com/2014/12/powershell-using-snmp-for-html-network.html
# https://www.powershellgallery.com/packages/Proxx.SNMP/1.0.1.2/Content/Invoke-SnmpGet.ps1
# https://www.powershellgallery.com/packages/Proxx.SNMP/1.0.0.1/Content/Invoke-SnmpWalk.ps1
# https://exchange.nagios.org/directory/Plugins/Hardware/Printers/SNMP-Printer-Check/details
# https://www.powershellgallery.com/packages/Proxx.SNMP/1.1.1.4
# https://www.reddit.com/r/PowerShell/comments/77ls36/printer_page_counter_from_print_server/
# https://gallery.technet.microsoft.com/Get-PrintStatistics-a6bb8323
# https://gallery.technet.microsoft.com/scriptcenter/Script-to-generate-print-84bdcf69
function Get-SHSPrinter {
param (
[string[]]$printers
)
begin {
$snmp = New-Object -ComObject olePrn.OleSNMP
}
process {
foreach ($printer in $printers) {
#Gets the printer ip and full domain name
$result = Get-PrinterIP $printer
#CMDB Data
# $cmdbRecord = Get-LANDeskCMDBItem -Name $printer
$cmdbRecord = Search-ISMBO -BO cis -filter "Name eq '$Printer'" -RawFilter
$LocationConstructors = @(
"SHS_AssetLocality",
"ivnt_Location",
"SHS_Floor",
"SHS_Department",
"SHS_LocationDetails"
)
$LocationData = Foreach($Loc in $LocationConstructors){
if ($Loc -eq 'SHS_Floor'){
$(if ($cmdbRecord.$Loc -match '-'){$cmdbRecord.$Loc.split('-')[-1] + " Floor"} else{$cmdbRecord.$Loc})
} elseif (![string]::IsNullOrEmpty($cmdbRecord.$Loc)){
$cmdbRecord.$Loc
}
2024-06-11 18:27:55 +00:00
}
$LocationData = $LocationData -join ' | '
# $locationData = $cmdbRecord.SHS_AssetLocality + " | " + $cmdbRecord.ivnt_Location + " | " + + " | " + $cmdbRecord.SHS_Department + " | " + $cmdbRecord.SHS_LocationDetails
# if($cmdbRecord.values._SHSLocation3.Length -gt $cmdbRecord.values._SHSCalcLocationString.Length){
# $locationData = $cmdbRecord.values._SHSLocation3
# }
# else{
# $locationData = $cmdbRecord.values._SHSCalcLocationString
# }
if($cmdbRecord){
# $CMDB_POA = $cmdbRecord.values._SHSPOANumber
# $CMDB_AssetTag = $cmdbRecord.values._SHSAssetTag
# $CMDB_Location = $locationData
# $CMDB_MAC = $cmdbRecord.values._SHSMACAddress
# $CMDB_model = $cmdbRecord.values._Model
# $CMDB_serial = $cmdbRecord.values._SerialNumber
# $CMDB_IP = $cmdbRecord.values._IPAddress
### Spark Properties
$CMDB_POA = $cmdbRecord.SHS_POANumber
$CMDB_AssetTag = $cmdbRecord.AssetTag
$CMDB_Location = $locationData
$CMDB_MAC = $cmdbRecord.MACAddress
$CMDB_model = $cmdbRecord.Model
$CMDB_serial = $cmdbRecord.SerialNumber
$CMDB_IP = $cmdbRecord.IPAddress
2024-06-11 18:27:55 +00:00
}
else{
$CMDB_POA = "*CMDB Mismatch - check CMDB*"
$CMDB_AssetTag = "*CMDB Mismatch - check CMDB*"
$CMDB_Location = "*CMDB Mismatch - check CMDB*"
$CMDB_MAC = "*CMDB Mismatch - check CMDB*"
$CMDB_model = "*CMDB Mismatch - check CMDB*"
$CMDB_serial = "*CMDB Mismatch - check CMDB*"
$CMDB_IP = "*CMDB Mismatch - check CMDB*"
2024-06-11 18:27:55 +00:00
}
if($result.IP -ne $null) {
2024-06-11 18:27:55 +00:00
$printerip = $result.IP
$domainName = $result.Path
#checks to see if the printer is online
$online = Test-Connection $printerip -ErrorAction SilentlyContinue
if($online){
2024-06-11 18:27:55 +00:00
#opens snmp connection to the printer
$snmp.open($printerip, 'public', 2, 3000)
# Start of MAC
$pMAC = $SNMP.get(".1.3.6.1.2.1.2.2.1.6.2")
$MAC = [System.Text.Encoding]::Default.GetBytes($PMac) | ForEach-Object {
$_.ToString('X2')
}
$MAC = $MAC -join ':'
#End of MAC
# MODEL
try { $model = $snmp.Get('.1.3.6.1.2.1.25.3.2.1.3.1') } catch { $model = $null }
# IF IT HAS A MODEL NAME...
if ($model) {
# COLOR
# might want to check on this one
try { if ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.2') -match 'Toner|Cartridge|ink') { $color = 'Yes' } else { $color = 'No' } } catch { $color = 'No' }
# TRAYS
try { $trays = $($snmp.GetTree('.1.3.6.1.2.1.43.8.2.1.13') | ? {$_ -notlike 'print*'}) -join ';' } catch { $trays = $null }
2024-06-11 18:27:55 +00:00
# SERIAL
try { $serial = $snmp.Get('.1.3.6.1.2.1.43.5.1.1.17.1') } catch { $serial = $null }
#PAGECOUNT
try { $pagecount = $snmp.Get('.1.3.6.1.2.1.43.10.2.1.4.1.1') } catch { $page = $null }
##### FEATURES, NAME
switch -Regex ($model) {
'^sharp' {
try { $status = $snmp.Get('.1.3.6.1.2.1.43.18.1.1.8.1.1') } catch { $status = $null }
# Start of MAC
$pMAC = $SNMP.get(".1.3.6.1.4.1.11.2.4.3.1.23.0")
write-host $pMAC.ToString()
$MAC = [System.Text.Encoding]::Default.GetBytes($PMac) | ForEach-Object {
$_.ToString('X2')
}
$MAC = $MAC -join ':'
#End of MAC
}
'^zebra' {
try { $name = $snmp.Get('.1.3.6.1.4.1.10642.1.4.0').toupper() } catch { $name = $null }
try { $model = $snmp.Get('.1.3.6.1.4.1.10642.200.19.7.0').toupper() } catch { $model = $null }
try { $MAC = $snmp.Get('.1.3.6.1.4.1.10642.20.10.20.10.2.1.5.2').toupper() } catch { $MAC = $null }
try { $serial = $snmp.Get('.1.3.6.1.4.1.10642.200.19.5.0') } catch { $serial = $null }
# try { $status = $snmp.Get('.1.3.6.1.4.1.10642.200.4.1.0') } catch { $status = $null }
2024-06-11 18:27:55 +00:00
$model = "Zebra $model"
#STATUS
$uri = $domainName
$html = Invoke-WebRequest -uri $uri
$raw = $html.rawcontent
2024-06-11 18:27:55 +00:00
$raw -match '([A-Z])\w+<.F' | Out-Null
$status = ($Matches[0] -split '<')[0]
#split off error condition
$raw -match 'RED>.+' | Out-Null
$split = $Matches[0] -split '>'
$err = $split[1] -split '<'
$status = $status + ' ' + $err[0]
}
'^lexmark' {
try { $name = $snmp.Get('.1.3.6.1.4.1.641.1.5.7.6.0').toupper() } catch { $name = $null }
}
'^ricoh' {
try { $name = $snmp.Get('.1.3.6.1.4.1.367.3.2.1.7.3.5.1.1.2.1.1').toupper() } catch { $name = $null }
}
'^hp' {
try { $name = $snmp.Get('.1.3.6.1.4.1.11.2.4.3.5.46.0').toupper() } catch { $name = $null }
try { $status = $snmp.Get('.1.3.6.1.4.1.11.2.3.9.1.1.3.0') } catch { $status = $null }
if($MAC -eq ''){
2024-06-11 18:27:55 +00:00
$pMAC = $snmp.Get('.1.3.6.1.4.1.11.2.4.3.1.12.1.2.5')
$MAC = ($pMAC -split " ")[-1]
$MAC = ($MAC -replace '(..)','$1:').trim(':')
2024-06-11 18:27:55 +00:00
}
#TONER
<#
HP Maintenance Kit Cur: 1.3.6.1.2.1.43.11.1.1.9.1.2
HP Maintenance Kit Max: 1.3.6.1.2.1.43.11.1.1.8.1.2
HP Toner Cur: 1.3.6.1.2.1.43.11.1.1.9.1.1
HP Toner Max: 1.3.6.1.2.1.43.11.1.1.8.1.1
#>
try { $tonerColor = ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.1') -split ' ')[0]} catch { $tonerColor = $null}
try { $tonerLvl = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.9.1.1')} catch {$tonerLvl = $null}
try { $tonerMax = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.8.1.1')} catch {$tonerMax = $null}
try { $toner = $tonerLvl/$tonerMax} catch {$toner = $null}
2024-06-11 18:27:55 +00:00
[int]$toner = $toner * 100
$supplies = "$tonerColor"+":$toner% "
if($color -eq 'Yes'){
try { $tonerColor = ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.2') -split ' ')[0]} catch { $tonerColor = $null}
try { $tonerLvl = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.9.1.2')} catch {$tonerLvl = $null}
try { $tonerMax = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.8.1.2')} catch {$tonerMax = $null}
try { $toner = $tonerLvl/$tonerMax * 100} catch {$toner = $null}
$supplies += "$tonerColor"+":$toner% "
try { $tonerColor = ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.3') -split ' ')[0]} catch { $tonerColor = $null}
try { $tonerLvl = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.9.1.3')} catch {$tonerLvl = $null}
try { $tonerMax = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.8.1.3')} catch {$tonerMax = $null}
try { $toner = $tonerLvl/$tonerMax * 100} catch {$toner = $null}
$supplies += "$tonerColor"+":$toner% "
try { $tonerColor = ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.4') -split ' ')[0]} catch { $tonerColor = $null}
try { $tonerLvl = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.9.1.4')} catch {$tonerLvl = $null}
try { $tonerMax = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.8.1.4')} catch {$tonerMax = $null}
try { $toner = $tonerLvl/$tonerMax * 100} catch {$toner = $null}
$supplies += "$tonerColor"+":$toner% "
try { $supplyName = ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.6') -split ' ')[0]} catch { $tonerColor = $null}
if($supplyName -contains 'Fuser' -or $supplyName -contains 'Maint'){
try { $supplyPartNumber = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.6')} catch { $supplyPartNumber = $null}
2024-06-11 18:27:55 +00:00
$supplyPartNumber -match '110V-(?<Name>.+), ' | Out-Null
$supplyPartNumber = $Matches.Name
try {$mainLvl = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.9.1.6')} catch {$mainLvl = $null}
try {$mainMax = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.8.1.6')} catch {$mainMax = $null}
if($mainMax -gt 1){
try {$maintKit = $mainLvl / $mainMax} catch {$maintKit = $null}
2024-06-11 18:27:55 +00:00
[int]$maintKit = $maintKit * 100
if($null -ne $maintKit){
$supplies += "| $supplyName" +":$maintKit%[$supplyPartNumber]"
2024-06-11 18:27:55 +00:00
}
}
}
else{
try { $supplyName = ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.5') -split ' ')[1]} catch { $tonerColor = $null}
try { $supplyPartNumber = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.5')} catch { $supplyPartNumber = $null}
2024-06-11 18:27:55 +00:00
$supplyPartNumber -match '110V-(?<Name>.+), ' | Out-Null
$supplyPartNumber = $Matches.Name
try {$mainLvl = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.9.1.5')} catch {$mainLvl = $null}
try {$mainMax = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.8.1.5')} catch {$mainMax = $null}
if($mainMax -gt 1){
try {$maintKit = $mainLvl / $mainMax} catch {$maintKit = $null}
2024-06-11 18:27:55 +00:00
[int]$maintKit = $maintKit * 100
if($null -ne $maintKit){
$supplies += "| $supplyName" +":$maintKit%[$supplyPartNumber]"
2024-06-11 18:27:55 +00:00
}
}
}
}
else{
try { $supplyName = ($snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.2') -split ' ')[0]} catch { $supplyName = $null}
try { $supplyPartNumber = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.6.1.2')} catch { $supplyPartNumber = $null}
2024-06-11 18:27:55 +00:00
$supplyPartNumber -match '110V-(?<Name>.+), ' | Out-Null
$supplyPartNumber = $Matches.Name
try {$mainLvl = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.9.1.2')} catch {$mainLvl = $null}
try {$mainMax = $snmp.Get('.1.3.6.1.2.1.43.11.1.1.8.1.2')} catch {$mainMax = $null}
if($mainMax -gt 1){
try {$maintKit = $mainLvl / $mainMax} catch {$maintKit = $null}
2024-06-11 18:27:55 +00:00
[int]$maintKit = $maintKit * 100
if($null -ne $maintKit){
$supplies += "| $supplyName" +":$maintKit%[$supplyPartNumber]"
2024-06-11 18:27:55 +00:00
}
}
}
}
'^muratec' {
try { $name = $snmp.Get('.1.3.6.1.4.1.11.2.4.3.5.46.0').toupper() } catch { $name = $null }
try { $status = $snmp.Get('.1.3.6.1.2.1.43.16.5.1.2.1.1') } catch { $status = $null }
# Start of MAC
$pMAC = $SNMP.get('.1.3.6.1.4.1.4322.101.1.2.1.5.2.1.1.3.1')
Write-Host $pMAC
$MAC = [System.Text.Encoding]::Default.GetBytes($PMac) | ForEach-Object {
$_.ToString('X2')
}
$MAC = $MAC -join ':'
#End of MAC
}
default {
}
}
#if ($model -like 'SHARP*') {}
}
}
else{
2024-06-11 18:27:55 +00:00
$status = "Offline"
$MAC = $CMDB_MAC
$model = $CMDB_model
$serial = $CMDB_serial
$color = "Offline"
$trays = "Offline"
$pagecount = "Offline"
}
$props = [Ordered]@{
Machine = $printer
Status = $status
IP = $printerip
DomainName = $domainName
MAC = $MAC
Model = $model
Serial = $serial
2024-06-11 18:27:55 +00:00
#Comment = $comment
Color = $color
Trays = $trays
2024-06-11 18:27:55 +00:00
#Features = $features
#SystemDescription = $sysdescr0
#Addresses = $addr
PageCount = $pagecount
Supplies = $supplies
'CMDB POA' = $CMDB_POA
'CMDB AssetTag' = $CMDB_AssetTag
'CMDB Location' = $CMDB_Location
2024-06-11 18:27:55 +00:00
}
$obj = New-Object -TypeName PSObject -Property $props
$snmp.close()
return $obj
}
}
if($CMDB_IP){
$offlineIP = 'CMDB IP - ' + $CMDB_IP
}
else{
$offlineIP = 'No DNS Entry'
}
$props = [Ordered]@{
Machine = $printer
Status = 'No DNS Entry'
IP = $offlineIP
DomainName = 'No DNS Entry'
MAC = $CMDB_MAC
Model = $CMDB_model
Serial = $CMDB_serial
#Comment = $comment
Color = 'No DNS Entry'
Trays = 'No DNS Entry'
PageCount = 'No DNS Entry'
'CMDB POA' = $CMDB_POA
'CMDB AssetTag' = $CMDB_AssetTag
'CMDB Location' = $CMDB_Location
}
$obj = New-Object -TypeName PSObject -Property $props
$snmp.close()
return $obj
2024-06-11 18:27:55 +00:00
}
}
function Get-PrinterIP {
param (
[parameter(ValueFromPipeline)]
[string]$printer
)
$hit = $false
$domains = @('.gsrmc.int.samhealth.net','.avery.int.samhealth.net','.sagh.int.samhealth.net','.snlh.int.samhealth.net','.slch.int.samhealth.net','.spch.int.samhealth.net')
$result = Resolve-DnsName $printer -ErrorAction SilentlyContinue
2024-06-11 18:27:55 +00:00
if($result -eq $null){
$hit = $false
foreach ($domain in $domains) {
$search = "$printer$domain"
$result = Resolve-DnsName $search -ErrorAction SilentlyContinue
if($result -ne $null){
$hit = $true
$name = $result.Name
$ip = $result.IPAddress
2024-06-11 18:27:55 +00:00
}
2024-06-11 18:27:55 +00:00
}
2024-06-11 18:27:55 +00:00
if($result -eq $null){
if($hit -eq $false){
$ip = $null
$name = $null
}
}
}
else{
$name = $result.Name
$ip = $result.IPAddress
2024-06-11 18:27:55 +00:00
}
2024-06-11 18:27:55 +00:00
$props = [ordered]@{Hostname=$printer
IP=$ip
Path=$name
}
$obj = New-Object -TypeName PSObject -Property $props
2024-06-11 18:27:55 +00:00
return $obj
2024-06-11 18:27:55 +00:00
}
2024-06-11 18:27:55 +00:00
function Start-SHSPrinterWeb {
2024-06-11 18:27:55 +00:00
param(
$printer
)
process {
if($printer.DomainName -ne 'No DNS Entry' -and $printer.MAC -ne 'Offline'){
2024-06-11 18:27:55 +00:00
$domainName = $printer.DomainName
start "http://$domainName"
2024-06-11 18:27:55 +00:00
$mac = $printer.MAC
Sleep -Seconds 1
start "https://shsorion/Orion/UDT/EndpointDetails.aspx?NetObject=UE-MAC:VAL=$mac"
2024-06-11 18:27:55 +00:00
}
else{
2024-06-11 18:27:55 +00:00
Write-Warning "Unable to launch web interface"
}
}
}