Search code examples
powershellbatch-filegreptail

Select a string from a tailing log


I have an application log for which I am trying to write a batch file that will tail the log and return strings that contain "queue size" so that the updating queue size can be displayed. Basically the Windows equivalent of:

tail -f app.log | grep "queue size"

From what I've read I would need to use Windows powershell. I have devised the following script:

powershell -command Select-String -Path C:\logs\app.log -Pattern "queue size"

This gives me the following error:

Select-String : A positional parameter cannot be found that accepts
argument 'size'. At line:1 char:1
+ Select-String -Path C:\logs\app.log -Pattern queue size
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-String], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectStringCommand

Although this as it stands doesn't work, would it constantly update with the current logic?


Solution

  • You need to wrap the command in double quotes and use single quotes for the pattern:

    powershell -command "Select-String -Path C:\logs\app.log -Pattern 'queue size'"