Hello.
I'm trying to write a script for remotly query a printerserver and check how many times all printerques on it has been used the last month.
My pseudocode looked like this:
Get remote eventlog, sort on ID 10 and type "print"
Get remote printerque, printerport list
read the first message from eventlog
match agains first portname
if match, add 1 to a counter
else take next portname/que and match against the same message.
when all ports has been tried, take next message and write to a file how many times portname/que has been used
Repeat
Ofc this will take some time, but that isnt the biggest problem.
The biggest problem is that it wont work at all since I'm looping in the wrong order, but I cant of any other way that I'm able write myself.
Perhaps someone has an idea?
My code currently look like this:
Write-Host "What host do you want to get the eventlog from?"
$hostname = Read-Host
#Write-Host "How many months back do you want to check?"
#$months = Read-Host
if ((Test-Path -path $hostname) -ne $True)
{
New-Item -type directory -path $hostname
}
#Get Eventlog from a remote host
$events = gwmi -ComputerName $hostname -query "select * from win32_ntlogevent where logfile='system' and eventcode='10' and sourcename='print'" | Select-Object EventCode, Timegenerated, Message | sort Timegenerated
#Making a variable for the printerports
$printerports = gwmi -computername $hostname Win32_Printer | Select-Object Portname, name
$counter = 0
$eventcounter = 0
while ($eventcounter -ne ($events.count-1))
{
foreach ($line in $printerports)
{
if ($line.portname -match $events[$eventcounter].message)
{
write-host -ForegroundColor Green "Det funkar"
}
else
{
write-host -ForegroundColor Red "Det funkar inte"
$counter = $counter + 1
}
}
$eventcounter = $eventcounter + 1
}
Hoping for some answers.
//GD