Sunday 15 January 2012

PowerShell: Searching for a part of a string in a logfile -


I am trying to search for a pattern, "error", in a series of logfiles, then the result Writing in a text file.

I am using the command:

  foreach ($ file in $ file) {$ match = get-content $ file | Selection - string $ p | Outfile $ matchfile}   

Pattern $ p is taken from the command line I am running a script on Windows Server 2008 R2, and returns the result if a word in the "error" logfile is. I have an example of "error code", which I want to identify the script.

I have a code working on Windows 7 as I want. Although I do not find any results on running the script on Windows Server 2008 R2, both PowerShell versions 2.0 are available.

Are you actually working on Windows 7? It has been written by you, each instance of the foreach loop overwrites the matchfile in $ files, resulting in the final file. You can add an Append switch after the out-file, but you do not even need a foreach loop. Besides, what is the purpose of the $ match? $ match = does not look too much for me: try it:

  go-content $ files | Selection - string $ p | $ Matchfile Out-File   

I suspect that you are not getting any results on the 2K8 server, it is that there is no match for the last file $ p in $ files. On Windows 7 machine, you are getting results because the last file is available for $ p, but you are not getting any results from other files.

No comments:

Post a Comment