Pregunta

I'm working on the first steps towards creating a powershell script that will read through printer logs (probably using get-WMI cmdlet), and parse through the logs. Afterwards, I plan on having the script output to a .txt file the name of the printer, a counter of the number of times a printer was used (if possible), and specific info found in the logs.

In order to do this, I've decided to try working backwards. Below is a small portion of what the logs will look like:

10         Document 81, A361058/GPR0000151814_1: owned by A361058 was printed on R3556 via port IP_***.***.***.***.  Size in bytes: 53704; pages printed: 2                                                                  20130219123105.000000-300  
10         Document 80, A361058/GPR0000151802_1: owned by A361058 was printed on R3556 via port IP_***.***.***.***.  Size in bytes: 53700; pages printed: 2   

Working backwards and just focusing on parsing first, I'd like to be able to specifically get the "/GRP", "R3446 (in general, R** as this is the printer name)", and get a counter that shows how often a specific printer appeared in the log files.

It has been a while since I last worked with Powershell, however at the moment this is what I've managed to create in order to try accomplishing my goal:

Select-String -Path "C:\Documents and Settings\a411882\My Documents\Scripts\Print Parse Test.txt" -Pattern "/GPR", " R****" -AllMatches -SimpleMatch 

The code does not produce any errors, however I'm also unable to get any output to appear on screen to see if I'm capturing the /GRP and printer name. At the moment I'm trying to just ensure I'm gathering the right output before worrying about any counters. Would anyone be able to assist me and tell me what I'm doing wrong with my code?

Thanks!

EDIT: Fixed a small error with my code that was causing no data to appear on screen. At the moment this code outputs the entire two lines of test text instead of only outputting the /GPR and server name. The new output is the following:

My Documents\Scripts\Print Parse Test.txt:1:10         Document 81, A361058/GPR0000151814_1: owned by A361058 was printed on
 R3556 via port IP_***.***.***.***.  Size in bytes: 53704; pages printed: 2                                                  
                20130219123105.000000-300  
My Documents\Scripts\Print Parse Test.txt:2:10         Document 80, A361058/GPR0000151802_1: owned by A361058 was printed on
 R3556 via port IP_***.***.***.***.  Size in bytes: 53700; pages printed: 2  

I'd like to try having it eventually look something like the following:

/GPR, R****, count: ## (although for now I'm less concerned about the counter)
¿Fue útil?

Solución

You can try this. It only returns a line when /GPR (and "on" from "printed on") is present.

Get-Content .\test.txt | % { 
    if ($_ -match '(?:.*)(/GPR)(?:.*)(?<=on\s)(\w+)(?:.*)') {
        $_ -replace '(?:.*)(/GPR)(?:.*)(?<=on\s)(\w+)(?:.*)', '$1,$2'
    }
}

Output:

/GPR,R3556
/GPR,R3556

I'm sure there are better regex versions. I'm still learning it :-)

EDIT this is easier to read. The regex is still there for extraction, but I filter out lines with /GPR first using select-string instead:

Get-Content .\test.txt | Select-String -SimpleMatch -AllMatches -Pattern "/GPR" | % {
    $_.Line -replace '(?:.*)(/GPR)(?:.*)(?<=on\s)(\w+)(?:.*)', '$1,$2'
}

Otros consejos

I generally start with an example of the line I'm matching, and build a regex from that, substituting regex metacharacters for the variable parts of the text. This makes makes the regex longer, but much more intuitive to read later.

Assign the regex to a variable, and then use that variable in subsequent code to keep the messy details of the regex from cluttering up the rest of the code:

[regex]$DocPrinted = 
'Document \d\d, \w+/(\D{3})[0-9_]+: owned by \w+ was printed on (\w+) via port IP_[0-9.]+  Size in bytes: \d+; pages printed: \d+'

get-content <log file> |

foreach {
 if ($_ -match $DocPrinted)
   {
     $line -match $docprinted  > $null 
     $matches 
   }
 }                                                           
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top