New
#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 aTotal
, easier said than done.
This shows the correctTotal
on the screen butNOT
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 correctTotal
on the screen, but in the file it just showsTotal 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.