I can see the general approach you're using, but I don't have access to an
event log with similar events shown in it to help confirm some things. There
is one likely problem as well as a couple of things that may need
confirmation.
+ The likely problem is the statement
if ( $printerports[$counter] -match $events.message )
You're attempting to match an object - a PSObject, as a matter of fact - to
a message property of the entire array of events that you've passed in. I
think you want to actually do something like this in that line:
if ( $printerports[$counter].Portname -match $line.message )
You might want to also explicitly print out $printerports and $events to
confirm that you _do_ have multiple items in each collection.
"The Key" <h.pedersen.amark@xxxxxx> wrote in message
news:dd2b4da3-e75e-416b-96de-161a34753452@xxxxxx
> So, I've been trying to get a script that tells me how many times all
> printerques on a printerserver has been used during a specific time
> (take the last month for example).
> As it is now, my current script isnt working at all since I'm not a
> good programmer and hasnt really understood looping.
>
> This is my current script:
> Write-Host "What host do you want to get the eventlog from?"
> $hostname = 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, DeviceID, __server, name
>
> $counter = 0
>
> while ($counter -ne ($events.count-1))
> {
> $portcount = 0
>
> foreach ($line in $events)
> {
> if ( $printerports[$counter] -match $events.message )
> {
> $portcount = $portcount +1
> }
> Write-Host $printerports[$counter] has been used $portcount this
> month.
> }
> $counter = $counter + 1
> }
>
> As you understand this script isnt working as I intend it to at all,
> any suggestions?