dc3sops
asked on
Powershell Pipeline Out-File/Log File
Hello,
I need assistance creating/logging to an outfile via a powershell pipeline. The code I've written should parse the child folders in $path var to an array, then delete nested folders from $path$dir older than x which in this instance is 60 days. What I need help with is creating a log file to keep track of what was deleted. With my limited powershell knowledge I am unable to figure out a proper way to output 1. the path of the deleted folders and 2. getting output for all events taking place in the loop/array using the $log_file var.
Thanks in advance!
I need assistance creating/logging to an outfile via a powershell pipeline. The code I've written should parse the child folders in $path var to an array, then delete nested folders from $path$dir older than x which in this instance is 60 days. What I need help with is creating a log file to keep track of what was deleted. With my limited powershell knowledge I am unable to figure out a proper way to output 1. the path of the deleted folders and 2. getting output for all events taking place in the loop/array using the $log_file var.
$date = (get-date -format yyyyMMdd)
$log_file = "backup_cleanup_$(get-date -format yyyyMMdd).log"
$path = "C:\Program Files\Backup\"
$file_year = get-date -format yyyy\"_*"
$arr = Get-ChildItem $path |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.Name}
ForEach ($dir in $arr)
{
Get-ChildItem $path$dir"\" | Where {$_.PSIsContainer -and `
$_.LastWriteTime -le (get-date).adddays(-60)} | Remove-Item -recurse -whatif
}
Thanks in advance!
ASKER
Thanks, footech. That worked beautifully.
One more question, is there a way I can add a header message, something like "---- folders removed ----" at the top of the log before the input from remove-item?
One more question, is there a way I can add a header message, something like "---- folders removed ----" at the top of the log before the input from remove-item?
If you had PS 3.0 would be easiest, then we could make use of the -append parameter for Tee-Object, and we would use a Set-Content command to write the header at the beginning of the script. Here's a way we could do it with PS 2.0.
$date = (get-date -format yyyyMMdd)
$log_file = "backup_cleanup_$(get-date -format yyyyMMdd).log"
$path = "C:\Program Files\Backup\"
$file_year = get-date -format yyyy\"_*"
Set-Content "---- folders removed ----" -path $log_file
Get-ChildItem $path |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.FullName} |
ForEach-Object {
Get-ChildItem $_ | Where {$_.PSIsContainer -and `
$_.LastWriteTime -le (Get-Date).adddays(-60)}
} |
Select -expandProperty FullName |
Tee-Object -Variable out |
Remove-Item -recurse -whatif
$out | Add-Content $log_file
ASKER
I have PS 3.0. I've done something similar as you suggest. The only problem is that I get NUL chars in between text chars in the log file.
$current_time = (get-date -format yyyyMMdd\_HHmm)
$log_file = "backup_cleanup_$(get-date -format yyyyMMdd).log"
$path = "C:\Program Files\Backup\"
Set-Content "--- Folders Removed from $($path) on $($current_time) ----" -path $log_file
Get-ChildItem $path |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.FullName} |
ForEach-Object {
Get-ChildItem $_ | Where {$_.PSIsContainer -and `
$_.LastWriteTime -le (Get-Date).adddays(-30)}
} |
Select -expandProperty FullName |
Tee-Object -FilePath $log_file -append |
Remove-Item -recurse -whatif
Can you post an example?
ASKER
Attached is a screenshot. I'm using notepad++ to view the log but with notepad there is a space between each char. Also attached.
nul-chars.png
nul-char2.png
nul-chars.png
nul-char2.png
Interesting! I get that when I use the -append parameter, but not when I use the code in http:#a39500138
I'll see if I can find a reason for it, but if not then the alternative works well.
I'll see if I can find a reason for it, but if not then the alternative works well.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent! That solved the formatting issue. Thanks so much for your help on this. I've increased the points because you've been such a huge help!
ASKER
Stuck with the problem until all issues with PS 3.0 script were resolved even though he provided an optional working solution for PS 2.0.
Thanks for the feedback.
Cheers!
Cheers!
Open in new window