How to monitor only first/last x lines - PowerShell
search cancel

How to monitor only first/last x lines - PowerShell

book

Article ID: 44449

calendar_today

Updated On:

Products

DX Unified Infrastructure Management (Nimsoft / UIM)

Issue/Introduction

If using command mode with Windows PowerShell, only first/last x lines can be monitored by logmon probe.

Unlike UNIX/Linux, Windows does not have head/tail commands. However, PowerShell has the equivalent "Get-Content" cmdlet. User can use the cmdlet for printing first/last x lines on Windows.

 

Environment

Any UIM and logmon version

Resolution

Instructions: 

Here is the example procedure.

  1. Create a batch file like below.

 

rem Wrapping powershell script in batch file 

@powershell -NoProfile -ExecutionPolicy Unrestricted "$s=[scriptblock]::create((gc \"%~f0\"|?{$_.readcount -gt 1})-join\"\");&$s" %*&goto:eof 

rem Printing first x lines on monitored log file

(Get-Content <Monitored log file name>)[0..x]

 

  1. Open logmon probe configuration
  2. Create a profile with command mode, and select the created batch file on [Command] field.
  3. Apply the setting, then logmon probe will start to monitor.

 

 

Additional Information

Additional Information:

Windows PowerShell Management Cmdlets - Get-Content

Make it easier to wrap powershell scripts in batch files