Link to home
Start Free TrialLog in
Avatar of AXISHK
AXISHK

asked on

Display PowerScript Header result once only

Is it possible to control the output of the following script such that it will only show header once when looping through logfiles in a folder ?  Thx

Result
---------
date       time     Username    IP            UserAgent              
----       ----     --------    --            ---------              
2018-07-07 19:46:49 abc\johnlee 40.69.186.133 Outlook-iOS-Android/1.0
2018-07-07 20:40:50 abc\johnlee 40.69.186.133 Outlook-iOS-Android/1.0

date       time     Username    IP            UserAgent              
----       ----     --------    --            ---------              
2018-07-07 19:46:49 abc\johnlee 40.69.186.133 Outlook-iOS-Android/1.0
2018-07-07 20:40:50 abc\johnlee 40.69.186.133 Outlook-iOS-Android/1.0

Powershell Script
-------------------------
$userName = "johnlee"

$logFile = Get-ChildItem C:\Temp -Recurse -Include myTest* |  Select-Object -First 1 | Select FullName
$logFiles = Get-ChildItem C:\Temp -Recurse -Include myTest* | Select FullName

$header = (Get-Content -Path $logFile.FullName -TotalCount 4)[-1] -replace '\A#Fields:\s' -split ' '
 
foreach ($currentlog in $logFiles) {
  Get-Content -Path $currentlog.FullName | Select-Object -Skip 4 | ConvertFrom-Csv -Header $header -Delimiter ' ' |
      Where-Object {$_.'cs-username' -Match $userName} |
      Select-Object -Property Date, Time, @{n='Username'; e={$_.'cs-username'}}, @{n='IP'; e={$_.'c-ip'}}, @{n='UserAgent'; e={$_.'cs(User-Agent)'}} | Format-Table -AutoSize}
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial