Avo
Avo

Reputation: 13

powershell - get file, parse it and if "value=" < X then function sendMail

i have a text file that is automatically downloaded and updated with Task Scheduler.

It looks like this:

C:\PrintLog\GetUsageReportFromPrinterWebService\10.22.17.102:58:<input type="hidden" name="AVAILABELBLACKTONER" value="60">
C:\PrintLog\GetUsageReportFromPrinterWebService\192.167.10.140:58:<input type="hidden" name="AVAILABELBLACKTONER" value="80">
C:\PrintLog\GetUsageReportFromPrinterWebService\192.167.14.128:58:<input type="hidden" name="AVAILABELBLACKTONER" value="80">

I would like to:

  1. delete "C:\PrintLog\GetUsageReportFromPrinterWebService\" and "input type="hidden" name="AVAILIBLETONER""
  2. replace that IP adress with printer name (example 10.51.17.122:58 equals HP-01, 192.168.10.150:58 equals HP-02 etc)
  3. check if "value=" is smaller than 20 and if so than send an email with function sendMail

It doesn't matter what is in that email (after that I will check it manually with webservice anyway). I just need this as an remainder/alerter that some printer is getting low on toner, so I am not forced to manually check that txt file everyday(I most certainly would forget that :) ). These printers are offsite so I need to know in advance that the printer is low.

Note1: There are empty lines and spaces at the beginning of that txt

Note2: And no, there is no send report via email when low on toner to be configured on those printers (I double checked).

Note3: using C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

I guess point one and two are optional. The third one is important for me, I googled for something similar but just got lost as everyone wanted something little bit different.

Thanks

Upvotes: 0

Views: 179

Answers (2)

Hunter Eidson
Hunter Eidson

Reputation: 1909

Here's another possibility (I think it requires Powershell 4 because of Send-Mailmessage, but I could be wrong):

#requires -version 4.0

$logs = Get-Content "F:\scripts\ParseLog\file.log"

$warning = $false
$lowPrinters = ""

# Mail Settings
$Subject = "Low Toner Warning"
$To = "[email protected]"
$From = "[email protected]"
$SMTPServer = "smtp.contoso.com"
$Priority = "High"

$printerList = @{
    "10.51.17.122:58" = "HP-01"; 
    "192.168.10.150:58" = "HP-02";
    "10.22.17.102:58" = "HP-03";
    "192.167.10.140:58" = "HP-04";
}

foreach ( $log in $logs ) {
    if ( $log -match '^C:\\PrintLog\\GetUsageReportFromPrinterWebService\\([^:]+:[^:]+):.*value="(.*)">$' ) {
        $printer = $Matches[1]
        $toner = [int]$Matches[2]
    }

    if( $toner -lt 20 ) {
        $warning = $true
        if( $printerList.ContainsKey( $printer ) ) {
            $printerName = $printerList[ $printer ]
        } else {
            $printerName = $printer
        }
        $lowPrinters += "Printer {0} has a low toner level of {1}.`n" -f $printerName, $toner
    }
}

if( $warning ) {
    Send-MailMessage -From $From -To $To -Subject $Subject -body $lowPrinters -SmtpServer $SMTPServer
}

Along about line 8 we setup some stuff for sending email. Starting at line 15, we build a hash table mapping printer IPs/Ports with Printer Names (since printer queues aren't always listed in DNS, I decided to use a hash table instead). On line 23, we use a regular expression to grab the ip and port, and the toner value by using the -match operator. Stuff grabbed by the regex is stored in an array called $Matches.

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

As an example, you could do something like this:

$filePath = "C:\updatedFile.txt"
$prefix   = "C:\PrintLog\GetUsageReportFromPrinterWebService\"
$lines    = Get-Content $filePath |Where-Object {$_.Trim() -like "$($prefix)*"}

foreach($line in $lines)
{
    $content = $line.Trim().Substring($prefix.Length)
    $parts   = $content -split ":"
    $inputML = [xml]"$($parts[2])</input>"
    $inputValue = [int]$inputML.input.value

    if($inputValue -lt 20)
    {
        $printer = [System.Net.DNS]::GetHostByAddress($parts[0]).HostName
        <#
            Your sendMail call in here
        #>
    }

}

You remove the "C:\PrintLog\GetUsageReportFromPrinterWebService\" part with Substring(), split it into 3 pieces, and then parse to last part as an xml element, giving much easier access to the value attribute

The IP to Printer name part will only work if you have reverse DNS in place already

Upvotes: 0

Related Questions