![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Inactive printer-checking script Hello! I'm working at a company that have alot of printers (1200-1500). However we are not sure wich ones that still are in function. So I started develop a script to find out what printer(port)s that had been used the latest 6 months. My idea was to run this on every printerserver locally and to use the eventviewer as a reference if the printer had been used or not. I have a separate file with all portnames (one name on each line) that I want to read and loop through. Since this tend to be abit messy post I'm gonna try explain again: Filtering evenlog on EventID 10 and no more then 6 months old. Read line 1 in Printerports.txt Check if the printerportname from line 1 in Printerportsname.txt is in the message from the logs. (And hence has been active the last 6 months.) If it has been, write to a file Printers_in_use.txt "$portname is in use!" And if not write to another file Printers_not_in_use.txt "$portname is not in use!" Loop through next line of the file. Currently my script is looking like this: #Sorting the logfile to filter on only EventID 10 $events = Get-EventLog System | where {$_.EventID -eq 10} #Filtering all but the latest 6 months from prev selection $events = Get-EventLog System | where {$_.timegenerated -gt $(Get- Date).AddMonths(-6)} #Making a variable for the printerports $printerport = Get-Content Printerports.txt My powershell skills however are poor, I've just started to learn it. Since this is a kind of advanced script (atleast to me) I would be very happy for some help/hints. As you can see I'm stuck at the looping part. I have some ideas, but I doubt they will work. Googling hasnt given me many tips either. Thanks in advance! |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Inactive printer-checking script After you have filtered the Event Logs, and concatenated all corresponding messages into one, let PowerShell's Switch loop through the contents of Printerports.txt and compare each port to the 'one' message and classify it accordingly. $active = $inactive = @() $sixMonths = (date).addMonths(-6) Get-EventLog System | where {$_.EventID -eq 10 -and $_.TimeGenerated -gt $sixMonths} | % {$messages = ''} { $messages += $_.message} Switch (gc Printerports.txt) { {$messages -match [regex]::escape($_)} {$active += $_} default {$inactive += $_} } set-content Active.txt $active set-content Inactive.txt $inactive -- Kiron |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Inactive printer-checking script Thanks for the advices! This is the script I intend to use, lets see if you can find any weaknesses in it ![]() #Sorting the logfile to filter on only EventID 10 $events = Get-EventLog System | where {$_.EventID -eq 10} #Filtering all but the latest 6 months from prev selection $events = $events | where {$_.timegenerated -gt $(Get- Date).AddMonths(-6)} #Making a variable for the printerports $printerports = Get-Content Printerports.txt #Saving hostname into a variable $hostname = hostname $counter = 0 New-item -type directory -path $hostname while ($counter -ne ($printerports.count-1)) { #Looping through the eventlog and matching against the portname $foundprinted = 0 foreach($line in $events) { if ($line.message -match $printerports[$counter]) { $foundprinted = 1 } # if ($counter -eq ($printerports.count-1)) { return } } if ($foundprinted) { write-host -ForegroundColor Green $printerports[$counter]"is in use!" add-content -path $hostname\ValidPrinters_$hostname.txt -Value $printerports[$counter] } else { write-host -ForegroundColor red $printerports[$counter]" is not in use!" add-content -path $hostname\InvalidPrinters_$hostname.txt -Value $printerports[$counter] } $counter=$counter+1 } I think it does the trick, so I'm now working on a remote version ![]() |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Inactive printer-checking script Thanks for the advices! This is the script I intend to use, lets see if you can find any weaknesses in it ![]() #Sorting the logfile to filter on only EventID 10 $events = Get-EventLog System | where {$_.EventID -eq 10} #Filtering all but the latest 6 months from prev selection $events = $events | where {$_.timegenerated -gt $(Get- Date).AddMonths(-6)} #Making a variable for the printerports $printerports = Get-Content Printerports.txt #Saving hostname into a variable $hostname = hostname $counter = 0 New-item -type directory -path $hostname while ($counter -ne ($printerports.count-1)) { #Looping through the eventlog and matching against the portname $foundprinted = 0 foreach($line in $events) { if ($line.message -match $printerports[$counter]) { $foundprinted = 1 } # if ($counter -eq ($printerports.count-1)) { return } } if ($foundprinted) { write-host -ForegroundColor Green $printerports[$counter]"is in use!" add-content -path $hostname\ValidPrinters_$hostname.txt -Value $printerports[$counter] } else { write-host -ForegroundColor red $printerports[$counter]" is not in use!" add-content -path $hostname\InvalidPrinters_$hostname.txt -Value $printerports[$counter] } $counter=$counter+1 } I think it does the trick, so I'm now working on a remote version ![]() |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: Inactive printer-checking script Thanks for the advices! Heres the script I intend to use, lets hope you wont find any weaknesses in it ![]() #PrinterChecking Script By Henrik Pedersen Åmark and Daniel Lundgren #This script is designed to check what printers that is still in use. #It's assumed the printer is classed as inactive if it hasnt been used in 6 months #The script is reading the Systemlog on a printserver, #sorting the log on eventID 10 (successful print) and searching for entries the latest 6 months. #When that is done, it reads a textfile and saves each line (portname) into an array. # #You will need admin rights to use the script # #Sorting the logfile to filter on only EventID 10 #$events = Get-EventLog System | where {$_.EventID -eq 10} #Filtering all but the latest 6 months from prev selection #$events = $events | where {$_.timegenerated -gt $(Get- Date).AddMonths(-6)} $events=get-eventlog system|where {($_.eventid -eq 10) -and ($_.timewritten -gt (get-date).addmonths(-6))} #Making a variable for the printerports $printerports = Get-Content Printerports.txt #Saving hostname into a variable $hostname = hostname $counter = 0 New-item -type directory -path $hostname while ($counter -ne ($printerports.count-1)) { #Looping through the eventlog and matching against the portname $foundprinted = 0 foreach($line in $events) { if ($line.message -match $printerports[$counter]) { $foundprinted = 1 } # if ($counter -eq ($printerports.count-1)) { return } } if ($foundprinted) { write-host -ForegroundColor Green $printerports[$counter]"is in use!" add-content -path $hostname\ValidPrinters_$hostname.txt -Value $printerports[$counter] } else { write-host -ForegroundColor red $printerports[$counter]" is not in use!" add-content -path $hostname\InvalidPrinters_$hostname.txt -Value $printerports[$counter] } $counter=$counter+1 } It does the trick however, perhaps not the most optimized way tho. However I'm working on a remote version now ![]() |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: Inactive printer-checking script Hi Henrik, I've added a few annotated suggestions to your --and Daniel's-- script. I would like to know if you tried using a Switch statement and how it peforms against yours and Daniel's. I forgot to mention that the Switch statement has a -File parameter, so the line: Switch (gc Printerports.txt) { ...could be replaced by: Switch -file .\Printerports.txt { ...anyway here is your script, watch out for word wrapping: #PrinterChecking Script By Henrik Pedersen Åmark and Daniel Lundgren #This script is designed to check what printers that is still in use. #It's assumed the printer is classed as inactive if it hasnt been used in 6 months #The script is reading the Systemlog on a printserver, #sorting the log on eventID 10 (successful print) and searching for entries the latest 6 months. #When that is done, it reads a textfile and saves each line (portname) into an array. # #You will need admin rights to use the script # #Sorting the logfile to filter on only EventID 10 #$events = Get-EventLog System | where {$_.EventID -eq 10} #Filtering all but the latest 6 months from prev selection #$events = $events | where {$_.timegenerated -gt $(Get-Date).AddMonths(-6)} ############################################ # assigning the date to compare against to a variable saves time during execution $thisDate = (get-date).addmonths(-6) $events=get-eventlog system|where {($_.eventid -eq 10) -and ($_.timewritten -gt $thisDate)} #Making a variable for the printerports $printerports = Get-Content Printerports.txt # --> file name/path should not be hardcoded, consider defining it as a parameter #Saving hostname into a variable $hostname = 'hostname' # --> should not be hardcoded, consider defining it as a parameter $counter = 0 New-item -type directory -path $hostname ############################################ # while ($counter -ne ($printerports.count-1)) --> will exit before traversing entire $printerports array while ($counter -ne $printerports.count) { #Looping through the eventlog and matching against the portname # $foundprinted = 0 --> not necessary unless counting 'used' instances foreach($line in $events) { if ($line.message -match $printerports[$counter]) # --> consider escaping the printerport name like [regex]::escape($printerports[$counter]), if the name contains a '[' --or any of these characters #.$^{()*+?\|-- PowerShell will raise an exception or output erroneous result { # $foundprinted = 1 --> not necessary unless counting 'used' instances ############################################ write-host -ForegroundColor Green $printerports[$counter]" is in use!" add-content -path $hostname\ValidPrinters_$hostname.txt -Value $printerports[$counter] # break out of the foreach loop break } else { write-host -ForegroundColor red $printerports[$counter]" is not in use!" add-content -path $hostname\InvalidPrinters_$hostname.txt -Value $printerports[$counter] } # if ($counter -eq ($printerports.count-1)) { return } } ############################################ # use the increment operator $counter++ } -- Kiron |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: PS Script to automate checking date of file and delete if old | Shay Levy [MVP] | PowerShell | 0 | 07-09-2008 06:19 PM |
| Error checking with WMI Lastreboot script | TimParker | PowerShell | 1 | 06-19-2008 01:43 AM |
| Help with Powershell - Disk space checking script - New to PS | darren1 | PowerShell | 5 | 04-26-2008 12:26 AM |
| Send-FTP Script with Error Checking? | Michael Trantas | PowerShell | 6 | 01-14-2008 07:30 AM |
| Re: checking for results when executing a script | Kiron | PowerShell | 0 | 12-06-2007 08:28 PM |