I am trying to find a way to incorporate multiple -replace into the statement below. When I try to add another section like {$Header -replace “<Commit ts=”,“”} it give me a missing statement block at the end. Any ideas for me? Thanks
Trying to do multiple replace like.
{$Header -replace “<Commit ts=”,“”}
{$Header -replace " >“,”"}
You can simply chain multiple replaces and if you actually want to remove partial strings you can ommit the second value from the -replace operator. … like this:
Thanks Olaf. Where would I put this in the statement below? I tried to add $Header -replace ‘<Commit ts=’ after { $Header = $_ } and it give me a missing block error at the end of the statement. Sorry for my ignorance I am relatively new at powershell.
Actually I don’t know. I did not get how the two code snippets you posted are related with each other. You may explain a tlittle more detailed what you’re actually trying to do.
When I run this code above it generates one line per record in the format below. There are many codes but this is one example.
<Commit ts="20210311040006" client=""> "39",”Save Data","Process 'Save Data' was executed."
I would like to make this more manageable to load into a DB. If I replace the “<Commit ts=” with nothing. Replace the client= with a comma and replace > with a comma. Example below. Then it would simple be a comma separated file and easily loaded into a DB.
"20210311040006","","39",”Save Data","Process 'Save Data' was executed."
So it’s about extracting particular strings from each line of an input file. Could you please post a few lines of the file in the original format. Please format it as code an obfuscate sensitive information if there are some.
I use the powershell script I outlined above to get the header and data on one line making it easier to load. Now just looking to format in comma separated to make easy to load into the database. Thanks!
<Commit ts="20210311040006" client=" automation_user">
"144"," automation_user","122.2.2.2","User 'automation _user' successfully logged in from address '122.2.2.2'."
<Commit\>
<Commit ts="20210311040006" client="">
"39",”Save Data","Process 'Save Data' was executed."
<Commit\>
<Commit ts="20210311125155" client="Steve">
"144","Steve","11.111.111.111","User 'Steve' successfully logged in from address '11.111.111.111'."
<Commit\>
<Commit ts="20210311130404" client="Shelly">
"144","Shelly","11.111.111.111","User 'Shelly' successfully logged in from address '11.111.111.111'."
<Commit\>
<Commit ts="20210311131122" client="Daniel">
"144","Daniel","11.111.111.111","User 'Daniel' successfully logged in from address '11.111.111.111'."
<Commit\>
<Commit ts="20210311131122" client="Daniel">
"144","Daniel","11.111.111.111","User 'Daniel' successfully logged in from address '11.111.111.111'."
<Commit\>
<Commit ts="20210311131747" client="Shelly">
"144","Shelly","11.111.111.111","User 'Shelly' successfully logged in from address '11.111.111.111'."
<Commit\>
<Commit ts="20210311135147" client="Shelly">
"144","Shelly","11.111.111.111","User 'Shelly' successfully logged in from address '11.111.111.111'."
<Commit\>
<Commit ts="20210311135151" client="Tom">
"144","Tom","11.111.111.111","User 'Tom' successfully logged in from address '11.111.111.111'."
<Commit\>
<Commit ts="20210311135250" client="">
"21","Version","Dimension 'Version' was updated."
<Commit\>
<Commit ts="20210311135250" client="">
"27","Version:Working Forecast","Version:07_05 Vs Working Forecast","1.0","-1.0","Element 'Version:Working Forecast' in consolidation 'Version:07_05 Vs Working Forecast' was changed from weight '1.0' to weight '-1.0'."
<Commit\>
Of course you can pipe the result contained in the variable $result to Export-Csv or whatever you need.
The ouput with Format-Table would look like this:
Name DateTime Number User IP Description
---- -------- ------ ---- -- -----------
automation_user Do, 11.03.2021 04:00:06 144 automation_user 122.2.2.2 User 'automation _user' successfully logged in from address '122.2.2.2'.
Do, 11.03.2021 04:00:06 39 Save Data Process 'Save Data' was executed.
Steve Do, 11.03.2021 12:51:55 144 Steve 11.111.111.111 User 'Steve' successfully logged in from address '11.111.111.111'.
Shelly Do, 11.03.2021 13:04:04 144 Shelly 11.111.111.111 User 'Shelly' successfully logged in from address '11.111.111.111'.
Daniel Do, 11.03.2021 13:11:22 144 Daniel 11.111.111.111 User 'Daniel' successfully logged in from address '11.111.111.111'.
Daniel Do, 11.03.2021 13:11:22 144 Daniel 11.111.111.111 User 'Daniel' successfully logged in from address '11.111.111.111'.
Shelly Do, 11.03.2021 13:17:47 144 Shelly 11.111.111.111 User 'Shelly' successfully logged in from address '11.111.111.111'.
Shelly Do, 11.03.2021 13:51:47 144 Shelly 11.111.111.111 User 'Shelly' successfully logged in from address '11.111.111.111'.
Tom Do, 11.03.2021 13:51:51 144 Tom 11.111.111.111 User 'Tom' successfully logged in from address '11.111.111.111'.
Do, 11.03.2021 13:52:50 21 Version Dimension 'Version' was updated.
Do, 11.03.2021 13:52:50 27 Version:Working Forecast Version:07_05 Vs Working Forecast 1.0
The DateTime column may look different for you as I am on a German system.
Olaf when I run your statement against my file I get the following error. Any ideas what I am doing wrong?
ConvertFrom-Csv : Cannot validate argument on parameter 'InputObject'. The argument is null. Provide a valid value for the
argument, and then try running the command again.
At line:6 char:35
+ ... t[$i + 1] | ConvertFrom-Csv -Delimiter ',' -Header 'Number', 'User', ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertFromCsvCommand
So your file might look different to the sample data you posted. The function depends on the structure of the file. From your sample data I assumed that there are a kind of data sets in your file organised in 3 rows each right from the beginning. If that’s not the case the code will fail.
Ok I will look into it some more. The output csv seem to be keeping the some of the and so I will try to figure out how to exclude those from the file as well. Thanks
Usually there is no “too much” information. The most reliable solution would probably be to ask the manufacturer of the audit software to include a function to output the data in an interoperable format.
Of course there would be ways to deal with the two different data sets in your log file. But you would need to decide how. If there is a “<Details>” section what would you like to do with information one line before this “<Details>” section?
Here is a suggestion where the additional line in front of the “<Details>” section will be ignored.
Olaf thanks for the help this seems to be working with a small exception. The only issue is it appear to be giving an error if values are blank. I am guessing a -replace somewhere in your statement but not sure where would be the best place for this.
ConvertFrom-Csv : Cannot validate argument on parameter 'InputObject'. The argument is null. Provide a valid value for the
argument, and then try running the command again.
At line:6 char:35
+ ... t[$i + 1] | ConvertFrom-Csv -Delimiter ',' -Header 'Number', 'User', ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertFromCsvCommand
It’s pretty much impossible to help you with this without seeing your actual input data. But if you understand the code you can debug the script and adapt it to your particular environment. The code expects the logfile to have a line count dividable by 3. If that’s not the case there will be errors like this where no data are available for the cmdlet ConvertFrom-Csv.