PowerShell Count


  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #1

    PowerShell Count


    I have created this line of code which works brilliantly run from a Batch Script . . .

    Code:
    
    Powershell Get-EventLog -LogName 'System' -EntryType 'Error' -After (Get-Date).AddHours(-%Period%) -ErrorAction 'SilentlyContinue' ^| Format-Table -Autosize ^| Out-String -Width 1000 ^| Out-File %OFN% -Append -Encoding UTF8

    I then thought that I would add a Total, easier said than done.

    This shows the correct Total on the screen but NOT in the output, for obvious reasons . . .

    Code:
    
    Powershell $EventCount=0; Get-EventLog -LogName 'System' -EntryType 'Error' -After (Get-Date).AddHours(-%Period%) -ErrorAction 'SilentlyContinue' ^| ForEach{$_;$EventCount++} ^| Format-Table -Autosize ^| Out-String -Width 1000 ^| Out-File %OFN% -Append -Encoding UTF8; ' Total Events is: ' +$EventCount

    So I thought that I would try and achieve this using a separate line of code. This puts the correct Total on the screen, but in the file it just shows Total Events is: . . .

    Code:
    
    Powershell $EventCount=0; Get-EventLog -LogName 'System' -EntryType 'Error' -After (Get-Date).AddHours(-%Period%) -ErrorAction 'SilentlyContinue' ^| ForEach{$_;$EventCount++} ^| Format-Table -Autosize ^| Out-String -Width 1000 ^| Out-File %OFN% -Append -Encoding UTF8;' Total Events is: '+$EventCount
    
    Powershell 'Total Events is: ' +$EventCount ^| Out-File %OFN% -Append -Encoding UTF8

    I have spent hours on this, but I am sure that you PowerShell guys will get this straight away.

    Thanks.
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #2

    OK, I came up with a dirty way of achieving this . . .

    Code:
    
    Powershell Get-EventLog -LogName 'System' -EntryType 'Error' -After (Get-Date).AddHours(-%Period%) -ErrorAction 'SilentlyContinue' ^| Format-Table -Autosize ^| Out-String -Width 1000 ^| Out-File %OFN% -Append -Encoding UTF8
    
    Powershell 'Total Events: ' ^| Out-File %OFN% -Append -Encoding UTF8
    
    Powershell (Get-EventLog -LogName 'System' -EntryType 'Error' -After (Get-Date).AddHours(-%Period%) -ErrorAction 'SilentlyContinue').Count ^| Format-Table -Autosize ^| Out-String -Width 1000 ^| Out-File %OFN% -Append -Encoding UTF8
    
    
    

    I have got to go to bed, it is gone 01:30 a.m.

      My Computer


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #3

    I have decided to go with . . .

    Code:
    
    Powershell Get-EventLog -LogName 'System' -EntryType 'Error' -After (Get-Date).AddHours(-%Period%) -ErrorAction 'SilentlyContinue' ^| Format-Table -Autosize ^| Out-String -Width 1000 ^| Out-File %OFN% -Append -Encoding UTF8
    
    Powershell 'Total Events: ' + (Get-EventLog -LogName 'System' -EntryType 'Error' -After (Get-Date).AddHours(-%Period%) -ErrorAction 'SilentlyContinue').Count ^| Out-File %OFN% -Append -Encoding UTF8
    

    I am going to mark this thread as Solved as the above code does what I want, although NOT very elegantly.

      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 08:43.
Find Us




Windows 10 Forums