When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.
When you read a log file with Get-Content you get strings. They don’t have a property LastWriteTime you could check. So you have to parse the strings and extract the string representation of the timestamps. Then you can convert this to type [DateTime] to be able to check if it meets the conditions you want.
First, you are trying to access a property that doesn’t exist. There is no attribute for those lines to say when they were written. You can check for certain properties or all properties.
Luckily you have a date/time stamp, so you could check that after you extract it. Unfortunately, you are breaking sections up with : so not the best choice to split with. You should be able to reconstruct your datetime object like this
Now you can compare and output the appropriate lines. Note, you want to use -gt to get the entries that are within the last day, -lt shows you the older entries. You could achieve both goals like this.
This will take the lines for the last 24 hours, stick them in “D:\TempOrder\ServerLog_$(get-date -f yyyy-MM-dd).txt”, and remove them from $oldlog. You may need to reverse the order of output if it adds the content back in the wrong order. Hope this helps.
Edit: Based on your title, I think I got it backwards. If so, just switch -gt to -lt and the reverse will happen. Any logs older than the time specified will go to $oldlog and the rest will go to serverlog.