Using log parser to parse lot of logs in different folders - sharepoint

I recently started to use Log Parser with visual interface.
The logs that I want to parse come from IIS, and they are related to SharePoint. For example, I want to know how many people were visiting particular web pages, etc.
And it seems that IIS creates logs in different folders (I don't know why) and every day there is a new log file in a different folder.
So my question is, is it possible to approach all those files in different folders?
I know you can use From-clause, put different folders, but it is too difficult especially if in the future new folders are added. The goal is to create one script which would be executed.
So for example in a folder log named LogFIles, I have folders folder1, folder2, folder3, folder4, etc. and in each folder there are log files log1, log2, log 3, logN, etc.
So my query should be like this: Select * FROM path/LogFiles/*/*.log but the log parser doesn't accept it, so how to realize it?

You can use the -recurse option when calling logparser.
For example:
logparser file:"query.sql" -i:IISW3C -o:CSV -recurse
where query.sql contains:
select *
from .\Logs\*.log
and in my current directory, there is a directory called "Logs" that contains multiple sub-directories, each containing log files. Such as:
\Logs\server1\W3SVC1
\Logs\server1\W3SVC2
\Logs\server2\W3SVC1
\Logs\server2\W3SVC2
etc.

You can merge the logs then query the merged log
what i have to do is
LogParser.exe -i:w3c "select * into E:\logs\merged.log from E:\logs\WEB35\*, E:\logs\WEB36\*, E:\logs\WEB37\*" -o:w3c

I prefer powershell like this:
Select-String C:\Logs\diag\*.log -pattern "/sites/Very" | ?{$_.Line -match "Important"}

LogParser's help does not list the -recurse option so I'm not sure it it's still supported. However, this is what I did to get around it:
Let's say you use the following command to execute logparser -
logparser "SELECT * INTO test.csv FROM 'D:\samplelog\test.log'" -i:COM -iProgID:Sample.LogParser.Scriptlet -o:CSV
Then simply create a batch script to "recurse" through the folder structure and parse all files in it. The batch script that does this looks like this -
echo off
for /r %%a in (*) do (
for %%F in ("%%a") do (
logparser "SELECT * INTO '%%~nxF'.csv FROM '%%a'" -i:COM -iProgID:Sample.LogParser.Scriptlet
REM echo %%~nxF
)
)
Execute it from the path where the log files that need to be parsed are located.
This can further be customized to spit all parsed logs in one file using append (>>) operator.
Hope this helps.

Check this out: https://stackoverflow.com/a/31024196/4502867 by using powershell to recursively get file items in sub directories and parse them.

Related

ADF Azure Data-Factory loop over folder syntax - wilcard?

i'm tryimg to loop over a diffrent countries folder that got fixed sub folder named survey (i.e Spain/survey , USA/survey ).
where and how I Need to define a wildcard / parameter for the countries so I could loop over all the files that in the survey folder ?
what is the right wildcard syntax ? ( the equivalent of - like 'survey%' in SQL) ?
I tried several ways to define it with no success and I would be happy to get some help on this - Thanks !
In case if the list of paths are static, you can create a parameter or add it in a SQL database and get that result from a lookup activity.
Pass the output to a for each activity and within foreach activity use a copy activity.
You can parameterize the input dataset to get the file paths thereby you need not think of any wildcard characters but use the actual paths itself.
Hope this is helpful.

Searching in multiple files using findstr, only proceeding with the resulting files? (cmd)

I'm currently working on a project where I search hundreds of files using findstr in the command line. If I find the string which I searched for, I want to proceed with this exact file (and the other ones that include my string).
So in my case:
I searched for the string WRI2016 by using:
H:\KOBINI>findstr "WRI2016" *.ini > %temp%\xx.txt && %temp%\xx.txt
To see what the PC does, I save it in a .txt file as you can see.
So if my file includes WRI2016 I want to extract some facts out of the file. In my case it is NR, Kunde, WebHDAktiv, DigIDAktiv.
But I just can't find a proper way to link both of these functions.
At first I simply printed all of the parameters:
H:\KOBINI>findstr "\<NR Kunde WRI2016 WebHDAktiv DigIDAktiv" *.ini > %temp%\xx.csv && %temp%\xx.csv
I also played around using the if command but that didn't really work out. I'm pretty new to this stuff as you'll see in my following tries to solve this problem:
H:\KOBINI>findstr "\<NR DigIDAktiv WebHDAktiv" set a =*.ini findstr "WRI2016" set b =*.ini if a EQU b > %temp%\xx.txt && %temp%\xx.txt
So all I wanted to achieve with that weird code was: if there is a WRI2016 in the file, give me the remaining parameters. But that didn't work out at all.
I also tried it with using new lines for every command which didn't change a thing.
As I want this to be a .csv in the end I want to add a semicolon between my parameters, any chance how I could do that? I've seen versions using -s";" which didn't do anything for me.
Sorry, I'm quite new and thought I'd give it a shot.
an example of my .ini files Looks like this:
> Kunde=Markt
> Nr=101381
> [...]
> DigIDAktiv=Ja
> WebHDAktiv=Nein
> Version=WRI2016_U2_P1
some files have a different Version though.
So I only want to know "NR, DigIDAktiv ..." if it's the 2016 Version.
As a result it should be sorted in a CSV, in different columns.
My Folder Looks like this
So I search These files in order to find Version 2016 and then try to extract my Information and put it into a .csv

If folder exists based on string, copy files to existing folder

I have a series of folders that I individually download via FTP on a daily basis.
Folder names are formatted as follows:
[DATE] [ID NO] [ID NAME]
Example:
W:\20150101 G0101 Building 1
W:\20150101 G0102 Building 2
W:\20150101 G0103 Building 3
[ID NO] and [ID NAME] are always the same. [DATE] changes each day.
I am trying to write a batch file to copy the contents of each FTP folder on a daily basis containing a known string to an equivalent local folder. Once the contents of each FTP folder are copied, the FTP folders are removed.
For example:
Always copy the folder containing the string "G0101" on FTP drive "W:\" to "C:\Building 1"
Always copy the folder containing the string "G0102" on FTP drive "W:\" to "C:\Building 2"
I have been playing around with IF EXIST but I can’t quite get the syntax right. Any assistance would be most appreciated!
IF EXIST "C:\00\*G0101*" XCOPY "C:\00\*G0101*\*.*" "C:\00\Building 1"
Regards
Martin :-)
You can only include a wildcard in the last element of the path. So, you need to enumerate the source folders and for each of them execute the xcopy operation
for /d %%a in ("c:\00\*G0101*") do xcopy "%%~fa\*.*" "c:\00\Building 1"
The for /d will search the folders matching the wildcard and for each of them a reference will be stored in the replaceable parameter %%a and the code in the do clause will be executed. %%~fa is just the reference to the folder with full path.
The code is written to be used inside a batch file. For usage in command line, replace all the double percent signs with single percent signs

Manipulating Strings that are inside Text Files

Here's my situation:
We depend on users to click some .bat and do the daily-backup.
I did some batching programming, now when they (are forced to) back-up, it leaves a log file in it's own the server folder. And yes, they need to be forced otherwise they just won't do the g.damn backup... Welcome to my company.
Then, after some more research, I developed another bat which checks if the file is outdated and prints it on a text file.
This is the kind of files that I have now:
File: DailyBkp
Content:
".\John\log.log"
".\Department1\Andy\log.log"
".\Department1\Nicole\log.log"
".\Department2\Ann\log.log"
File: Departments
Content:
Department1
Department2
...
Great, after some string treatment in PowerShell I managed to get DailyBkp content to be this:
John
Department1 Andy
...
Note that it has a space at beginning and another at the end which won't go away no matter what trim I use...
So now I have this setup: SERVER > E:\Backup\
Inside backup we have i.e. "Department 1" ... "Dep.-N"
Inside each one: "User-Lastname1" .. "User-lastname-N"
What I need, or what I do want to is use PowerShell to get the contents of DailyBkp containing "Dept_User" string, and export entries to a csv file like this:
COLUMN_Name COLUMN_Dept
Andy Department1
So, how do I do this? I can't find anything on internet that uses text files.
so assuming I am reading your question correctly you have a DailyBkp file that looks like this:
Department1 Andy
Dpeartment2 Jim
...
to get what you want you could read each line in split it out then spit it back out in a csv like this:
"COLUMN_Name,COLUMN_Dept" | out-file <path>\outfile.csv
$bkpFile = get-content $dailyBkpFile
$bkpFile | ForEach-Object {$line = $_.split(" ")
"$($line[1]),$($line[0])" | out-file <path>\outfile.csv -Append}
output should look like this:
COLUMN_Name,COLUMN_Dept
Andy,Department1
Jim,Department2
just doind a quick post here.
My question was answered, thanks.
And, if anyone is interested in Powershell programming, google Mastering Powershell by Tobias Weltner; this is a free book with lots of explanations.

Powershell how to take string from file and put into a variable

I am writing a Powershell script to "build" Windows 7 PCs: adding users, printers, applications, changing settings, et al. I am adding some printer drivers using PNPUtil, but the problem is I won't know what "Published name" the drivers will be given.
If I put the output from the PNPUtil command into a .txt file, is there a way for me to then take the __.inf Published name and put it into a variable so that I can then use that name to add the printer using $printerclass.CreateInstance()?
You don't have to use a file if PNPUtil only outputs the name your interested in. That is, you can assign its output to a variable like so:
$result = pnputil.exe
BTW if you want to use a file, to read content from a file you use Get-Content:
pnputil.exe > result.txt
$result = Get-Content result.txt
$line = $result | Foreach {if ($_ -match 'assigned an (\w+\.inf)') {$matches[1]}}
Okay - I found my own solution: Once the .inf file is added, all the driver names in that .inf are stored in the Microsoft update files. I just need to know the specific name of the driver I need from each .inf file in order to add the Printers.
However, I'd still love to know how to get a string from a line from a file using Powershell.

Resources