Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by stevens »

Hi,

I'm trying to monitor entries in a database (entries about status of an SCCM deployment).
For this I got a working script https://smsagent.wordpress.com/2015/07/ ... owershell/ = get-cmstatusmessages

I now would like to monitor the file and get only new items on top.
This is what I have so far but it is not working fully. I get results but first 10 rows then again first then rows below, not only the NEW ones and not on top of the result but at the bottom.

Please advise if this is the correct way to go. If yes, howto continue if no, how would you suggest to approach?
Thanks.
S.


#Elements of $Status => Severity Type SiteCode Date / Time System Component Module MessageID Description
  1. while($true)
  2. {
  3.     $i++
  4.    
  5.     $status = Get-CMStatusMessages -ComputerName $computername | select -first 10
  6.     $status | select 'Date / Time',description
  7.     Get-CMStatusMessages -ComputerName $computername | select -first 10  | select 'Date / Time',description # | where-object Description -NotContains $($status.description) does not work
  8.     #Items are put at the bottom whereas I'd like to have them on top
  9. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by jvierra »

Again this is unclear. What does "monitor a file" mean and what are "new entries on top".

In a text file items are added at the end. If your code is not returning objects then this doesn't make much sense.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by dan.potter »

1..10 | sort -Descending ?
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by stevens »

Thanks. Let me try to clarify. It's actually a logviewer i would like to build.
Hope it makes it more clear with an example on eventviewer:

1.I load logs at startup of the form, f.e. Get-EventLog -LogName Application -ComputerName localhost | select -First 10
2.Now I would like to do a continuous loop = refresh of this log and add extra items to this "logviewer" that are NEW
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by jvierra »

You would have to use the instance of the log to query for entries that are greater then the last instance.
Also you need to use Get-WinEvent.

Get-WinEvent -Logname Application -Max 10

Do not specify a computer name for the localhost as it can cause add behavior.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by jvierra »

Here is an example of how to get all future events:
  1. $filterXML = @'
  2. <QueryList>
  3.     <Query Id="0" Path="Application">
  4.         <Select Path="Application">
  5.             *[System[(EventRecordID> {0})]]
  6.         </Select>
  7.     </Query>
  8. </QueryList>
  9. '@
  10. $events = get-winevent -logname application -max 10
  11. $lastid = ($events | Measure-Object -Maximum RecordId).Maximum
  12. $filt = $filterXML -f $lastid
  13. Get-WinEvent -FilterXml $filt
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by stevens »

Thanks!
So I see there is a difference between the two methods: http://www.mcbsys.com/blog/2011/04/powe ... -eventlog/
and get-winvent is what I need.

Would you mind clarifying what your code exactly does, specifically why the xml is needed and then these lines

<Query Id="0" Path="Application">
<Select Path="Application">
*[System[(EventRecordID> {0})]]

Thanks!
S.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by jvierra »

The data portion of every provider is custom. It is stored as XML and must be queried with XML/XPath. Items like RecordID can only be filtered efficiently with an XML/XPath query.

Look up and study what the new "EventLog" is. It has been here since Vista and has dozens of new and enhanced features. "Get-EventLog" is retained for use with XP/WS2003 compatibility since they have the old binary event log.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by stevens »

Ok, thanks. However, might have found an alternative oneliner => http://www.happysysadm.com/2013/11/powe ... files.html
What would help me defining if an sccm client is correctly installed, not sure if it will help with checking the database latest items.

Get-ChildItem -Path "\\$computername\c$\Windows\ccmsetup\Logs\ccmsetup.log" -Recurse | Get-Content -Wait | Select-String 'CcmSetup is exiting with return code 0'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Monitor entries in a database = ok, but refresh only last restults on top via refresh?

Post by jvierra »

I cannot see what that has to do with the event logs.

For SCCM client management I recommend going to the source forum for SCCM on Microsoft. They will help you resolve issues with versions and provisioning issues.
This topic is 7 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked