Link to home
Start Free TrialLog in
Avatar of dc3sops
dc3sopsFlag for United States of America

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.


$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
	}

Open in new window


Thanks in advance!
Avatar of footech
footech
Flag of United States of America image

Try the following.  I assume you've got some other purpose for $date and $file_year so I left them in there.
$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\"_*"
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 -FilePath $log_file |
 Remove-Item -recurse -whatif

Open in new window

Avatar of dc3sops

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?
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

Open in new window

Avatar of dc3sops

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

Open in new window

Can you post an example?
Avatar of dc3sops

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
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.
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dc3sops

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!
Avatar of dc3sops

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!