# gitea.RdzeN.net # HP Smart Tank Plus 555 head clean. # # requeres snmp $days = 7 # It indicates the number of days that must pass since the last cleaning session. $secs_per_day = 86400 $days = $days * $secs_per_day # Converted to seconds. $location = Get-Location $path_last_run = "$($location)/hp_clean_last_run" $path_log = "$($location)/hp_clean_log" $snmp_ver = "2c" $snmp_community = "public" $snmp_code_01 = "iso.3.6.1.2.1.43.10.2.1.4.1.1" # OID for printed pages durring actuall sesion $snmp_code_02 = "iso.3.6.1.2.1.43.10.2.1.5.1.1" # OID for printed pages to actuall session $snmp_code_n = 1 # counter for pages starts from 0, so summary we need to decrase by 1 $printer_ip = "" # printer IP $url = "http://$($printer_ip)/DevMgmt/InternalPrintDyn.xml" # url where we need to POST body for start HEAD cleaning. $body = "cleaningVerificationPage" $headers = @{ "Content-Type" = "text/xml" } $logs = @() $log_limit = 10000 function log { param ( $l_level, $l_message ) $l_timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.ffff K" switch ($l_level) { 0 { $log_message = "$($l_timestamp) | [INFO] | $($l_message)" } 1 { $log_message = "$($l_timestamp) | [WARNING] | $($l_message)" } 2 { $l_error = $Error[0].Exception $log_message = "$($l_timestamp) | [ERROR] | $($l_message) | $l_error" } } return $log_message } function log_store { $logs >> $path_log if (Test-Path $path_log) { $tmp = Get-Content $path_log -Tail $log_limit $tmp | Set-Content $path_log } } # Create last_run file where we can store last run data and document count. if (-not(Test-Path $path_last_run)) { $logs += log -l_level 1 -l_message "$($path_last_run) Does not exists - CREATING." $last_run = [PSCustomObject]@{ date = Get-Date -Format "yyyy-MM-dd HH:mm:ss.ffff" p_count = $null } $logs += log -l_level 0 -l_message "Actual Date Stored in $($path_last_run)" $last_run | Export-Csv -Path $path_last_run -NoTypeInformation # Get last run date and amount of printed documents } else { $last_run = Import-Csv $path_last_run $logs += log -l_level 0 -l_message "Last Run Date: $($last_run.date)" } # If the current date is earlier than the retrieved date, we proceed further $actual_run = Get-Date $difference = [DateTime]$actual_run - [DateTime]$last_run.Date | Select-Object TotalSeconds -ExpandProperty TotalSeconds if ($difference -gt $days) { try { $tmp_01 = (snmpget -v $snmp_ver -c $snmp_community $printer_ip $snmp_code_01).Split(" ")[-1] $tmp_02 = (snmpget -v $snmp_ver -c $snmp_community $printer_ip $snmp_code_02).Split(" ")[-1] $tmp = [int]$tmp_01 + [int]$tmp_02 - [int]$snmp_code_n $logs += log -l_level 0 -l_message "SNMP recived $($last_run[0].p_count) | CONTINUE to clean head" # If the number of printed documents is greater than or equal to the number of documents since the last run: try { if ($tmp -ge $last_run[0].p_count) { $last_run[0].p_count = $tmp $tmp = Invoke-RestMethod -Method Post -Body $body -Headers $headers -Uri $url $logs += log -l_level 0 -l_message "HEAD CLEANED $($last_run[0].p_count)" $last_run | Export-Csv -Path $path_last_run -NoTypeInformation $logs += log -l_level 0 -l_message "Head Cleaned | EXIT" log_store exit } else { $logs += log -l_level 0 -l_message "Something was printed in meantime. | EXIT" $last_run | Export-Csv -Path $path_last_run -NoTypeInformation log_store exit } } catch { $logs += log -l_level 2 -l_message "Cannot Clean Head: $($Error[0].Exception) | Printed: $($last_run[0].p_count) | CLOSE" log_store exit } } catch { $logs += log -l_level 2 -l_message "snmpget got error: $($Error[0].Exception) | Printed: $($last_run[0].p_count) | CLOSE" $logs += log_store log_store exit } } else { $logs += log -l_level 0 -l_message "Actual Date is less than $($days) seconds since $($last_run[0].Date) = $difference | Printed: $($last_run[0].p_count) | CLOSE" log_store Exit } log_store # Poprawić logikę. Dodać drukowanie strony tylko za dnia.